From 7c5f4ab1f6c469753783176753bafb02e0831f25 Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Thu, 21 Jun 2018 22:22:21 -0700 Subject: [PATCH] Add proper region and profile override support Adds ability to override the default profile and the region set within the selected profile via command line flags. Closes #1 --- README.md | 13 ++++++++++++- blade/blade.go | 24 +++++++++++++++++++++--- cmd/get.go | 2 +- cmd/groups.go | 2 +- cmd/saw.go | 5 +++++ cmd/streams.go | 2 +- cmd/watch.go | 2 +- config/aws.go | 6 ++++++ config/configuration.go | 1 - config/output.go | 1 + 10 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 config/aws.go diff --git a/README.md b/README.md index e7a555e..5af21b3 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,20 @@ sudo dpkg -i ## Profile and Region Support -By default Saw uses the region and credentials in your default profile. We are working on adding support for easily switching these via a CLI flag. For now, to switch region or profile: +By default Saw uses the region and credentials in your default profile. You can override these to your liking using the command line flags: ```sh +# Use personal profile +saw groups --profile personal + +# Use us-west-1 region +saw groups --region us-west-1 +``` + +Alternatively you can hard code these in your shell's init scripts (bashrc, zshrc, etc...): + +```sh +# Export profile and region that override the default export AWS_PROFILE='work_profile' export AWS_REGION='us-west-1' ``` diff --git a/blade/blade.go b/blade/blade.go index 9a95a58..b346bce 100644 --- a/blade/blade.go +++ b/blade/blade.go @@ -16,15 +16,33 @@ import ( type Blade struct { config *config.Configuration + aws *config.AWSConfiguration output *config.OutputConfiguration cwl *cloudwatchlogs.CloudWatchLogs } -func NewBlade(config *config.Configuration, outputConfig *config.OutputConfiguration) *Blade { +func NewBlade( + config *config.Configuration, + awsConfig *config.AWSConfiguration, + outputConfig *config.OutputConfiguration, +) *Blade { blade := Blade{} - sess := session.Must(session.NewSessionWithOptions(session.Options{ + awsCfg := aws.Config{} + + if awsConfig.Region != "" { + awsCfg.Region = &awsConfig.Region + } + + awsSessionOpts := session.Options{ + Config: awsCfg, SharedConfigState: session.SharedConfigEnable, - })) + } + + if awsConfig.Profile != "" { + awsSessionOpts.Profile = awsConfig.Profile + } + + sess := session.Must(session.NewSessionWithOptions(awsSessionOpts)) blade.cwl = cloudwatchlogs.New(sess) blade.config = config diff --git a/cmd/get.go b/cmd/get.go index abe0e7f..e2d81e4 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -24,7 +24,7 @@ var GetCommand = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { getConfig.Group = args[0] - b := blade.NewBlade(&getConfig, nil) + b := blade.NewBlade(&getConfig, &awsConfig, nil) if getConfig.Prefix != "" { streams := b.GetLogStreams() if len(streams) == 0 { diff --git a/cmd/groups.go b/cmd/groups.go index e96c1b9..79b8456 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -16,7 +16,7 @@ var GroupsCommand = &cobra.Command{ Short: "List log groups", Long: "", Run: func(cmd *cobra.Command, args []string) { - b := blade.NewBlade(&groupsConfig, nil) + b := blade.NewBlade(&groupsConfig, &awsConfig, nil) logGroups := b.GetLogGroups() for _, group := range logGroups { fmt.Println(*group.LogGroupName) diff --git a/cmd/saw.go b/cmd/saw.go index b2a2e7f..29142bc 100644 --- a/cmd/saw.go +++ b/cmd/saw.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/TylerBrock/saw/config" "github.com/spf13/cobra" ) @@ -17,6 +18,8 @@ var SawCommand = &cobra.Command{ }, } +var awsConfig config.AWSConfiguration + func init() { SawCommand.AddCommand(GroupsCommand) SawCommand.AddCommand(StreamsCommand) @@ -24,4 +27,6 @@ func init() { SawCommand.AddCommand(WatchCommand) SawCommand.AddCommand(GetCommand) //Saw.AddCommand(Delete) + SawCommand.PersistentFlags().StringVar(&awsConfig.Region, "region", "", "override profile AWS region") + SawCommand.PersistentFlags().StringVar(&awsConfig.Profile, "profile", "", "override default AWS profile") } diff --git a/cmd/streams.go b/cmd/streams.go index e44c20d..8f3fd5c 100644 --- a/cmd/streams.go +++ b/cmd/streams.go @@ -23,7 +23,7 @@ var StreamsCommand = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { streamsConfig.Group = args[0] - b := blade.NewBlade(&streamsConfig, nil) + b := blade.NewBlade(&streamsConfig, &awsConfig, nil) logStreams := b.GetLogStreams() for _, stream := range logStreams { diff --git a/cmd/watch.go b/cmd/watch.go index 1af79ed..7a674d0 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -25,7 +25,7 @@ var WatchCommand = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { watchConfig.Group = args[0] - b := blade.NewBlade(&watchConfig, &outputConfig) + b := blade.NewBlade(&watchConfig, &awsConfig, &outputConfig) if watchConfig.Prefix != "" { streams := b.GetLogStreams() if len(streams) == 0 { diff --git a/config/aws.go b/config/aws.go new file mode 100644 index 0000000..8895341 --- /dev/null +++ b/config/aws.go @@ -0,0 +1,6 @@ +package config + +type AWSConfiguration struct { + Region string + Profile string +} diff --git a/config/configuration.go b/config/configuration.go index 7325df7..5291231 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -17,7 +17,6 @@ type Configuration struct { Start string End string Filter string - Region string Streams []*cloudwatchlogs.LogStream Descending bool OrderBy string diff --git a/config/output.go b/config/output.go index 60748af..1c23207 100644 --- a/config/output.go +++ b/config/output.go @@ -10,6 +10,7 @@ type OutputConfiguration struct { Raw bool RawString bool HideStreamName bool + HideDate bool Invert bool NoColor bool }