Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions gsw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
GSW_VERSION="v0.3.0"

function gsw() {
# Check if gcloud is installed
if ! command -v gcloud >/dev/null 2>&1; then
echo "Error: 'gcloud' command not found."
echo "Please install the Google Cloud SDK first: https://cloud.google.com/sdk/docs/install"
return 1
fi

local is_global=false
local config_name=""

Expand Down Expand Up @@ -106,15 +113,36 @@ function gsw() {
if [ -n "$BASH_VERSION" ]; then
# Bash Completion
_gsw_bash_autocomplete() {
local cur opts
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Fetch configurations
opts=$(gcloud config configurations list --format="value(name)" 2>/dev/null)
# Flags
opts="-g --global -h --help -v --version"

case "${prev}" in
-g|--global)
# Only configurations after global flag
local configs
configs=$(gcloud config configurations list --format="value(name)" 2>/dev/null)
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${configs}" -- "${cur}") )
return 0
;;
esac

if [[ ${cur} == -* ]] ; then
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi

# Default: configurations + flags
local configs
configs=$(gcloud config configurations list --format="value(name)" 2>/dev/null)
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
COMPREPLY=( $(compgen -W "${opts} ${configs}" -- "${cur}") )
return 0
}
complete -F _gsw_bash_autocomplete gsw
Expand Down
26 changes: 26 additions & 0 deletions test/gsw.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ setup() {
}
export -f gcloud

# Mock 'command -v gcloud' to return success by default in tests
function command() {
if [[ "$1" == "-v" && "$2" == "gcloud" ]]; then
return 0
fi
builtin command "$@"
}
export -f command

# Source the script
source ./gsw.sh
}
Expand Down Expand Up @@ -96,3 +105,20 @@ setup() {
[ "$status" -eq 1 ]
[[ "$output" =~ "Error: Configuration 'invalid-config' does not exist" ]]
}

@test "gsw returns error if gcloud is not installed" {
# Mock 'command -v gcloud' failure
function command() {
if [[ "$1" == "-v" && "$2" == "gcloud" ]]; then
return 1
fi
builtin command "$@"
}
export -f command

run gsw
[ "$status" -eq 1 ]
[[ "$output" =~ "Error: 'gcloud' command not found" ]]

unset -f command
}
Loading