Skip to content

Instantly share code, notes, and snippets.

@tomcoakes
Last active March 29, 2021 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomcoakes/9cfd2c8f2ab27bac2da04919b754a239 to your computer and use it in GitHub Desktop.
Save tomcoakes/9cfd2c8f2ab27bac2da04919b754a239 to your computer and use it in GitHub Desktop.
display current stack in prompt but only make call when changing directory
setopt PROMPT_SUBST
pulumi()
{
if [[ "$@" == *"stack select"* ]] then
CURRENT_STACK=$(cat ~/.pulumi-prompt.json | jq '.currentStack' -r) && \
jq -n --arg currentStack $CURRENT_STACK --arg previousPwd $PWD '{"currentStack":$currentStack, "previousPwd":$previousPwd, "stackChanged": true}' > ~/.pulumi-prompt.json
fi
/opt/homebrew/bin/pulumi "$@"
}
up_find()
{
while [[ $PWD != / ]] ; do
find "$PWD"/ -maxdepth 1 "$@"
cd ..
done
}
get_pulumi_stack()
{
PREVIOUS_PWD=$(cat ~/.pulumi-prompt.json | jq '.previousPwd' -r)
STACK_CHANGED=$(cat ~/.pulumi-prompt.json | jq '.stackChanged' -r)
CURRENT_STACK=$(cat ~/.pulumi-prompt.json | jq '.currentStack' -r) && jq -n --arg currentStack $CURRENT_STACK --arg previousPwd $PWD --arg stackChanged $STACK_CHANGED '{"currentStack":$currentStack, "previousPwd":$previousPwd, "stackChanged":$stackChanged}' > ~/.pulumi-prompt.json
if [[ $(up_find -name 'Pulumi.yaml') ]] && [[ "$PWD" != "$PREVIOUS_PWD" || $STACK_CHANGED == true ]] then
CURRENT_STACK=$(pulumi stack --show-name)
jq -n --arg currentStack $CURRENT_STACK --arg previousPwd $PWD '{"currentStack":$currentStack, "previousPwd":$previousPwd, "stackChanged":false}' > ~/.pulumi-prompt.json
print "\n%F{blue}\E[3mcurrent stack: $CURRENT_STACK \E[23m "
elif [[ $(up_find -name 'Pulumi.yaml') ]] then
CURRENT_STACK=$(cat ~/.pulumi-prompt.json | jq '.currentStack' -r) && jq -n --arg currentStack $CURRENT_STACK --arg previousPwd $PWD '{"currentStack":$currentStack, "previousPwd":$previousPwd, "stackChanged":false}' > ~/.pulumi-prompt.json
print "\n%F{blue}\E[3mcurrent stack: $CURRENT_STACK \E[23m "
fi
}
PS1="\$(get_pulumi_stack)$PS1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment