Skip to content

Commit 7c5f4ab

Browse files
committed
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
1 parent 76e1d42 commit 7c5f4ab

File tree

10 files changed

+49
-9
lines changed

10 files changed

+49
-9
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,20 @@ sudo dpkg -i <the_deb_name>
8383

8484
## Profile and Region Support
8585

86-
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:
86+
By default Saw uses the region and credentials in your default profile. You can override these to your liking using the command line flags:
8787

8888
```sh
89+
# Use personal profile
90+
saw groups --profile personal
91+
92+
# Use us-west-1 region
93+
saw groups --region us-west-1
94+
```
95+
96+
Alternatively you can hard code these in your shell's init scripts (bashrc, zshrc, etc...):
97+
98+
```sh
99+
# Export profile and region that override the default
89100
export AWS_PROFILE='work_profile'
90101
export AWS_REGION='us-west-1'
91102
```

blade/blade.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,33 @@ import (
1616

1717
type Blade struct {
1818
config *config.Configuration
19+
aws *config.AWSConfiguration
1920
output *config.OutputConfiguration
2021
cwl *cloudwatchlogs.CloudWatchLogs
2122
}
2223

23-
func NewBlade(config *config.Configuration, outputConfig *config.OutputConfiguration) *Blade {
24+
func NewBlade(
25+
config *config.Configuration,
26+
awsConfig *config.AWSConfiguration,
27+
outputConfig *config.OutputConfiguration,
28+
) *Blade {
2429
blade := Blade{}
25-
sess := session.Must(session.NewSessionWithOptions(session.Options{
30+
awsCfg := aws.Config{}
31+
32+
if awsConfig.Region != "" {
33+
awsCfg.Region = &awsConfig.Region
34+
}
35+
36+
awsSessionOpts := session.Options{
37+
Config: awsCfg,
2638
SharedConfigState: session.SharedConfigEnable,
27-
}))
39+
}
40+
41+
if awsConfig.Profile != "" {
42+
awsSessionOpts.Profile = awsConfig.Profile
43+
}
44+
45+
sess := session.Must(session.NewSessionWithOptions(awsSessionOpts))
2846

2947
blade.cwl = cloudwatchlogs.New(sess)
3048
blade.config = config

cmd/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var GetCommand = &cobra.Command{
2424
},
2525
Run: func(cmd *cobra.Command, args []string) {
2626
getConfig.Group = args[0]
27-
b := blade.NewBlade(&getConfig, nil)
27+
b := blade.NewBlade(&getConfig, &awsConfig, nil)
2828
if getConfig.Prefix != "" {
2929
streams := b.GetLogStreams()
3030
if len(streams) == 0 {

cmd/groups.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var GroupsCommand = &cobra.Command{
1616
Short: "List log groups",
1717
Long: "",
1818
Run: func(cmd *cobra.Command, args []string) {
19-
b := blade.NewBlade(&groupsConfig, nil)
19+
b := blade.NewBlade(&groupsConfig, &awsConfig, nil)
2020
logGroups := b.GetLogGroups()
2121
for _, group := range logGroups {
2222
fmt.Println(*group.LogGroupName)

cmd/saw.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/TylerBrock/saw/config"
45
"github.com/spf13/cobra"
56
)
67

@@ -17,11 +18,15 @@ var SawCommand = &cobra.Command{
1718
},
1819
}
1920

21+
var awsConfig config.AWSConfiguration
22+
2023
func init() {
2124
SawCommand.AddCommand(GroupsCommand)
2225
SawCommand.AddCommand(StreamsCommand)
2326
SawCommand.AddCommand(VersionCommand)
2427
SawCommand.AddCommand(WatchCommand)
2528
SawCommand.AddCommand(GetCommand)
2629
//Saw.AddCommand(Delete)
30+
SawCommand.PersistentFlags().StringVar(&awsConfig.Region, "region", "", "override profile AWS region")
31+
SawCommand.PersistentFlags().StringVar(&awsConfig.Profile, "profile", "", "override default AWS profile")
2732
}

cmd/streams.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var StreamsCommand = &cobra.Command{
2323
},
2424
Run: func(cmd *cobra.Command, args []string) {
2525
streamsConfig.Group = args[0]
26-
b := blade.NewBlade(&streamsConfig, nil)
26+
b := blade.NewBlade(&streamsConfig, &awsConfig, nil)
2727

2828
logStreams := b.GetLogStreams()
2929
for _, stream := range logStreams {

cmd/watch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var WatchCommand = &cobra.Command{
2525
},
2626
Run: func(cmd *cobra.Command, args []string) {
2727
watchConfig.Group = args[0]
28-
b := blade.NewBlade(&watchConfig, &outputConfig)
28+
b := blade.NewBlade(&watchConfig, &awsConfig, &outputConfig)
2929
if watchConfig.Prefix != "" {
3030
streams := b.GetLogStreams()
3131
if len(streams) == 0 {

config/aws.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package config
2+
3+
type AWSConfiguration struct {
4+
Region string
5+
Profile string
6+
}

config/configuration.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type Configuration struct {
1717
Start string
1818
End string
1919
Filter string
20-
Region string
2120
Streams []*cloudwatchlogs.LogStream
2221
Descending bool
2322
OrderBy string

config/output.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type OutputConfiguration struct {
1010
Raw bool
1111
RawString bool
1212
HideStreamName bool
13+
HideDate bool
1314
Invert bool
1415
NoColor bool
1516
}

0 commit comments

Comments
 (0)