Skip to content

Commit

Permalink
Add proper region and profile override support
Browse files Browse the repository at this point in the history
Adds ability to override the default profile and the region
set within the selected profile via command line flags.

Closes #1
  • Loading branch information
TylerBrock committed Jun 22, 2018
1 parent 76e1d42 commit 7c5f4ab
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,20 @@ sudo dpkg -i <the_deb_name>

## 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'
```
Expand Down
24 changes: 21 additions & 3 deletions blade/blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cmd/saw.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/TylerBrock/saw/config"
"github.com/spf13/cobra"
)

Expand All @@ -17,11 +18,15 @@ var SawCommand = &cobra.Command{
},
}

var awsConfig config.AWSConfiguration

func init() {
SawCommand.AddCommand(GroupsCommand)
SawCommand.AddCommand(StreamsCommand)
SawCommand.AddCommand(VersionCommand)
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")
}
2 changes: 1 addition & 1 deletion cmd/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions config/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

type AWSConfiguration struct {
Region string
Profile string
}
1 change: 0 additions & 1 deletion config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Configuration struct {
Start string
End string
Filter string
Region string
Streams []*cloudwatchlogs.LogStream
Descending bool
OrderBy string
Expand Down
1 change: 1 addition & 0 deletions config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type OutputConfiguration struct {
Raw bool
RawString bool
HideStreamName bool
HideDate bool
Invert bool
NoColor bool
}
Expand Down

0 comments on commit 7c5f4ab

Please sign in to comment.