From 47b6a6077aa38d2bd6e41901ae5054dd1c87e806 Mon Sep 17 00:00:00 2001 From: belitre <3584328+belitre@users.noreply.github.com> Date: Thu, 13 Jun 2024 01:02:10 +0200 Subject: [PATCH 1/3] feat: added sort flag and updated documentation --- Makefile | 2 +- README.md | 131 +++++++++++++++++++++++++++++++++--- cmd/canu/install/install.go | 4 +- cmd/canu/main.go | 1 + pkg/canu/canu.go | 4 +- pkg/config/config.go | 1 + 6 files changed, 131 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 0665751..13cba54 100644 --- a/Makefile +++ b/Makefile @@ -112,7 +112,7 @@ build-cross: clean-dist go-cache darwin-arm64 darwin-amd64 linux-amd64 dist: build-cross ( \ cd $(DIST_DIR) && \ - $(TARGET_DIRS) tar -zcf $(APP_NAME)-${VERSION}-{}.tar.gz -C {} . \; \ + $(TARGET_DIRS) tar -zcf $(APP_NAME)-{}.tar.gz -C {} . \; \ ) # Semantic Release diff --git a/README.md b/README.md index 4b684a9..06f9694 100644 --- a/README.md +++ b/README.md @@ -2,24 +2,139 @@ CLI to switch aws profiles +I do this for fun on my free time, so if you have any issues or suggestions, please add an issue to the repository, and if I have time I'll try to do it, but keep in mind I can be months without checking the repository again! So feel free to fork the repository if you want and do all the changes you want/need! + - [What does canu mean?](#what-does-canu-mean) -- [How to use canu? \[WIP\]](#how-to-use-canu-wip) +- [How to use canu?](#how-to-use-canu) + - [Installation](#installation) + - [Custom installation](#custom-installation) + - [Examples](#examples) + - [Uninstall](#uninstall) ## What does canu mean? The name is a tribute to a friend. It's probably not the best name for an `aws profile switcher` but... well, it means something to me 😊 -I do this for fun and on my free time, so if you have any issues or suggestions, please add an issue to the repository, and if I have time I'll try to do it, but keep in mind I can be months without checking the repository again! So feel free to fork the repository if you want and do all the changes you want/need! - -## How to use canu? [WIP] +## How to use canu? The first thing you need to know is: **don't use `go install github.com/belitre/canu`, that would add `canu` as an executable to your `$GOPATH/bin` with name `canu`, and we don't want that!** +Also, `canu` will only read profiles from the aws profiles config file that match the pattern `[profile profile-name]` with `sso_start_url`, `sso_account_id` and `sso_role_name` defined. That means it will not show the `default` profile, or any other profile that is not using SSO. For example, if your config file looks like this: +``` +[default] +region = us-east-1 + +[profile test] +region = us-east-2 + +[profile admin] +sso_start_url = https://d-93456h53g77.awsapps.com/start +sso_region = us-east-1 +sso_account_id = 987654321 +sso_role_name = MyRoleName +region = us-east-1 +``` +`canu` will only show the profile `admin` in the menu. + +### Installation + * Download the binary for your OS/ARCH from the release page and unpack it in a folder available in your `$PATH` (I'll use for the example `$GOPATH/bin`): ``` - curl -sL https://github.com/belitre/canu/releases/download/1.0.0/canu-1.0.0-darwin-arm64.tar.gz | tar -zxf - -C $GOPATH/bin - + curl -sL https://github.com/belitre/canu/releases/download/1.1.0/canu-darwin-arm64.tar.gz | tar -zxf - -C $GOPATH/bin ``` -* Run `_canu install` (more details about flags incoming!) + I recommend to download the `canu` binary in a folder where your user has permissions to write. Of course, as a user, you can download the binary to a folder that requires root permissions, but then you will need to run the `install` command with root permissions, and, unless you are using `sudo` which will keep the same value for your `$HOME` env var, you may need to set the path where you want `canu` to write the alias, to be sure it writes the alias to the user shell config file, not the root one. More information about this in the next section. +* Run `_canu install`. Running this command, `canu` will install with the default settings. That means it will use as default config path for your aws config: `$HOME/.aws/config`, and it will use `$HOME/.bash_profile` to add the alias. If you have different settings, please check the next section to learn how to customise your `canu` installation. * Restart your shell, or run the alias returned in the `_canu install` output. -* Run `canu` and enjoy! \ No newline at end of file +* Run `canu` and enjoy! + +What happens during the installation is: +* `canu` will create a shell script in the same folder the binary is downloaded with the name of the alias (by default this name is `canu`, but you can set a different name, check the next section to learn how). +* `canu` will add an alias to your `$HOME/.bash_profile` that will source the shell script, this is how `canu` sets the selected profile in the `AWS_PROFILE` environment variable. + +#### Custom installation + +Once you downloaded the binary, you can check the available flags running `_canu install -h`: + +``` +installs canu aliases + +Usage: + _canu install [flags] + +Flags: + --alias-name string name of the alias (default "canu") + -c, --config-path string Path with the AWS config file with the defined profiles (default "/Users/miguel/.aws/config") + -e, --exclude stringArray Profiles containing any of the excludes will be ignored. Exclude takes preference over include (ignores upper/lower case) + -h, --help help for install + -i, --include stringArray Profiles containing any of the includes will be available to select (ignores upper/lower case) + --shell-config string path to the shell config script (default "/Users/miguel/.bash_profile") + --skip-alias if provided it won't add an alias to the shell config script + -s, --sort If provided it will sort the profiles, by default the profiles will use the same order you have in your aws config file +``` + +There are two different kind of flags here: + +* Flags related with the installation: + ``` + --alias-name string name of the alias (default "canu") + --shell-config string path to the shell config script (default "/Users/miguel/.bash_profile") + --skip-alias if provided it won't add an alias to the shell config script + ``` + * `--alias-name`: This is the name of the alias `canu` will create, also, it will be the name of the shell script generated to set the correct value for the `AWS_PROFILE` environment variable. + * `--shell-config`: The path to your shell profile config file. By default `canu` will use `$HOME/.bash_profile`. If you use `zsh` you can run the install command with `--shell-config $HOME/.zshrc` + * `--skip-alias`: If this flag is provided `canu` won't add the alias to your shell profile config file. +* The other flags will change the behaviour when running the generated alias: + ``` + -c, --config-path string Path with the AWS config file with the defined profiles (default "/Users/miguel/.aws/config") + -e, --exclude stringArray Profiles containing any of the excludes will be ignored. Exclude takes preference over include (ignores upper/lower case) + -i, --include stringArray Profiles containing any of the includes will be available to select (ignores upper/lower case) + -s, --sort If provided it will sort the profiles, by default the profiles will use the same order you have in your aws config file + ``` + * `-c, --config-path`: The path where the aws config for your profiles is located, by default it will use `$HOME/.aws/config` + * `-e, --exclude`: Flag to exclude profiles, multiple values can be provided. `canu` will exclude the profiles containing any of the provided values with this flag. This flag takes preference over `--include` + * `-i, --include`: Flag to include profiles, multiple values can be provided. `canu` will include only the profiles containing any of provided values with this flag. + * `-s, --sort`: If this flag is provided `canu` will sort the profiles alphabetically before showing the menu. By default the order of the profiles will be the same one you have in your aws profiles config file. + +##### Examples + +* Install `canu` with alias `profile-switcher`, using as shell `zsh`: + ``` + _canu install --shell-config /Users/my-user/.zshrc --alias-name profile-switcher + ``` +* Install `canu` with default values for installation, excluding profiles with the string `test` and `dev`, sorting the list and using a different path for the aws profiles config file while running: + ``` + _canu install -e test -e dev -s -c /Users/my-user/.aws/custom-config + ``` +* Install `canu` with all custom settings: + ``` + _canu install --alias-name profile-switcher --shell-config /Users/my-user/.zshrc -c /Users/my-user/.aws/custom-config -e test -e dev -i account1 -i account2 -s + ``` + This will install `canu` with the alias `profile-switcher` using the `zsh` shell config file to create the alias, excluding profiles containing `test` and `dev`, and including profiles containing `account1` and `account2`, and sorting the profiles before showing the menu. + +### Uninstall + +The uninstall process will delete the script created by `_canu install` and remove the alias added if it's found in the file. + +The available flags for uninstall are: +``` +./bin/_canu uninstall -h +uninstalls canu aliases + +Usage: + _canu uninstall [flags] + +Flags: + --alias-name string name of the alias (default "canu") + -h, --help help for uninstall + --shell-config string path to the shell config script (default "/Users/miguel/.bash_profile") +``` + +If you used the default installation, you can just run: +``` +_canu uninstall +``` + +In case you used a different alias or a different shell config file to add the alias you can run: +``` +_canu uninstall --alias-name profile-switcher --shell-config /Users/my-user/.zshrc +``` \ No newline at end of file diff --git a/cmd/canu/install/install.go b/cmd/canu/install/install.go index cc541d3..9ecfc02 100644 --- a/cmd/canu/install/install.go +++ b/cmd/canu/install/install.go @@ -74,7 +74,7 @@ func start(cfg *config.Config) error { fmt.Printf("creating script %s for alias %s ...\n", scriptPath, cfg.AliasName) - if err := os.WriteFile(scriptPath, []byte(scriptContent), 0700); err != nil { + if err := os.WriteFile(scriptPath, []byte(scriptContent), 0755); err != nil { return fmt.Errorf("error while creating script %s: %v", scriptPath, err) } @@ -89,7 +89,7 @@ func start(cfg *config.Config) error { fmt.Printf("creating alias %s in shell config file %s ...\n", cfg.AliasName, cfg.ShellConfigScriptPath) - aliasBlock := fmt.Sprintf("\n%s\n", aliasCommand) + aliasBlock := fmt.Sprintf("\n%s", aliasCommand) f, err := os.OpenFile(cfg.ShellConfigScriptPath, os.O_APPEND|os.O_WRONLY, 0644) diff --git a/cmd/canu/main.go b/cmd/canu/main.go index f45c0db..aa039fa 100644 --- a/cmd/canu/main.go +++ b/cmd/canu/main.go @@ -56,6 +56,7 @@ func addCommonFlags(cmd *cobra.Command, c *config.Config, defaultPath string) { cmd.Flags().StringVarP(&c.ConfigPath, "config-path", "c", defaultPath, "Path with the AWS config file with the defined profiles") cmd.Flags().StringArrayVarP(&c.Include, "include", "i", []string{}, "Profiles containing any of the includes will be available to select (ignores upper/lower case)") cmd.Flags().StringArrayVarP(&c.Exclude, "exclude", "e", []string{}, "Profiles containing any of the excludes will be ignored. Exclude takes preference over include (ignores upper/lower case)") + cmd.Flags().BoolVarP(&c.IsSort, "sort", "s", false, "If provided it will sort the profiles, by default the profiles will use the same order you have in your aws config file") } func start(cfg *config.Config) error { diff --git a/pkg/canu/canu.go b/pkg/canu/canu.go index ee97d1a..f681e67 100644 --- a/pkg/canu/canu.go +++ b/pkg/canu/canu.go @@ -46,7 +46,9 @@ func (c *canu) Run() error { return fmt.Errorf("no profiles found on file %s", c.cfg.ConfigPath) } - slices.Sort(listProfiles) + if c.cfg.IsSort { + slices.Sort(listProfiles) + } menu := gocliselect.NewMenu("Choose a profile") diff --git a/pkg/config/config.go b/pkg/config/config.go index f62a778..a043a01 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,6 +13,7 @@ type Config struct { UserHomePath string Include []string Exclude []string + IsSort bool // install flags IsSkipAlias bool From 814dc47d6b7648a1a0e32b3ce6a06c3df7e163b7 Mon Sep 17 00:00:00 2001 From: belitre <3584328+belitre@users.noreply.github.com> Date: Thu, 13 Jun 2024 01:10:01 +0200 Subject: [PATCH 2/3] docs: update docs --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 06f9694..5883ad7 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,14 @@ region = us-east-1 ``` curl -sL https://github.com/belitre/canu/releases/download/1.1.0/canu-darwin-arm64.tar.gz | tar -zxf - -C $GOPATH/bin ``` - I recommend to download the `canu` binary in a folder where your user has permissions to write. Of course, as a user, you can download the binary to a folder that requires root permissions, but then you will need to run the `install` command with root permissions, and, unless you are using `sudo` which will keep the same value for your `$HOME` env var, you may need to set the path where you want `canu` to write the alias, to be sure it writes the alias to the user shell config file, not the root one. More information about this in the next section. + I recommend to download the `_canu` binary in a folder where your user has permissions to write. Of course, as a user, you can download the binary to a folder that requires root permissions, but then you will need to run the `install` command with root permissions as well, and unless you are using `sudo`, which should keep the correct value for your user in the `$HOME` env var, you may need to set the path where you want `canu` to write the alias, to be sure it writes it to the user shell config file, not the root one. More information about this in the next section. * Run `_canu install`. Running this command, `canu` will install with the default settings. That means it will use as default config path for your aws config: `$HOME/.aws/config`, and it will use `$HOME/.bash_profile` to add the alias. If you have different settings, please check the next section to learn how to customise your `canu` installation. * Restart your shell, or run the alias returned in the `_canu install` output. * Run `canu` and enjoy! What happens during the installation is: * `canu` will create a shell script in the same folder the binary is downloaded with the name of the alias (by default this name is `canu`, but you can set a different name, check the next section to learn how). -* `canu` will add an alias to your `$HOME/.bash_profile` that will source the shell script, this is how `canu` sets the selected profile in the `AWS_PROFILE` environment variable. +* `canu` will add an alias to your `$HOME/.bash_profile` that will source the generated shell script, this is how `canu` sets the selected profile in the `AWS_PROFILE` environment variable. #### Custom installation @@ -110,6 +110,12 @@ There are two different kind of flags here: _canu install --alias-name profile-switcher --shell-config /Users/my-user/.zshrc -c /Users/my-user/.aws/custom-config -e test -e dev -i account1 -i account2 -s ``` This will install `canu` with the alias `profile-switcher` using the `zsh` shell config file to create the alias, excluding profiles containing `test` and `dev`, and including profiles containing `account1` and `account2`, and sorting the profiles before showing the menu. +* Install multiple aliases for different profiles: + ``` + _canu install --alias-name canu-dev -i dev + _canu install --alias-name canu-prod -i prod + ``` + This will create two aliases, one called `canu-dev` that will include only profiles containing `dev`, and another one called `canu-prod` that will include only profiles containing `prod`. ### Uninstall From c1e7333f41963cbec175965a6ab763b72f64469a Mon Sep 17 00:00:00 2001 From: belitre <3584328+belitre@users.noreply.github.com> Date: Thu, 13 Jun 2024 01:11:37 +0200 Subject: [PATCH 3/3] docs: update docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5883ad7..9923865 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ region = us-east-1 * Run `canu` and enjoy! What happens during the installation is: -* `canu` will create a shell script in the same folder the binary is downloaded with the name of the alias (by default this name is `canu`, but you can set a different name, check the next section to learn how). +* `canu` will create a shell script in the same folder the binary is downloaded with the name of the alias (by default this name is `canu`, but you can set a different name, check the next section to learn how). This is the reason you will need to run `_canu install` with root permissions if you download the `_canu` binary to a folder where your user doesn't have write permissions! * `canu` will add an alias to your `$HOME/.bash_profile` that will source the generated shell script, this is how `canu` sets the selected profile in the `AWS_PROFILE` environment variable. #### Custom installation