/
makeConfig.go
51 lines (38 loc) · 1.22 KB
/
makeConfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cmd
import (
"fmt"
"path/filepath"
"github.com/gridscale/gscloud/runtime"
"github.com/gridscale/gscloud/utils"
"github.com/spf13/cobra"
)
var makeConfigCmd = &cobra.Command{
Use: "make-config",
Short: "Create a new configuration file",
Long: fmt.Sprintf(`Create a new and possibly almost empty configuration file not overwriting an existing one if it exists. Prints the path to the newly created file to stdout.
# EXAMPLES
Create a new configuration file at the default configuration path (%s):
$ gscloud make-config
Create a new configuration file at a specified path:
$ gscloud --config /tmp/myconfig.yaml make-config
`, runtime.ConfigPathWithoutUser()),
RunE: func(cmd *cobra.Command, args []string) error {
filePath := filepath.Join(runtime.ConfigPath(), "config.yaml")
if rootFlags.configFile != "" {
filePath = rootFlags.configFile
}
if !utils.FileExists(filePath) {
err := runtime.WriteConfig(&runtime.Config{Projects: []runtime.ProjectEntry{{URL: defaultAPIURL}}}, filePath)
if err != nil {
return err
}
fmt.Printf("Written: %s\n", filePath)
} else {
fmt.Printf("%s already exists\n", filePath)
}
return nil
},
}
func init() {
rootCmd.AddCommand(makeConfigCmd)
}