Skip to content

Commit

Permalink
add awsd list
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv committed Oct 6, 2023
1 parent daf0116 commit 2672cc3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,30 @@ 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
desiredProfile = os.Args[1]
}
}

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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2672cc3

Please sign in to comment.