Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions cmd/release/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"errors"
"os"
"path/filepath"

"github.com/rancher/ecm-distro-tools/cmd/release/config"
"github.com/spf13/cobra"
Expand All @@ -17,11 +19,28 @@ var genConfigSubCmd = &cobra.Command{
Use: "gen",
Short: "Generates a config file in the default location if it doesn't exists",
RunE: func(cmd *cobra.Command, args []string) error {
configPath := filepath.Clean(os.ExpandEnv(configFile))

if _, err := os.Stat(configPath); err == nil {
return errors.New("config file already exists at " + configPath)
} else if !errors.Is(err, os.ErrNotExist) {
return err
}

conf, err := config.ExampleConfig()
if err != nil {
return err
}
fmt.Println(conf)

if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
return err
}

if err := os.WriteFile(configPath, []byte(conf+"\n"), 0600); err != nil {
return err
}

cmd.Println("config file written to " + configPath)
return nil
},
}
Expand Down