ohmyzsh/plugins/scw/scw.plugin.zsh
Albin Kauffmann 62bc41d615 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")
2023-06-30 10:11:32 +02:00

104 lines
2.7 KiB
Bash

if ! command -v scw &> /dev/null; then
echo "[oh-my-zsh] scw command not found, please install it from https://github.com/scaleway/scaleway-cli"
return
fi
function sgp() {
echo "$SCW_PROFILE"
}
# SCW profile selection
function ssp() {
if [[ -z "$1" ]]; then
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
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"
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() {
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
function scw_prompt_info() {
local _scw_to_show
if [[ -n "$SCW_PROFILE" ]]; then
_scw_to_show+="${ZSH_THEME_SCW_PROFILE_PREFIX="<scw:"}${SCW_PROFILE}${ZSH_THEME_SCW_PROFILE_SUFFIX=">"}"
fi
echo "$_scw_to_show"
}
if [[ "$SHOW_SCW_PROMPT" != false && "$RPROMPT" != *'$(scw_prompt_info)'* ]]; then
RPROMPT='$(scw_prompt_info)'"$RPROMPT"
fi
# Load scw autocompletion if autocompletion not already loaded
if ! command -v _scw &> /dev/null; then
eval "$(scw autocomplete script shell=zsh)"
fi