From 5c9dc0cfddcff9b06c6e9628ed90c23e6fe5a4e7 Mon Sep 17 00:00:00 2001 From: Albin Kauffmann Date: Fri, 30 Jun 2023 10:11:24 +0200 Subject: [PATCH 1/7] feat(scw): add new functions to handle profiles This commit adds the following commands: - ssp (Scaleway Set Profile) allows to set the current SCW_PROFILE - sgp (Scaleway Get Profile) allows to show the current SCW_PROFILE - scw_profiles allows to list available profiles Completion and error handling is supported for the ssp command. --- plugins/scw/scw.plugin.zsh | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/plugins/scw/scw.plugin.zsh b/plugins/scw/scw.plugin.zsh index cd8ed4ac7..6d2e5940f 100644 --- a/plugins/scw/scw.plugin.zsh +++ b/plugins/scw/scw.plugin.zsh @@ -12,3 +12,51 @@ _scw () { } compdef _scw scw + +function sgp() { + echo "$SCW_PROFILE" +} + +# SCW profile selection +function ssp() { + if [[ -z "$1" ]]; then + unset SCW_PROFILE + echo SCW profile cleared. + return + fi + + local -a available_profiles + available_profiles=($(scw_profiles)) + if [[ -z "${available_profiles[(r)$1]}" ]]; then + echo "${fg[red]}Profile '$1' not found in '$(scw_config_path)'" >&2 + echo "Available profiles: ${(j:, :)available_profiles:-no profiles found}${reset_color}" >&2 + return 1 + fi + + export SCW_PROFILE=$1 +} + +function scw_profiles() { + scw autocomplete complete zsh 3 -- scw --profile 2> /dev/null +} + +function scw_config_path() { + if [[ -v SCW_CONFIG_PATH ]]; then + echo "$SCW_CONFIG_PATH" + return + fi + + for f in "$XDG_CONFIG_HOME/scw/config.yaml" \ + "$HOME/.config/scw/config.yaml" \ + "$USERPROFILE/.config/scw/config.yaml"; do + if [[ -f "$f" ]]; then + echo "$f" + return + fi + done +} + +function _scw_profiles() { + reply=($(scw_profiles)) +} +compctl -K _scw_profiles ssp From 23578143d736ab6f6b459bcf994b60d66d2dfc88 Mon Sep 17 00:00:00 2001 From: Albin Kauffmann Date: Fri, 30 Jun 2023 10:11:30 +0200 Subject: [PATCH 2/7] feat(scw): display the current profile in the prompt --- plugins/scw/scw.plugin.zsh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugins/scw/scw.plugin.zsh b/plugins/scw/scw.plugin.zsh index 6d2e5940f..d0f7371ba 100644 --- a/plugins/scw/scw.plugin.zsh +++ b/plugins/scw/scw.plugin.zsh @@ -60,3 +60,17 @@ function _scw_profiles() { reply=($(scw_profiles)) } compctl -K _scw_profiles ssp + +function scw_prompt_info() { + local _scw_to_show + + if [[ -n "$SCW_PROFILE" ]]; then + _scw_to_show+="${ZSH_THEME_SCW_PROFILE_PREFIX=""}" + fi + + echo "$_scw_to_show" +} + +if [[ "$SHOW_SCW_PROMPT" != false && "$RPROMPT" != *'$(scw_prompt_info)'* ]]; then + RPROMPT='$(scw_prompt_info)'"$RPROMPT" +fi From a577aedc0b75882cbbc42060f3fc5f35658eaffd Mon Sep 17 00:00:00 2001 From: Albin Kauffmann Date: Fri, 30 Jun 2023 10:11:32 +0200 Subject: [PATCH 3/7] feat(scw): export other SCW_* vars based on the selected profile This commit makes the `ssp` command also export the following variables: - SCW_DEFAULT_ORGANIZATION_ID - SCW_DEFAULT_PROJECT_ID - SCW_DEFAULT_REGION - SCW_DEFAULT_ZONE - SCW_API_URL - SCW_ACCESS_KEY (only exported if SCW_EXPORT_TOKENS set to "true") - SCW_SECRET_KEY (only exported if SCW_EXPORT_TOKENS set to "true") --- plugins/scw/scw.plugin.zsh | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/plugins/scw/scw.plugin.zsh b/plugins/scw/scw.plugin.zsh index d0f7371ba..4e10a8b76 100644 --- a/plugins/scw/scw.plugin.zsh +++ b/plugins/scw/scw.plugin.zsh @@ -20,7 +20,14 @@ function sgp() { # SCW profile selection function ssp() { if [[ -z "$1" ]]; then - unset SCW_PROFILE + unset SCW_PROFILE \ + SCW_DEFAULT_ORGANIZATION_ID \ + SCW_DEFAULT_PROJECT_ID \ + SCW_DEFAULT_REGION \ + SCW_DEFAULT_ZONE \ + SCW_API_URL \ + SCW_ACCESS_KEY \ + SCW_SECRET_KEY echo SCW profile cleared. return fi @@ -33,7 +40,33 @@ function ssp() { return 1 fi - export SCW_PROFILE=$1 + export SCW_PROFILE="$1" + unset SCW_DEFAULT_ORGANIZATION_ID \ + SCW_DEFAULT_PROJECT_ID \ + SCW_DEFAULT_REGION \ + SCW_DEFAULT_ZONE \ + SCW_API_URL \ + SCW_ACCESS_KEY \ + SCW_SECRET_KEY + + function scw_export() { + local -r scw_var="$1" + local -r scw_key="$2" + + local -r scw_val="$(scw config get $scw_key)" + if [[ -n "$scw_val" ]] && [[ "$scw_val" != "-" ]]; then + eval "export ${scw_var}=$scw_val" + fi + } + scw_export "SCW_DEFAULT_ORGANIZATION_ID" "default-organization-id" + scw_export "SCW_DEFAULT_PROJECT_ID" "default-project-id" + scw_export "SCW_DEFAULT_REGION" "default-region" + scw_export "SCW_DEFAULT_ZONE" "default-zone" + scw_export "SCW_API_URL" "api-url" + if [[ "$SCW_EXPORT_TOKENS" = "true" ]]; then + scw_export "SCW_ACCESS_KEY" "access-key" + scw_export "SCW_SECRET_KEY" "secret-key" + fi } function scw_profiles() { From f8254668e019dcb52e4e215f3ced75820ebbe59b Mon Sep 17 00:00:00 2001 From: Albin Kauffmann Date: Fri, 30 Jun 2023 14:42:13 +0200 Subject: [PATCH 4/7] feat(scw): add a scw_upgrade command Upgrade can only be used if scw is installed under the $HOME. --- plugins/scw/scw.plugin.zsh | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/plugins/scw/scw.plugin.zsh b/plugins/scw/scw.plugin.zsh index 4e10a8b76..257f1ab42 100644 --- a/plugins/scw/scw.plugin.zsh +++ b/plugins/scw/scw.plugin.zsh @@ -89,6 +89,74 @@ function scw_config_path() { done } +function scw_upgrade() { + if ! command -v curl &> /dev/null; then + echo "[oh-my-zsh] scw upgrade requires curl, please install it" + return + fi + + local -r scw_path="$(which scw)" + if ! [[ "$scw_path" =~ "^$HOME/.*" ]]; then + echo "[oh-my-zsh] scw not installed in your HOME, upgrade cannot be performed" + return + fi + local scw_version="" + while read -r line; do + if [[ "$line" =~ "^Version +([0-9.]+)\$" ]]; then + scw_version="${match[1]}" + break + fi + done <<< "$(scw version)" + if [[ -z "$scw_version" ]]; then + echo "[oh-my-zsh] cannot determine current scw version" + return + fi + + local -r shasums="$(curl --location --silent "https://github.com/scaleway/scaleway-cli/releases/latest/download/SHA256SUMS")" + if [[ -z "$shasums" ]]; then + echo "[oh-my-zsh] cannot download the list of binaries for the last release" + return + fi + + local -r kernel_name="${$(uname --kernel-name):l}" + local -r arch="${$(uname --machine)//x86_64/amd64}" + local binary_sha256="" + local binary_name="" + while read -r line; do + if [[ "$line" =~ "^([0-9a-f]+) (scaleway-cli_[0-9.]+_${kernel_name}_${arch})\$" ]]; then + binary_sha256="${match[1]}" + binary_name="${match[2]}" + break + fi + done <<< "$shasums" + if [[ -z "$binary_sha256" ]] || [[ -z "$binary_name" ]]; then + echo "[oh-my-zsh] cannot find a scw binary for your computer" + return + fi + if [[ "$binary_name" =~ "^scaleway-cli_${scw_version}_.*\$" ]]; then + echo "[oh-my-zsh] current scw version is already the latest (v${scw_version})" + return + fi + + local -r binary_tmp="$(mktemp)" + echo "[oh-my-zsh] downloading ${binary_name}..." + curl --location --progress-bar "https://github.com/scaleway/scaleway-cli/releases/latest/download/${binary_name}" -o "$binary_tmp" + if [[ $? -ne 0 ]]; then + echo "[oh-my-zsh] cannot download the latest ${binary_name} release binary" + rm -f "$binary_tmp" + return + fi + if [[ "${$(sha256sum "$binary_tmp")%% *}" != "$binary_sha256" ]]; then + echo "[oh-my-zsh] downloaded ${binary_name} binary has a wrong sha256sum" + rm -f "$binary_tmp" + return + fi + # Install new scw command and preserve original file permissions + \cp --no-preserve=all --force "$binary_tmp" "$scw_path" + echo "[oh-my-zsh] scw successfully updated" + rm -f "$binary_tmp" +} + function _scw_profiles() { reply=($(scw_profiles)) } From 80e3d1313eb7fd2f870d92e4982426b701bac068 Mon Sep 17 00:00:00 2001 From: alysscendre Date: Fri, 30 Jun 2023 16:04:21 +0200 Subject: [PATCH 5/7] docs(scw): update scw-plugin README.md --- plugins/scw/README.md | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/plugins/scw/README.md b/plugins/scw/README.md index 5dd630d64..9c2a78adb 100644 --- a/plugins/scw/README.md +++ b/plugins/scw/README.md @@ -7,3 +7,48 @@ To use it, add `scw` to the plugins array in your zshrc file: ```zsh plugins=(... scw) ``` + +This plugin also adds functions to manage your Scaleway profile + +## Prerequisites + +Scaleway CLI (scw) should be installed. You can install it from https://github.com/scaleway/scaleway-cli. + +Copy and paste the code into your Zsh shell configuration file (e.g., .zshrc). +Source the updated configuration file or restart your shell. + + +```bash +plugins=(... scw) +``` + +## Functions + +| Commands | Description | +| :---------------: |:---------------| +| scw_upgrade | Update your Scaleway CLI version if needed. | +| sgp | Displays the current Scaleway profile. | +| ssp | Sets the Scaleway profile. If no profile name is provided, fallback to the curent active profile set in your +configuration file. | +| scw_profiles | Displays a list of available Scaleway profiles. | +| scw_config_path | Returns the path to the Scaleway CLI configuration file (config.yaml). | + +In addition to setting the `SCW_PROFILE` environment variable, `ssp` also sets the following variables: `SCW_DEFAULT_ORGANIZATION_ID`, +`SCW_DEFAULT_PROJECT_ID`, `SCW_DEFAULT_REGION`, `SCW_DEFAULT_ZONE`, `SCW_API_URL`. +Additionnally, if `SCW_EXPORT_TOKENS` is set to "true", `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` are also exported. + + +## Customizations + +| Commands | Description | +| :---------------: |:---------------| +| SHOW_SCW_PROMPT | Controls whether to display the Scaleway profile information in the shell prompt. Set this variable to false if you don't +want to show the profile information. | +| ZSH_THEME_SCW_PROFILE_PREFIX | sets the prompt prefix. Defaults to `` | + + +## Scaleway CLI Autocompletion + +If Scaleway CLI autocompletion is not already loaded, the code automatically loads the autocompletion script for the scw command. This enables +autocompletion for all Scaleway CLI commands. From b47107c12072d502ac37d42ec950afe81f6bbd88 Mon Sep 17 00:00:00 2001 From: Albin Kauffmann Date: Wed, 5 Jul 2023 11:30:27 +0200 Subject: [PATCH 6/7] docs(scw): fix and reindent README file correctly --- plugins/scw/README.md | 52 +++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/plugins/scw/README.md b/plugins/scw/README.md index 9c2a78adb..c1931ae79 100644 --- a/plugins/scw/README.md +++ b/plugins/scw/README.md @@ -8,15 +8,15 @@ To use it, add `scw` to the plugins array in your zshrc file: plugins=(... scw) ``` -This plugin also adds functions to manage your Scaleway profile +This plugin also adds functions to easily manage your Scaleway profiles with the +`scw` command. ## Prerequisites -Scaleway CLI (scw) should be installed. You can install it from https://github.com/scaleway/scaleway-cli. - -Copy and paste the code into your Zsh shell configuration file (e.g., .zshrc). -Source the updated configuration file or restart your shell. +Scaleway CLI (scw) should be installed. You can install it from +https://github.com/scaleway/scaleway-cli. +Copy and paste the code into your Oh My Zsh configuration file (e.g., .zshrc): ```bash plugins=(... scw) @@ -24,31 +24,31 @@ plugins=(... scw) ## Functions -| Commands | Description | -| :---------------: |:---------------| -| scw_upgrade | Update your Scaleway CLI version if needed. | -| sgp | Displays the current Scaleway profile. | -| ssp | Sets the Scaleway profile. If no profile name is provided, fallback to the curent active profile set in your -configuration file. | -| scw_profiles | Displays a list of available Scaleway profiles. | -| scw_config_path | Returns the path to the Scaleway CLI configuration file (config.yaml). | - -In addition to setting the `SCW_PROFILE` environment variable, `ssp` also sets the following variables: `SCW_DEFAULT_ORGANIZATION_ID`, -`SCW_DEFAULT_PROJECT_ID`, `SCW_DEFAULT_REGION`, `SCW_DEFAULT_ZONE`, `SCW_API_URL`. -Additionnally, if `SCW_EXPORT_TOKENS` is set to "true", `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` are also exported. +| Commands | Description | +| :---------------: |:-------------------------------------------------------- | +| scw_upgrade | Update your Scaleway CLI version if needed. | +| sgp | Displays the current Scaleway profile. | +| ssp | Sets the Scaleway profile. If no profile name is provided, fallback to the curent active profile set in your configuration file. | +| scw_profiles | Displays a list of available Scaleway profiles. | +| scw_config_path | Returns the path to the Scaleway CLI configuration file (config.yaml). | +In addition to setting the `SCW_PROFILE` environment variable, `ssp` also sets +the following variables: `SCW_DEFAULT_ORGANIZATION_ID`, +`SCW_DEFAULT_PROJECT_ID`, `SCW_DEFAULT_REGION`, `SCW_DEFAULT_ZONE`, +`SCW_API_URL`. +Additionnally, if `SCW_EXPORT_TOKENS` is set to "true", `SCW_ACCESS_KEY` and +`SCW_SECRET_KEY` are also exported. ## Customizations -| Commands | Description | -| :---------------: |:---------------| -| SHOW_SCW_PROMPT | Controls whether to display the Scaleway profile information in the shell prompt. Set this variable to false if you don't -want to show the profile information. | -| ZSH_THEME_SCW_PROFILE_PREFIX | sets the prompt prefix. Defaults to `` | - +| Env variables | Description | +| :--------------------------: |:--------------------------------------------- | +| SHOW_SCW_PROMPT | Controls whether to display the Scaleway profile information in the shell prompt. Set this variable to false if you don't want to show the profile information. | +| ZSH_THEME_SCW_PROFILE_PREFIX | sets the prompt prefix. Defaults to `` | ## Scaleway CLI Autocompletion -If Scaleway CLI autocompletion is not already loaded, the code automatically loads the autocompletion script for the scw command. This enables -autocompletion for all Scaleway CLI commands. +If Scaleway CLI autocompletion is not already loaded, the code automatically +loads the autocompletion script for the scw command. This enables autocompletion +for all Scaleway CLI commands. From 27015ffa5ecb5378e175a01c2668735d0982f13a Mon Sep 17 00:00:00 2001 From: Albin Kauffmann Date: Wed, 5 Jul 2023 14:19:59 +0200 Subject: [PATCH 7/7] feat(scw): allow to set profile to "default" --- plugins/scw/scw.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scw/scw.plugin.zsh b/plugins/scw/scw.plugin.zsh index 257f1ab42..34bbeed5d 100644 --- a/plugins/scw/scw.plugin.zsh +++ b/plugins/scw/scw.plugin.zsh @@ -70,7 +70,7 @@ function ssp() { } function scw_profiles() { - scw autocomplete complete zsh 3 -- scw --profile 2> /dev/null + scw autocomplete complete zsh 5 -- scw config profile activate 2> /dev/null } function scw_config_path() {