Skip to content

Commit 33a4b47

Browse files
committed
release config gen writes file
1 parent 8ce282c commit 33a4b47

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

cmd/release/cmd/config.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cmd
22

33
import (
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
}

0 commit comments

Comments
 (0)