dotfiles/.config/shell/common

143 lines
4.0 KiB
Bash

# vim: set filetype=zsh
# Set pager
export PAGER='less'
# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
export EDITOR='nvim'
else
export EDITOR='nvim'
fi
# Ansible Settings
export ANSIBLE_NOCOWS=1
export ANSIBLE_VAULT_PASSWORD_FILE=$HOME/.local/bin/ansible-vault-pass-client
# VAULT
export VAULT_ADDR=https://vault.service.consul:8200
vaultlogin () {
#vault login $(pass show personal/vault/syd1/token)
export VAULT_TOKEN=$(vault login --field=token --method=ldap username=benvin)
}
# CONSUL: https://developer.hashicorp.com/consul/commands
export CONSUL_HTTP_ADDR=consul.service.consul
export CONSUL_HTTP_TOKEN_FILE=$HOME/.config/consul/token.secret
export CONSUL_HTTP_SSL=true
export CONSUL_HTTP_SSL_VERIFY=true
# NOMAD
export NOMAD_ADDR=https://nomad.service.consul:4646
# set MPD host
export MPD_HOST="$HOME/.config/mpd/socket"
# pass passwordstore
PASSWORD_STORE_DIR=$HOME/.config/password-store
PASSWORD_STORE_KEY=$HOME/.config/password-store/.gpg-id
# LXD
export LXD_SOCKET=/run/lxd.socket
# unzip to directory of same name, minus .zip
unzipd () {
zipfile="$1"
zipdir=${1%.zip}
unzip -d "$zipdir" "$zipfile"
}
zshreload () {
source ~/.zshrc
}
winget () {
wget $1 --header="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"
}
myip () {curl https://ipinfo.io/ip}
removespaces () {
mv "$1" `echo $1 | tr ' ' '_'`
}
pdf () {xpdf $1 &}
# download youtube playlist to create batchfile
# $1 is the playlist
ytpl-export () {yt-dlp -j --flat-playlist "$1" | jq -r '.id' | sed 's_^_https://youtu.be/_'}
# common date format
shdate () {date +'%Y%m%d'}
ncurl() {
curl --netrc-file <(pass show personal/netrc) "$@"
}
export GPG_TTY=$(tty)
export RESTIC_REPOSITORY=s3:https://radosgw.service.consul/restic-personal
export RESTIC_PASSWORD_COMMAND="pass show personal/restic/metabox"
# create function later:
# large image heavy pdf into smaller pdf
# below will:
# break pages into jpg files with 300dpi
# resize images to roughly 100k
# create a new pdf from jpg files
#
# pdftoppm -jpeg -r 300 input_document.pdf output_image
# jpegoptim --size=100k output_image*.jpg
# img2pdf $(ls -v output_image*.jpg) -o output_document.pdf
# Create a git worktree for the current repo.
# Usage: newtree <branch-name> [<from-ref>]
# - If <from-ref> is omitted, defaults to HEAD.
# - Worktree path: $HOME/src/worktrees/<repo_name>/<branch_name_sanitised>
# (slashes and dashes in the *path component only* are replaced with underscores)
newtree() {
local branch from_ref repo_root repo_name sanitized dest parent
branch="$1"
from_ref="${2:-HEAD}"
if [[ -z "$branch" ]]; then
echo "Usage: newtree <branch-name> [<from-ref>]" >&2
return 2
fi
# Ensure we're inside a git repo
if ! repo_root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
echo "Error: not inside a git repository." >&2
return 1
fi
repo_name="$(basename "$repo_root")"
# Sanitize the branch *for the path only* (keep original branch name for git)
sanitized="${branch//[\/-]/_}"
dest="$HOME/src/worktrees/$repo_name/$sanitized"
parent="$(dirname "$dest")"
# Create parent dir; git will create the final leaf
mkdir -p "$parent" || {
echo "Error: unable to create directory: $parent" >&2
return 1
}
# Optional: prune stale worktrees to avoid false conflicts
git -C "$repo_root" worktree prune >/dev/null 2>&1
# Does the branch already exist?
if git -C "$repo_root" rev-parse --verify --quiet "refs/heads/$branch" >/dev/null; then
echo "Branch '$branch' exists; adding worktree at: $dest"
# --force in case the branch is already checked out elsewhere
git -C "$repo_root" worktree add --force "$dest" "$branch" || return $?
else
echo "Branch '$branch' does not exist; creating from '$from_ref' at: $dest"
git -C "$repo_root" worktree add --force -b "$branch" "$dest" "$from_ref" || return $?
fi
# Jump into the new worktree
cd "$dest" || return $?
echo "✔ Worktree ready at: $dest"
}