Skip to content

Commit 72a86a7

Browse files
committed
config set now produces a new config if it does not exist
1 parent 5b520a9 commit 72a86a7

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

mg/cmd/config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
2122
"github.com/laetho/metagraf/internal/pkg/helpers"
2223
"github.com/spf13/cobra"
2324
"github.com/spf13/viper"
25+
log "k8s.io/klog"
2426
)
2527

2628
func init() {
@@ -41,15 +43,17 @@ var configCmdSet = &cobra.Command{
4143
Long: `set`,
4244
Run: func(cmd *cobra.Command, args []string) {
4345
if len(args) < 2 {
44-
fmt.Println("Insufficient arguments")
45-
return
46+
log.Fatal("Insufficient arguments")
47+
4648
}
4749
if helpers.StringInSlice(args[0], configkeys) {
4850
viper.Set(args[0], args[1])
4951
err := viper.WriteConfig()
5052
if err != nil {
51-
fmt.Println("ERROR:", err)
52-
return
53+
err = viper.SafeWriteConfig()
54+
if err != nil {
55+
log.Fatal(err)
56+
}
5357
}
5458
}
5559
},

mg/cmd/root.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cmd
1919
import (
2020
"flag"
2121
"fmt"
22-
"os"
2322

2423
"github.com/mitchellh/go-homedir"
2524
"github.com/spf13/cobra"
@@ -33,10 +32,12 @@ var MGBanner string = "mg " + MGVersion
3332

3433
// @todo: This should be moved to it's own package to avoid cyclic dependencies since both cmd and modules package use them.
3534
var (
36-
Namespace string
37-
OName string // Flag for overriding application name.
38-
Config string // Viper config override
39-
Verbose bool = false
35+
Namespace string
36+
OName string // Flag for overriding application name.
37+
ConfigPath string // Viper config override
38+
ConfigFile string = "config"
39+
ConfigFormat string = "yaml"
40+
Verbose bool = false
4041
// Output flag, makes mg output generated kubernetes resources in json or yaml.
4142
Output bool = false
4243
Version string
@@ -77,28 +78,29 @@ datastructure and help you generate kubernetes primitives`,
7778
}
7879

7980
func init() {
80-
RootCmd.PersistentFlags().StringVar(&Config, "config", "", "config file (default is $HOME/.config/mg/mg.yaml)")
81+
RootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "config file (default is $HOME/.config/mg/config.yaml)")
8182
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
8283
cobra.OnInitialize(initConfig)
8384
}
8485

8586
func initConfig() {
86-
viper.SetConfigType("yaml")
8787

8888
home, err := homedir.Dir()
8989
if err != nil {
9090
fmt.Println(err)
91-
os.Exit(1)
91+
log.Fatal(err)
9292
}
9393

94-
if Config != "" {
95-
fmt.Printf("Using configfile: %v %v", os.Stdout, Config)
96-
viper.SetConfigFile(Config)
94+
viper.SetConfigType("yaml")
95+
if len(ConfigPath) > 0 {
96+
viper.SetConfigFile(ConfigPath)
9797
} else {
98+
ConfigPath = home+"/.config/mg/"
9899
//fmt.Println(os.Stderr, "Using default config file: ~/.config/mg/config.yaml")
99-
viper.AddConfigPath(home + "/.config/mg/")
100+
viper.AddConfigPath(ConfigPath)
100101
viper.SetConfigName("config")
101102
}
103+
log.Infof("Using configfile: %v", ConfigPath)
102104

103105
viper.AutomaticEnv()
104106
if err := viper.ReadInConfig(); err != nil {

0 commit comments

Comments
 (0)