diff --git a/CHANGELOG.md b/CHANGELOG.md index e550413..b578d22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## v0.0.5 (October 6, 2023) * Added support for passing arbitrary profile names as arguments. [#4] thanks @withakay +* Added `awsd list` command to simply list all profiles. ## v0.0.4 (October 4, 2023) * Don't append default profile if it already exists. diff --git a/README.md b/README.md index 9b0dd13..a493cce 100644 --- a/README.md +++ b/README.md @@ -69,4 +69,21 @@ function aws_prof { PROMPT='OTHER_PROMPT_STUFF $(aws_info)' ``` +## Add autocompletion +You can add autocompletion when passing profile as argument by creating a script with the following. I put it in +`~/bin/awsd_autocompltete.sh`, then source that script and add to your bash profile or zshrc file. +`source ~/bin/awsd_autocompltete.sh` + +```bash +_awsd_completion() { + local cur=${COMP_WORDS[COMP_CWORD]} + local suggestions=$(awsd list) + COMPREPLY=($(compgen -W "$suggestions" -- $cur)) + return 0 +} +complete -F _awsd_completion awsd +``` + +Now you can do `awsd my-p` and hit tab and if you had a profile `my-profile` it would autocomplete and find it. + Inspired by https://github.com/johnnyopao/awsp diff --git a/main.go b/main.go index 68e2130..0cbfa85 100644 --- a/main.go +++ b/main.go @@ -31,11 +31,23 @@ func main() { // we will assume it is the desired profile value and try to set it. // If not argument is passed we will prompt the user to select a profile. var desiredProfile = "" + home := os.Getenv("HOME") + profileFileLocation := getenv("AWS_CONFIG_FILE", fmt.Sprintf("%s/.aws/config", home)) + profiles := getProfiles(profileFileLocation) + err := touchFile(fmt.Sprintf("%s/.awsd", home)) + if err != nil { + log.Fatal(err) + } if len(os.Args) > 1 { if os.Args[1] == "version" { fmt.Println("awsd version", version) os.Exit(0) + } else if os.Args[1] == "list" { + for _, p := range profiles { + fmt.Println(p) + } + os.Exit(0) } else { // if there is an argument, and it is not "version" // assume it is the desired profile value @@ -43,14 +55,6 @@ func main() { } } - home := os.Getenv("HOME") - profileFileLocation := getenv("AWS_CONFIG_FILE", fmt.Sprintf("%s/.aws/config", home)) - profiles := getProfiles(profileFileLocation) - err := touchFile(fmt.Sprintf("%s/.awsd", home)) - if err != nil { - log.Fatal(err) - } - if desiredProfile != "" { //check if desired profile exists for _, profile := range profiles { @@ -119,7 +123,7 @@ func writeFile(profile, loc string) { } } -func getProfiles(profileFileLocation string) []string { + func getProfiles(profileFileLocation string) []string { profiles := make([]string, 0) file, err := os.Open(profileFileLocation)