Skip to content

Commit

Permalink
added better handling for when region is not set in a profile
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoval committed Aug 2, 2021
1 parent e30116e commit 2d36ec1
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 131 deletions.
4 changes: 2 additions & 2 deletions awsconfig/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func loadAwsProfiles() {

func GetAwsCredentialsFilePath() string {
// see https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html
path, _ := os.LookupEnv("AWS_SHARED_CREDENTIALS_FILE")
path := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
if path == "" {
path = config.DefaultSharedCredentialsFilename()
}
Expand All @@ -75,7 +75,7 @@ func GetAwsCredentialsFilePath() string {

func GetAwsProfileFilePath() string {
// see https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html
path, _ := os.LookupEnv("AWS_CONFIG_FILE")
path := os.Getenv("AWS_CONFIG_FILE")
if path == "" {
path = config.DefaultSharedConfigFilename()
}
Expand Down
2 changes: 1 addition & 1 deletion awsworkflow/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var defaultAwsConsoleDomain string = "console.aws.amazon.com"
var defaultAwsConsoleDomainChina string = "console.amazonaws.cn"

func initAWSConsoleDomain(region string) {
awsConsoleDomain, _ := os.LookupEnv("ALRED_AWS_CONSOLE_SERVICES_WORKFLOW_AWS_CONSOLE_DOMAIN")
awsConsoleDomain := os.Getenv("ALRED_AWS_CONSOLE_SERVICES_WORKFLOW_AWS_CONSOLE_DOMAIN")
if awsConsoleDomain == "" {
if strings.HasPrefix(region, "cn-") {
awsConsoleDomain = defaultAwsConsoleDomainChina
Expand Down
8 changes: 6 additions & 2 deletions caching/fetch_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

aw "github.com/deanishe/awgo"
"github.com/rkoval/alfred-aws-console-services-workflow/awsconfig"
"github.com/rkoval/alfred-aws-console-services-workflow/searchers/searchutil"
"github.com/rkoval/alfred-aws-console-services-workflow/util"
)
Expand Down Expand Up @@ -63,16 +64,19 @@ func handleFetchErr(wf *aw.Workflow, lastFetchErrPath string, searchArgs searchu

// TODO need to fix "no results" display when there's really a fetch error

userHomePath := os.Getenv("HOME")
errString := string(data)
wf.Configure(aw.SuppressUIDs(true))
if strings.HasPrefix(errString, "NoCredentialProviders") {
util.NewURLItem(wf, "AWS credentials not set in ~/.aws/credentials for profile \""+searchArgs.Profile+"\"").
credentialsFilePath := strings.Replace(awsconfig.GetAwsCredentialsFilePath(), userHomePath, "~", 1)
util.NewURLItem(wf, "AWS credentials not set in "+credentialsFilePath+" for profile \""+searchArgs.Profile+"\"").
Subtitle("Press enter to open AWS docs on how to configure").
Arg("https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#creating-the-credentials-file").
Icon(aw.IconError).
Valid(true)
} else if strings.HasPrefix(errString, "MissingRegion") {
util.NewURLItem(wf, "AWS region not set in ~/.aws/config for profile \""+searchArgs.Profile+"\"").
configFilePath := strings.Replace(awsconfig.GetAwsProfileFilePath(), userHomePath, "~", 1)
util.NewURLItem(wf, "AWS region not set in "+configFilePath+" for profile \""+searchArgs.Profile+"\"").
Subtitle("Press enter to open AWS docs on how to configure").
Arg("https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#creating-the-config-file").
Icon(aw.IconError).
Expand Down
1 change: 0 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
set -e
export TEST=1
source env.sh
export AWS_REGION=us-west-2
export AWS_SHARED_CREDENTIALS_FILE="../tests/test_aws_credentials_file"
export AWS_CONFIG_FILE="../tests/test_aws_config_file"
./generate.sh
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aws_credentials_file
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
aws_access_key_id=BBBBBBBBBBBBBBBBBBBB
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

[profile4]
[noregion]
aws_access_key_id=BBBBBBBBBBBBBBBBBBBB
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Expand Down
24 changes: 12 additions & 12 deletions workflow/.snapshots/TestRun-$us-east-1_@
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
title: (string) (len=8) "noregion",
subtitle: (*string)((len=76) "⚠️ This profile does not specify a region. Functionality will be limited"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile1"),
autocomplete: (*string)((len=21) "$us-east-1 @profile1 "),
uid: (*string)((len=8) "noregion"),
autocomplete: (*string)((len=21) "$us-east-1 @noregion "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand All @@ -42,11 +42,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile3",
subtitle: (*string)((len=14) "🌎 sa-east-1"),
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile3"),
autocomplete: (*string)((len=21) "$us-east-1 @profile3 "),
uid: (*string)((len=8) "profile1"),
autocomplete: (*string)((len=21) "$us-east-1 @profile1 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand All @@ -63,11 +63,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
title: (string) (len=8) "profile3",
subtitle: (*string)((len=14) "🌎 sa-east-1"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=21) "$us-east-1 @profile4 "),
uid: (*string)((len=8) "profile3"),
autocomplete: (*string)((len=21) "$us-east-1 @profile3 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand Down
23 changes: 1 addition & 22 deletions workflow/.snapshots/TestRun-$us-east-1_@prof
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]*aw.Item) (len=4) {
([]*aw.Item) (len=3) {
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
Expand Down Expand Up @@ -41,27 +41,6 @@
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=21) "$us-east-1 @profile4 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) {
},
mods: (map[aw.ModKey]*aw.Modifier) <nil>,
icon: (*aw.Icon)({
Value: (string) (len=78) "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Accounts.icns",
Type: (aw.IconType) ""
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=12) "bogusprofile",
subtitle: (*string)((len=10) "🌎 bogus"),
Expand Down
24 changes: 12 additions & 12 deletions workflow/.snapshots/TestRun-@
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
title: (string) (len=8) "noregion",
subtitle: (*string)((len=76) "⚠️ This profile does not specify a region. Functionality will be limited"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile1"),
autocomplete: (*string)((len=10) "@profile1 "),
uid: (*string)((len=8) "noregion"),
autocomplete: (*string)((len=10) "@noregion "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand All @@ -42,11 +42,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile3",
subtitle: (*string)((len=14) "🌎 sa-east-1"),
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile3"),
autocomplete: (*string)((len=10) "@profile3 "),
uid: (*string)((len=8) "profile1"),
autocomplete: (*string)((len=10) "@profile1 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand All @@ -63,11 +63,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
title: (string) (len=8) "profile3",
subtitle: (*string)((len=14) "🌎 sa-east-1"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=10) "@profile4 "),
uid: (*string)((len=8) "profile3"),
autocomplete: (*string)((len=10) "@profile3 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand Down
24 changes: 12 additions & 12 deletions workflow/.snapshots/TestRun-@_$
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
title: (string) (len=8) "noregion",
subtitle: (*string)((len=76) "⚠️ This profile does not specify a region. Functionality will be limited"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile1"),
autocomplete: (*string)((len=12) "@profile1 $"),
uid: (*string)((len=8) "noregion"),
autocomplete: (*string)((len=12) "@noregion $"),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand All @@ -42,11 +42,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile3",
subtitle: (*string)((len=14) "🌎 sa-east-1"),
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile3"),
autocomplete: (*string)((len=12) "@profile3 $"),
uid: (*string)((len=8) "profile1"),
autocomplete: (*string)((len=12) "@profile1 $"),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand All @@ -63,11 +63,11 @@
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
title: (string) (len=8) "profile3",
subtitle: (*string)((len=14) "🌎 sa-east-1"),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=12) "@profile4 $"),
uid: (*string)((len=8) "profile3"),
autocomplete: (*string)((len=12) "@profile3 $"),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
Expand Down
23 changes: 1 addition & 22 deletions workflow/.snapshots/TestRun-@_prof
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]*aw.Item) (len=4) {
([]*aw.Item) (len=3) {
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
Expand Down Expand Up @@ -41,27 +41,6 @@
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=6) "@ prof"),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) {
},
mods: (map[aw.ModKey]*aw.Modifier) <nil>,
icon: (*aw.Icon)({
Value: (string) (len=78) "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Accounts.icns",
Type: (aw.IconType) ""
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=12) "bogusprofile",
subtitle: (*string)((len=10) "🌎 bogus"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
([]*aw.Item) (len=1) {
(*aw.Item)({
title: (string) (len=29) "elasticbeanstalk applications",
subtitle: (*string)((len=39) "🔎 Elastic Beanstalk – Applications"),
match: (*string)((len=25) "applications Applications"),
uid: (*string)((len=12) "applications"),
autocomplete: (*string)((len=40) "@noregion elasticbeanstalk applications "),
arg: (*string)((len=66) "https://console.aws.amazon.com/elasticbeanstalk/home#/applications"),
valid: (bool) true,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) (len=1) {
(string) (len=6) "action": (string) (len=8) "open-url"
},
mods: (map[aw.ModKey]*aw.Modifier) (len=1) {
(aw.ModKey) (len=3) "cmd": (*aw.Modifier)({
Key: (aw.ModKey) (len=3) "cmd",
arg: (*string)(<nil>),
subtitle: (*string)((len=21) "Copy URL to clipboard"),
valid: (bool) false,
icon: (*aw.Icon)(<nil>),
vars: (map[string]string) (len=1) {
(string) (len=6) "action": (string) (len=17) "copy-to-clipboard"
}
})
},
icon: (*aw.Icon)({
Value: (string) (len=27) "images/elasticbeanstalk.png",
Type: (aw.IconType) ""
}),
noUID: (bool) false
})
}
23 changes: 1 addition & 22 deletions workflow/.snapshots/TestRun-@prof
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]*aw.Item) (len=4) {
([]*aw.Item) (len=3) {
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
Expand Down Expand Up @@ -41,27 +41,6 @@
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=10) "@profile4 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) {
},
mods: (map[aw.ModKey]*aw.Modifier) <nil>,
icon: (*aw.Icon)({
Value: (string) (len=78) "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Accounts.icns",
Type: (aw.IconType) ""
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=12) "bogusprofile",
subtitle: (*string)((len=10) "🌎 bogus"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]*aw.Item) (len=4) {
([]*aw.Item) (len=3) {
(*aw.Item)({
title: (string) (len=8) "profile1",
subtitle: (*string)((len=14) "🌎 us-east-1"),
Expand Down Expand Up @@ -41,27 +41,6 @@
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=8) "profile4",
subtitle: (*string)(<nil>),
match: (*string)(<nil>),
uid: (*string)((len=8) "profile4"),
autocomplete: (*string)((len=45) "elasticbeanstalk applications App1 @profile4 "),
arg: (*string)(<nil>),
valid: (bool) false,
file: (bool) false,
copytext: (*string)(<nil>),
largetype: (*string)(<nil>),
ql: (*string)(<nil>),
vars: (map[string]string) {
},
mods: (map[aw.ModKey]*aw.Modifier) <nil>,
icon: (*aw.Icon)({
Value: (string) (len=78) "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Accounts.icns",
Type: (aw.IconType) ""
}),
noUID: (bool) false
}),
(*aw.Item)({
title: (string) (len=12) "bogusprofile",
subtitle: (*string)((len=10) "🌎 bogus"),
Expand Down
2 changes: 2 additions & 0 deletions workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func Run(wf *aw.Workflow, rawQuery string, transport http.RoundTripper, forceFet

if profile.Region != "" {
item.Subtitle(fmt.Sprintf("🌎 %s", profile.Region))
} else {
item.Subtitle("⚠️ This profile does not specify a region. Functionality will be limited")
}
}
log.Printf("filtering with profile override %q", *query.ProfileQuery)
Expand Down
Loading

0 comments on commit 2d36ec1

Please sign in to comment.