File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change 11package cmd
22
33import (
4- "fmt"
4+ "errors"
5+ "os"
6+ "path/filepath"
57
68 "github.com/rancher/ecm-distro-tools/cmd/release/config"
79 "github.com/spf13/cobra"
@@ -17,11 +19,28 @@ var genConfigSubCmd = &cobra.Command{
1719 Use : "gen" ,
1820 Short : "Generates a config file in the default location if it doesn't exists" ,
1921 RunE : func (cmd * cobra.Command , args []string ) error {
22+ configPath := filepath .Clean (os .ExpandEnv (configFile ))
23+
24+ if _ , err := os .Stat (configPath ); err == nil {
25+ return errors .New ("config file already exists at " + configPath )
26+ } else if ! errors .Is (err , os .ErrNotExist ) {
27+ return err
28+ }
29+
2030 conf , err := config .ExampleConfig ()
2131 if err != nil {
2232 return err
2333 }
24- fmt .Println (conf )
34+
35+ if err := os .MkdirAll (filepath .Dir (configPath ), 0755 ); err != nil {
36+ return err
37+ }
38+
39+ if err := os .WriteFile (configPath , []byte (conf + "\n " ), 0600 ); err != nil {
40+ return err
41+ }
42+
43+ cmd .Println ("config file written to " + configPath )
2544 return nil
2645 },
2746}
You can’t perform that action at this time.
0 commit comments