Skip to content

Commit

Permalink
Merge pull request #10 from slankdev/slankdev-default-config
Browse files Browse the repository at this point in the history
Support default config
  • Loading branch information
slankdev authored Jan 31, 2020
2 parents ca1a920 + 1e986ac commit f7e5304
Showing 1 changed file with 20 additions and 44 deletions.
64 changes: 20 additions & 44 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/viper"
)

var defaultCfgFile string = "spec.yaml"
var cfgFile string
var tnconfig shell.Tn
var verbose bool
Expand Down Expand Up @@ -43,20 +44,20 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

checkCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
confCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
downCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
execCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
initCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
imgCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
printCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
psCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
pullCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
reconfCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
reupCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
testCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
upCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
upconfCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
checkCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
confCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
downCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
execCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
initCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
imgCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
printCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
psCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
pullCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
reconfCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
reupCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
testCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
upCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")
upconfCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "config file (default is ./spec.yaml)")

// verbose option
confCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose (default: false)")
Expand All @@ -75,36 +76,13 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
if !utils.Exists(cfgFile) {
// confirmMessage := fmt.Sprintf("Do you create %s file", cfgFile)
// isConfirmed := utils.Ask4confirm(confirmMessage)
// if isConfirmed {
// file, err := os.OpenFile(cfgFile, os.O_RDWR|os.O_CREATE, 0644)
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if err := file.Close(); err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// } else {
err := fmt.Errorf("%s is not Found", cfgFile)
fmt.Println(err)
os.Exit(1)
// }
}

viper.SetConfigFile(cfgFile)

} else {
// viper.AddConfigPath(".")
// viper.SetConfigName("spec")
return
if !utils.Exists(cfgFile) {
err := fmt.Errorf("%s is not Found", cfgFile)
fmt.Println(err)
os.Exit(1)
}

viper.SetConfigFile(cfgFile)
viper.SetConfigType("yaml")

if err := viper.ReadInConfig(); err != nil {
Expand All @@ -114,7 +92,5 @@ func initConfig() {

if err := viper.Unmarshal(&tnconfig); err != nil {
panic(err)
// fmt.Println(err)
// os.Exit(1)
}
}

0 comments on commit f7e5304

Please sign in to comment.