Skip to content

Commit

Permalink
Improve handling of config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrildiagne committed Oct 13, 2019
1 parent 4be9aa2 commit a98ea3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
5 changes: 5 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ func Delete() error {
command := []string{"kuda_delete"}
// Run.
err := RunDockerWithEnvs(docker.CommandOption{Image: image, Command: command})

// Delete config file.
config := viper.GetString("config")
os.Remove(config)

return err
}
27 changes: 12 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,20 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kuda.yaml)")
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", home+"/.kuda.yaml",
"Configuration file.")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

viper.AddConfigPath(home)
viper.SetConfigName(".kuda")
}
// Use config file from the flag.
viper.SetConfigFile(cfgFile)

viper.SetEnvPrefix("kuda")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand All @@ -79,5 +74,7 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
fmt.Println(err)
}
}
14 changes: 7 additions & 7 deletions cmd/setup-gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ import (
"github.com/spf13/viper"
)

var provider = "gcp"
var providerVersion = "1.2.0"

// gcpCmd represents the `setup gcp` command
var gcpCmd = &cobra.Command{
Use: "gcp",
Short: "Setup Kuda on GCP.",
Long: "Setup Kuda on GCP.",
Run: func(cmd *cobra.Command, args []string) {
if err := setup(); err != nil {
viper.BindPFlags(cmd.Flags())
viper.BindPFlags(cmd.PersistentFlags())
if err := setupGCP(); err != nil {
fmt.Println("ERROR:", err)
}
},
Expand All @@ -49,18 +48,19 @@ func init() {

gcpCmd.PersistentFlags().StringP("gcp_credentials", "c", "", "Path to GCP credentials JSON")
gcpCmd.MarkPersistentFlagRequired("gcp_credentials")
viper.BindPFlags(gcpCmd.PersistentFlags())

gcpCmd.Flags().String("gcp_cluster_name", "kuda", "Name of the cluster.")
gcpCmd.Flags().String("gcp_compute_zone", "us-central1-a", "Compute Zone for the cluster.")
gcpCmd.Flags().String("gcp_machine_type", "n1-standard-4", "Machine type.")
gcpCmd.Flags().Int("gcp_pool_num_nodes", 1, "Default number of nodes on the system pool. ")
gcpCmd.Flags().String("gcp_gpu", "k80", "Default GPU to use")
gcpCmd.Flags().Bool("gcp_use_preemptible", false, "Wether or not to use pre-emptible instances")
viper.BindPFlags(gcpCmd.Flags())
}

func setup() error {
func setupGCP() error {
const provider = "gcp"
const providerVersion = "1.2.0"

// Set provider config.
viper.Set("provider", provider)

Expand Down

0 comments on commit a98ea3a

Please sign in to comment.