Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlkrs committed May 31, 2021
0 parents commit fe3f1d1
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: build

on:
push:
branches: [ main ]
tags: [ v* ]
pull_request: {}

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go ${{ matrix.node-version }}
uses: actions/setup-go@v2
with:
go-version: 1.16.x

- name: Cache Go modules
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run Setup
run: go mod tidy

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
if: startsWith(github.ref, 'refs/tags/')
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
43 changes: 43 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
env:
- GO111MODULE=on
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- 386
- amd64
- arm
- arm64
archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
format_overrides:
- goos: windows
format: zip
files:
- README.md
- LICENSE
- scripts/*
checksum:
name_template: '{{ .ProjectName }}_checksums.txt'
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- Merge pull request
- Merge branch
- go mod tidy
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 TechTheory Manuel Kraus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/techtheoryio/aws-profile-picker?color=gree">
<a href="https://goreportcard.com/report/github.com/techtheoryio/aws-profile-picker"><img src="https://goreportcard.com/badge/github.com/techtheoryio/aws-profile-picker" alt="Version"></a>
<a href="#"><img src="https://img.shields.io/github/go-mod/go-version/techtheoryio/aws-profile-picker" alt="Version"></a>

<p align="center">
<img width="600" src="./images/example.svg">
</p>


# aws-profile-picker

`aws-profile-picker` is a simple CLI build to assist switching AWS profiles. It includes:

- Dropdown list of all configured AWS profiles in `$HOME/.aws/config`
- Fuzzy search for all profiles
- No additional shell is being created - the environment variable `AWS_PROFILE` is set in the current shell

## Installation

1. Download [the latest release](https://github.com/techtheoryio/aws-profile-picker/releases).
2. Unpack the archive
3. `cd scripts`
4. `sh ./install.sh`
5. Add an alias to your shell configuration (.bshrc, .zshrc, etc.)
```bash
alias awsp="source _awsp"
```
This is required so no additional shell has to be created.

## Usage

```shell
$ awsp
```

## Uninstall

In the release archive, there's an uninstall script
1. `cd scripts`
2. `sh ./uninstall.sh`
3. Remove the alias from your shell configuration file.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/techtheoryio/aws-profile-picker

go 1.16

require (
github.com/lithammer/fuzzysearch v1.1.2
github.com/manifoldco/promptui v0.8.0
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lithammer/fuzzysearch v1.1.2 h1:ePUtm14xKxbpCxozcFbIDRtvANxnVnE+RKpJUqkr2gA=
github.com/lithammer/fuzzysearch v1.1.2/go.mod h1:v6tYW/9kpfV6LNcweXdSjQsfCku/1M/oObmSox1fzP8=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo=
github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec h1:DGmKwyZwEB8dI7tbLt/I/gQuP559o/0FrAkHKlQM/Ks=
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec/go.mod h1:owBmyHYMLkxyrugmfwE/DLJyW8Ro9mkphwuVErQ0iUw=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1 change: 1 addition & 0 deletions images/example.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"log"

"github.com/lithammer/fuzzysearch/fuzzy"

"github.com/manifoldco/promptui"
)

var version string = "v1.0.5"

func main() {
profiles, err := parseProfiles()

if err != nil {
log.Fatal((err))
return
}

templates := &promptui.SelectTemplates{
Active: `{{ "> " | cyan | bold }}{{ . | cyan | bold }}`,
Inactive: ` {{ . }}`,
}

searcher := func(input string, index int) bool {
item := profiles[index]
return fuzzy.MatchFold(input, item)
}

prompt := promptui.Select{
Label: "Select AWS Profile",
Items: profiles,
Size: 10,
StartInSearchMode: true,
Templates: templates,
Searcher: searcher,
}

_, result, err := prompt.Run()

if err != nil {
log.Fatal(err)
return
}

setProfile(result)
}
34 changes: 34 additions & 0 deletions parse_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"log"
"os"
"sort"
"strings"

"github.com/vaughan0/go-ini"
)

func parseProfiles() ([]string, error) {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

configFile, err := ini.LoadFile(fmt.Sprintf("%s/.aws/config", dirname))

if err != nil {
fmt.Println("❌ No or malformatted AWS configuration found")
return nil, err
}

var profileNames = []string{}
for k := range configFile {
profileNames = append(profileNames, strings.Replace(k, "profile ", "", 1))
}

sort.Strings(profileNames)

return profileNames, nil
}
6 changes: 6 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

cp ../aws-profile-picker /usr/local/bin/aws-profile-picker
cp ./profile-exporter.sh /usr/local/bin/_awsp
chmod +x /usr/local/bin/aws-profile-picker
chmod +x /usr/local/bin/_awsp
12 changes: 12 additions & 0 deletions scripts/profile-exporter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

aws-profile-picker

selected_profile="$(cat $HOME/.aws-profile-picker)"

if [ -z "$selected_profile" ]
then
unset AWS_PROFILE
else
export AWS_PROFILE="$selected_profile"
fi
2 changes: 2 additions & 0 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rm /usr/local/bin/aws-profile-picker
rm /usr/local/bin/awsp
16 changes: 16 additions & 0 deletions set_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"log"
"os"
)

func setProfile(profile string) {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

os.WriteFile(fmt.Sprintf("%s/.aws-profile-picker", dirname), []byte(profile), 0644)
}

0 comments on commit fe3f1d1

Please sign in to comment.