Skip to content

Instantly share code, notes, and snippets.

@jjorissen52
Created August 10, 2020 20:43
Show Gist options
  • Save jjorissen52/b364b6a62f1504422c6122ade68ae81b to your computer and use it in GitHub Desktop.
Save jjorissen52/b364b6a62f1504422c6122ade68ae81b to your computer and use it in GitHub Desktop.
Goodies from my .bashrc file
alias ll="ls -l"
alias la="ls -al"
alias lh="ls -lh"
alias code="code-insiders"
gcloud-projects-set() {
# Make a gcloud config for this project locally if there isn't one already.
# if there is one, go ahead and activate.
local script_dir=$PWD
local configname="$1"
local project_id="$2"
local account="[email protected]"
local existing_account=$(gcloud auth list --format="value(account)" --filter="account:$account")
if [[ "$configname" == "--help" ]]; then
echo -e "Use this command to activate the given configname/project_id if it exists on this machine, and to create it if it does not.\n\n"
echo -e "Usage:\n\tgcloud projects set <configname> <project_id>\n"
return 0
fi
if ! [ -n "$existing_account" ]; then
echo "Error: Account \"$account\" is currently not authenticated via gcloud on this machine:"
gcloud config configurations list --filter="properties.core.project:$project_id OR name:$configname"
return 1
fi
# but do they match?
local existing_project_config=$(gcloud config configurations list --format="value(name)" --filter="name:($configname) AND properties.core.project:$project_id")
if [ -n "$existing_project_config" ]; then
# if one does exist, go ahead and activate it.
gcloud config configurations activate $existing_project_config
else
# if config with project_id doesn't exist, go ahead and create one
gcloud config configurations create $configname || (echo "Could not create config, exiting." && return 1) # activates on creation
gcloud config set project $project_id || (echo "Could not set project to config, exiting." && return 1)
gcloud config set account $existing_account || (echo "Could not set account to config, exiting." && return 1)
fi
}
gcloud() {
# shortens `gcloud config configurations activate <configname>` to `gcloud activate <configname>`
# and adds `gcloud projects set <configname> <projectname> to create and/or activate the provided config/project pair`
if [[ "$1" == "activate" ]]; then
shift 1
command gcloud config configurations activate "$@"
elif [[ "$1" == "projects" && "$2" == "set" ]]; then
shift 2
gcloud-projects-set "$@"
elif [[ "$1" == "ssh" ]]; then
shift 1
command gcloud compute ssh "jp.jorissen@$1"
elif [[ "$1" == "scp" ]]; then
shift 1
command gcloud compute scp "$1" "$2"
else
command gcloud "$@"
fi
}
docker() {
# docker --all stop
# Stops all running containers.
# https://unix.stackexchange.com/questions/331522/how-do-i-parse-optional-arguments-in-a-bash-script-if-no-order-is-given
while :; do
case $1 in
-A|--all) local all="SET"
;;
*) break
esac
shift
done
if [[ "$all" == "SET" ]]; then
if [[ "$1" == "stop" ]]; then
shift 1
local to_stop=$(docker ps --quiet)
if [[ "$to_stop" == "" ]]; then echo "no running containers." && return 0; fi
command docker stop $to_stop
fi
else
command docker "$@"
fi
}
git() {
# create a tag and push it to remote
# git tag --push -a test -m "This is a test"
# delete a tag locally and from remote
# git tag --push -d v.test.tag
if [[ "$1" == "tag" ]]; then
shift
while :; do
case $1 in
-p|--push) local push="SET"
shift
;;
-d|-D|--delete) local delete="SET"
shift
;;
-a|--name) local name="$2"
shift
shift
;;
*) break
esac
done
if [[ "$delete" == "SET" ]]; then
if [[ "$push" == "SET" ]]; then
command git push --delete origin "$1"
fi
command git tag -d "$1"
elif [[ ! -z "$name" ]]; then
command git tag -a "$name" "$@"
if [ "$?" == "0" ] && [ "$push" == "SET" ]; then
command git push origin "$name"
fi
else
command git tag "$@"
fi
else
command git "$@"
fi
}
delete-tag() {
git push --delete origin "$1"
git tag -d "$1"
}
pid() {
local port="$1"
if [[ "$port" == "" || "$port" == "--help" ]]; then
printf "Usage: pid <port> will give the PID of the process listening on that port\n"
return 0
fi
command lsof -nP -i4TCP:$port | grep LISTEN
}
run () {
# shortcut for some CLI commands
local program="$1"
if [[ ! -z "$program" ]]; then
case "$program" in
django-server)
command python manage.py runserver local.server.com:8000
;;
*)
echo "No configurations for $program."
return 1
esac
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment