Skip to content

Commit

Permalink
Allow extra config file (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydhulia committed Nov 30, 2021
1 parent ca53acd commit 4335648
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 3 additions & 5 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand All @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
)
Expand All @@ -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(&region, "region", "r", viper.GetString("aws.region"), "AWS region")
rootCmd.PersistentFlags().StringVar(&extraConfigFile, "extra-config-file", "", "extra-config-file <yaml_file>")
if err := viper.BindPFlag("log_level", rootCmd.PersistentFlags().Lookup("log-level")); err != nil {
logging.LogError(err, "Error parsing")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package config

import (
"os"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit 4335648

Please sign in to comment.