Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Created June 5, 2024 15:56
Show Gist options
  • Save greyhoundforty/994957298aeb3b0c252c10cb7fc5cebc to your computer and use it in GitHub Desktop.
Save greyhoundforty/994957298aeb3b0c252c10cb7fc5cebc to your computer and use it in GitHub Desktop.
zsh-functions for remote load with zinit
function get_ubuntu_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting Ubuntu images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility public --output json | jq -r '.[] | select(.status=="available") | select(.operating_system.architecture=="amd64") | select(.name | startswith("ibm-ubuntu")) | .name,.id'
}
function get_windows_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting Windows images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility public --output json | jq -r '.[] | select(.status=="available") | select(.operating_system.architecture=="amd64") | select(.name | startswith("ibm-windows")) | .name,.id'
}
function get_all_public_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting all public x86 VPC images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility public --output json | jq -r '.[] | select(.status=="available") | select(.operating_system.architecture=="amd64") | .name,.id'
}
function get_all_private_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting all private VPC images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility private --output json | jq -r '.[] | select(.status=="available") | .name,.id'
}
function showFiles() {
defaults write com.apple.Finder AppleShowAllFiles true
killAll Finder
}
function hideFiles() {
defaults write com.apple.Finder AppleShowAllFiles false
killAll Finder
}
function hs() {
hishtory config-set filter-duplicate-commands true
hishtory config-set displayed-columns Hostname Command
hishtory query "$@"
}
function tfv() {
terraform fmt --recursive
terraform init -upgrade=true
terraform validate
}
function icl() {
ibmcloud login -r "$@" -q > /dev/null 2>&1
ibm_region=$(ibmcloud target --output json | jq -r .region.name)
printf "%b" "\e[1;34mConnected to ${ibm_region}:.\e[0m"
}
function get_scrt() { scrt get "$@" | pbcopy }
function 1pass() {
scrt get onepassword | pbcopy
}
# Target IBM Cloud CDE account
function target_cde() {
unset IBMCLOUD_API_KEY;
export IBMCLOUD_API_KEY=$(scrt get ibmcloud_api_key);
icl us-south
ibmcloud target -g CDE
}
# Target IBM Cloud dev account
function target_dev() {
unset IBMCLOUD_API_KEY;
export IBMCLOUD_API_KEY=$(scrt get dev_account_ibmcloud_api_key);
icl us-south
ibmcloud target -g Default
}
function target_personal() {
unset IBMCLOUD_API_KEY;
export IBMCLOUD_API_KEY=$(scrt get personal_ibmcloud_api_key);
icl us-south
ibmcloud target -g Default
}
# Remove an IP from the SSH known_hosts file
function iprem() {
printf "%b" "\e[1;34mRemoving IP ${@} from known hosts file::\e[0m\n"
ssh-keygen -R "$@" -f ~/.ssh/known_hosts
}
# SSH through a bastion host. Usage: ssh_tunnel <bastion> <target>
function ssh_tunnel() {
printf "%b" "\e[1;34mSSH tunneling through ${1} to ${2}::\e[0m\n"
ssh -o ProxyCommand="ssh -W %h:%p root@${1}" "root@${2}"
}
# Run tofu plan against current workspace
function tfp() {
printf "%b" "\e[1;34mRunning terraform plan against $(terraform workspace show) workspace: \e[0m"
printf "%b" "\e[1;::\e[0m\n"
terraform plan -out "$(terraform workspace show).tfplan"
}
# Run tofu apply -auto-approve against current workspace
function tfa() {
printf "%b" "\e[1;34mRunning terraform apply with auto-approve against $(terraform workspace show) workspace: \e[0m"
printf "%b" "\e[1;::\e[0m\n"
terraform apply -auto-approve
}
tfd () {
printf "%b" "\e[1;34mRunning terraform destroy against current terraform workspace: $(terraform workspace show)\e[0m"
printf "%b" "\e[1;::\e[0m\n"
terraform plan -destroy -out "destroy-$(terraform workspace show).tfplan"
}
tfda () {
printf "%b" "\e[1;34mRunning terraform destroy apply against current terraform workspace: $(terraform workspace show)\e[0m"
printf "%b" "\e[1;::\e[0m\n"
terraform apply "destroy-$(terraform workspace show).tfplan"
}
# Use Permute to convert image/audio/video files
function file_convert() {
open -a Permute "$@"
}
# Capture current screen and save to ~/Sync/Screenshots (Synology mount)
function capture_screen() {
capture_time=$(date +%Y-%m-%d-%H%M)
screencapture "$HOME/Sync/Screenshots/${capture_time}-screenshot.png"
}
# Jump to current projects directory and use ranger to navigate
function project_jump() {
export PROJECTS_DIR="${HOME}/Sync/SynologyDrive/current-projects"
ranger $PROJECTS_DIR
}
# Find the 20 most recently modified directories in the current path
function recentdirs() {
tree -D -L 1 -d | head -n 20
}
# Find the 20 most recently modified directories in our projects folder
function recentprojects() {
export PROJECTS_DIR="${HOME}/Sync/SynologyDrive/current-projects"
tree -D -L 1 -d $PROJECTS_DIR | head -n 20
}
# Perform a gitdiff using bat
batdiff() {
git diff --name-only --relative --diff-filter=d | xargs bat --diff
}
# Get IAM token from IBM IAM API and set to TOKEN variable
function get_iam_token() {
TOKEN=$(curl -s -X POST "https://iam.cloud.ibm.com/identity/token" --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" --data-urlencode "apikey=${IBMCLOUD_API_KEY}" | jq -r '.access_token')
}
# Get external IP address
function get_external_ip() {
curl -s "https://api.ipify.org?format=json" | jq -r '.ip'
}
# Use awk to grab lines from a file. Usage: grab_lines <start_line> <end_line> <file>
function grab_lines() {
awk NR=="$1",NR=="$2" "$3"
}
function take () {
if [[ $1 =~ ^(https?|ftp).*\.(tar\.(gz|bz2|xz)|tgz)$ ]]
then
takeurl "$1"
elif [[ $1 =~ ^([A-Za-z0-9]\+@|https?|git|ssh|ftps?|rsync).*\.git/?$ ]]
then
takegit "$1"
else
takedir "$@"
fi
}
takeurl() {
local data thedir
data="$(mktemp)"
curl -L "$1" > "$data"
tar xf "$data"
thedir="$(tar tf "$data" | head -n 1)"
rm "$data"
cd "$thedir"
}
takegit() {
git clone "$1"
cd "$(basename ${1%%.git})"
}
takedir() {
mkdir -p "$@" && cd "${@:$#}"
}
function namegen() {
uuidgen | tr '[:upper:]' '[:lower:]'
}
function new-pyenv() {
# Check if a Python version is specified
if [ -n "$1" ]; then
PYTHON_VERSION=$1
else
echo "Please specify a Python version, e.g. 3.12.0"
exit 1
fi
# Extract the current directory name
BASE_PATH=$(basename `pwd`)
# Create a virtual environment with the specified Python version and the current directory name
pyenv virtualenv $PYTHON_VERSION $BASE_PATH
# Set the local Python version to the newly created virtual environment
pyenv local $BASE_PATH
echo "Python virtual environment '$BASE_PATH' created successfully with Python version $PYTHON_VERSION"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment