From 982086bb1ee42a8aa1fdc4829e2f5610daa0cc56 Mon Sep 17 00:00:00 2001 From: Manuel Kraus Date: Mon, 7 Aug 2023 11:50:45 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20allow=20directly=20passing=20p?= =?UTF-8?q?rofile=20as=20argument?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .goreleaser.yml | 15 ++++++++------- README.md | 26 ++++++++++++++++++++------ main.go | 30 ++++++++++++++++++++++++++---- scripts/profile-exporter.sh | 2 +- 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 0bf613e..d355437 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -16,13 +16,14 @@ builds: - 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 + - id: binary + name_template: >- + {{- .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end -}} format_overrides: - goos: windows format: zip diff --git a/README.md b/README.md index ac6040c..d6e11dd 100644 --- a/README.md +++ b/README.md @@ -21,24 +21,38 @@ 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. + ```bash + alias awsp="source _awsp" + ``` + This is required so no additional shell has to be created. ## Usage -```shell +Supplying no argument will open the interactive +prompt. + +```bash $ awsp ``` +Alternatively you can also directly pass a +profile to search for as an argument. The profile +that matches the argument the most using the Levinsthein +algorithm will be set. +If no profile is found, the prompt opens. + +```bash +$ awsp client-proj +``` + ## 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. ### Special thanks -The `alias awsp="source _awsp"` idea comes straight from [johnnyopao/awsp](https://github.com/johnnyopao/awsp). It's a very similar tool, however we really wanted fuzzy search :) \ No newline at end of file +The `alias awsp="source _awsp"` idea comes straight from [johnnyopao/awsp](https://github.com/johnnyopao/awsp). It's a very similar tool, however we really wanted fuzzy search :) diff --git a/main.go b/main.go index a785c3f..1a32d0d 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,14 @@ package main import ( "log" + "os" "github.com/lithammer/fuzzysearch/fuzzy" "github.com/manifoldco/promptui" ) -var version string = "v1.0.9" +var version string = "v1.1.0" func main() { profiles, err := parseProfiles() @@ -18,16 +19,37 @@ func main() { return } - templates := &promptui.SelectTemplates{ - Active: `{{ "> " | cyan | bold }}{{ . | cyan | bold }}`, - Inactive: ` {{ . }}`, + /** + * In case the user already provided a profile + * to search for in the command line arguments, + * try to set it without using the prompt. + */ + if len(os.Args) > 1 { + argument := os.Args[1] + + matchingProfiles := fuzzy.RankFind(argument, profiles) + + if matchingProfiles.Len() > 0 { + setProfile(matchingProfiles[0].Target) + return + } } + /** + * In case no profile could be found, + * or no argument has been provided, + * open the prompt. + */ searcher := func(input string, index int) bool { item := profiles[index] return fuzzy.MatchFold(input, item) } + templates := &promptui.SelectTemplates{ + Active: `{{ "> " | cyan | bold }}{{ . | cyan | bold }}`, + Inactive: ` {{ . }}`, + } + prompt := promptui.Select{ Label: "Select AWS Profile", Items: profiles, diff --git a/scripts/profile-exporter.sh b/scripts/profile-exporter.sh index 29dd42f..4054c2d 100755 --- a/scripts/profile-exporter.sh +++ b/scripts/profile-exporter.sh @@ -1,6 +1,6 @@ #!/bin/sh -aws-profile-picker +aws-profile-picker $@ selected_profile="$(cat $HOME/.aws-profile-picker)"