From 7a1251853b19742212967307b7916140851181c7 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Tue, 23 Oct 2018 23:32:28 -0700 Subject: [PATCH] Adding ability to use a config file in order to reduce command length. --- CHANGELOG | 4 +++- cmd/root.go | 67 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 55c51b4..4d8d941 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,4 +3,6 @@ * Backup and snapshot commands have now been moved to under the force subcommand as these are expensive operations and the subcommand should reflect that. * Cobra library upgrade. -* Added version command. \ No newline at end of file +* Added version command. +* Added ability to use config config file. By default this file will be located in /etc/aurora/australis.yml +* Changed insecureSkipVerify to skipCertVerification. \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index dfe775d..fbb2fa6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,37 +1,44 @@ package cmd import ( - "fmt" - "os" - "time" + "fmt" + "github.com/spf13/viper" + "os" + "strings" + "time" - "github.com/paypal/gorealis" - "github.com/spf13/cobra" + "github.com/paypal/gorealis" + "github.com/spf13/cobra" ) var username, password, zkAddr, schedAddr string var env, role, name = new(string), new(string), new(string) var client realis.Realis var monitor *realis.Monitor -var insecureSkipVerify bool +var skipCertVerification bool var caCertsPath string var clientKey, clientCert string +var configFile string const australisVer = "v0.0.5" var monitorInterval, monitorTimeout int func init() { + + rootCmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`) - rootCmd.PersistentFlags().StringVarP(&zkAddr, "zookeeper", "z", "", "Zookeeper node(s) where Aurora stores information.") + rootCmd.PersistentFlags().StringVarP(&zkAddr, "zookeeper", "z", "", "Zookeeper node(s) where Aurora stores information. (comma separated list)") rootCmd.PersistentFlags().StringVarP(&username, "username", "u", "", "Username to use for API authentication") rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Password to use for API authentication") rootCmd.PersistentFlags().StringVarP(&schedAddr, "scheduler_addr", "s", "", "Aurora Scheduler's address.") rootCmd.PersistentFlags().StringVarP(&clientKey, "clientKey", "k", "", "Client key to use to connect to Aurora.") rootCmd.PersistentFlags().StringVarP(&clientCert, "clientCert", "c", "", "Client certificate to use to connect to Aurora.") - rootCmd.PersistentFlags().StringVarP(&caCertsPath, "caCertsPath", "a", "", "CA certificates path to use.") - rootCmd.PersistentFlags().BoolVarP(&insecureSkipVerify, "insecureSkipVerify", "i", false, "Skip verification.") + rootCmd.PersistentFlags().StringVarP(&caCertsPath, "caCertsPath", "a", "", "Path where CA certificates can be found.") + rootCmd.PersistentFlags().BoolVarP(&skipCertVerification, "skipCertVerification", "i", false, "Skip CA certificate hostname verification.") + rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/aurora/australis.yml", "Config file to use.") + } var rootCmd = &cobra.Command{ @@ -51,8 +58,42 @@ func Execute() { } func connect(cmd *cobra.Command, args []string) { + var err error - var err error + zkAddrSlice := strings.Split(zkAddr, ",") + + viper.SetConfigFile(configFile) + err = viper.ReadInConfig() + if err == nil { + // Best effort load configuration. Will only set config values when flags have not set them already. + if viper.IsSet("zk") && len(zkAddrSlice) == 1 && zkAddrSlice[0] == "" { + zkAddrSlice = viper.GetStringSlice("zk") + } + + if viper.IsSet("username") && username == "" { + username = viper.GetString("username") + } + + if viper.IsSet("password") && password == "" { + password = viper.GetString("password") + } + + if viper.IsSet("clientKey") && clientKey == "" { + clientKey = viper.GetString("clientKey") + } + + if viper.IsSet("clientCert") && clientCert == "" { + clientCert = viper.GetString("clientCert") + } + + if viper.IsSet("caCertsPath") && caCertsPath == "" { + caCertsPath = viper.GetString("caCertsPath") + } + + if viper.IsSet("skipCertVerification") && !skipCertVerification { + skipCertVerification = viper.GetBool("skipCertVerification") + } + } realisOptions := []realis.ClientOption{realis.BasicAuth(username, password), realis.ThriftJSON(), @@ -65,9 +106,9 @@ func connect(cmd *cobra.Command, args []string) { })} // Prefer zookeeper if both ways of connecting are provided - if zkAddr != "" { + if len(zkAddrSlice) > 0 && zkAddrSlice[0] != "" { // Configure Zookeeper to connect - zkOptions := []realis.ZKOpt{ realis.ZKEndpoints(zkAddr), realis.ZKPath("/aurora/scheduler")} + zkOptions := []realis.ZKOpt{ realis.ZKEndpoints(zkAddrSlice...), realis.ZKPath("/aurora/scheduler")} realisOptions = append(realisOptions, realis.ZookeeperOptions(zkOptions...)) } else if schedAddr != "" { realisOptions = append(realisOptions, realis.SchedulerUrl(schedAddr)) @@ -81,7 +122,7 @@ func connect(cmd *cobra.Command, args []string) { realisOptions = append(realisOptions, realis.Certspath(caCertsPath), realis.ClientCerts(clientKey, clientCert), - realis.InsecureSkipVerify(insecureSkipVerify)) + realis.InsecureSkipVerify(skipCertVerification)) } // Connect to Aurora Scheduler and create a client object