From 43356488c8011e324f2bb560148a21dda3e68be7 Mon Sep 17 00:00:00 2001 From: Jay Dhulia <46459060+jaydhulia@users.noreply.github.com> Date: Mon, 29 Nov 2021 17:40:06 -0700 Subject: [PATCH] Allow extra config file (#105) --- cmd/list.go | 8 +++----- cmd/root.go | 10 +++++++++- cmd/vars.go | 2 +- pkg/config/config.go | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index c0d3ae0..32d62e5 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -17,10 +17,11 @@ package cmd import ( - "fmt" "os" "strings" + "github.com/spf13/viper" + "github.com/netflix/weep/pkg/logging" "github.com/netflix/weep/pkg/creds" @@ -35,7 +36,6 @@ func init() { listCmd.PersistentFlags().BoolVar(&showAll, "all", true, "show user profiles as well as instance profiles") listCmd.PersistentFlags().BoolVar(&showInstanceProfilesOnly, "instance", false, "show only instance roles") listCmd.PersistentFlags().BoolVar(&showConfiguredProfilesOnly, "profiles", false, "show only configured roles") - listCmd.PersistentFlags().StringToStringVar(&awsProfiles, "aws-profiles", nil, "") rootCmd.AddCommand(listCmd) } @@ -58,10 +58,8 @@ func roleList() (string, error) { var rolesData [][]string awsProfilesARNs := make(map[string]bool) + awsProfiles := viper.GetStringMapString("aws-profiles") if showConfiguredProfilesOnly { - if awsProfiles == nil { - return "", fmt.Errorf("cannot filter by aws-profiles, as no aws-profiles were found") - } for _, arn := range awsProfiles { awsProfilesARNs[arn] = true } diff --git a/cmd/root.go b/cmd/root.go index a4d560b..bc30c8a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,13 +36,20 @@ var ( Long: "Weep is a CLI tool that manages AWS access via ConsoleMe for local development.", DisableAutoGenTag: true, SilenceUsage: true, - PersistentPreRun: func(cmd *cobra.Command, args []string) { + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // This runs before any subcommand, and cmd.CalledAs() returns the subcommand // that was called. We want to use this for the weep method in the instance info. metadata.SetWeepMethod(cmd.CalledAs()) // Add basic metadata to ALL future logs metadata.AddMetadataToLogger(args) logging.Log.Infoln("Incoming weep command") + if extraConfigFile != "" { + err := config.MergeExtraConfigFile(extraConfigFile) + if err != nil { + return err + } + } + return nil }, } ) @@ -58,6 +65,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&logFile, "log-file", viper.GetString("log_file"), "log file path") rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "log level (debug, info, warn)") rootCmd.PersistentFlags().StringVarP(®ion, "region", "r", viper.GetString("aws.region"), "AWS region") + rootCmd.PersistentFlags().StringVar(&extraConfigFile, "extra-config-file", "", "extra-config-file ") if err := viper.BindPFlag("log_level", rootCmd.PersistentFlags().Lookup("log-level")); err != nil { logging.LogError(err, "Error parsing") } diff --git a/cmd/vars.go b/cmd/vars.go index 5dea05e..82ee59b 100644 --- a/cmd/vars.go +++ b/cmd/vars.go @@ -22,12 +22,12 @@ var ( accountFilter string assumeRole []string autoRefresh bool - awsProfiles map[string]string cfgFile string destination string destinationConfig string done chan int extendedInfo bool + extraConfigFile string force bool generate bool infoDecode bool diff --git a/pkg/config/config.go b/pkg/config/config.go index 9798153..758b209 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,6 +17,7 @@ package config import ( + "os" "path" "path/filepath" "runtime" @@ -171,6 +172,20 @@ func BaseWebURL() string { return viper.GetString("consoleme_url") } +func MergeExtraConfigFile(extraConfigFile string) error { + f, err := os.Open(extraConfigFile) + if err != nil { + return err + } + defer f.Close() + + if err = viper.MergeConfig(f); err != nil { + return errors.Wrap(err, "could not merge extra config") + } + + return nil +} + var ( Config WeepConfig )