Skip to content

Instantly share code, notes, and snippets.

@fenying
Last active August 13, 2024 07:58
Show Gist options
  • Save fenying/83ed7e4efa0ce89a972a07313fecce9b to your computer and use it in GitHub Desktop.
Save fenying/83ed7e4efa0ce89a972a07313fecce9b to your computer and use it in GitHub Desktop.
Personal Shell Aliases
# bash assistant commands
# [General]
alias z.cls='clear'
alias z.history.clear='rm -f ~/.bash_history && history -c'
alias z.uuid='cat /proc/sys/kernel/random/uuid'
alias z.pss='ps -A -opid,cmd'
z.file.new.sh() {
if [[ -z "$1" ]]; then
echo "Usage: new_sh_file <filename>"
return 1
fi
# Create a new file
if [[ -f "$1" ]]; then
echo "File $1 already exists."
return 1
fi
if [[ -d "$1" ]]; then
echo "Folder $1 already exists."
return 1
fi
if ! touch "$1"; then
echo "Failed to create file $1."
return 1
fi
# Add the shebang
echo "#!/usr/bin/env bash" >> $1
# Add the content
echo 'SCRIPT_ROOT=$(cd $(dirname $0); pwd)' >> $1
# Make the file executable
chmod +x $1
}
# [Network]
if [[ -z "$Z_NETWORK_NIC_NAME" ]]; then
Z_NETWORK_NIC_NAME='eth0'
fi
alias z.network.ip.public4='curl -4 ifconfig.me'
alias z.network.ip.public6='curl --ipv6 ifconfig.me'
alias z.network.ip.private4='ip addr show $Z_NETWORK_NIC_NAME | grep "inet " | grep -Eo "[0-9]+([.][0-9]+){3}/[0-9]+"'
alias z.network.ip.private6='ip addr show $Z_NETWORK_NIC_NAME | grep -Eo "inet6 [0-9a-f:/]+" | grep -Eo "[0-9a-f:/]+$"'
# [VSCode]
alias z.vscode.open=code
z.vscode.kill() {
kill -9 $(ps aux | grep '[v]scode' | awk '{print $2}')
}
# [TTY] Colorful PS
ps_get_git_branch() {
if [[ -d .git ]]; then
PS_GIT_BRANCH=$(git branch 2>/dev/null | grep '^*' | sed 's/* //')
if [[ -z "$PS_GIT_BRANCH" ]]; then
PS_GIT_BRANCH=''
elif [[ -z "$(echo $PS_GIT_BRANCH | grep '(HEAD detached at')" ]]; then
PS_GIT_BRANCH="@~$PS_GIT_BRANCH"
else
PS_GIT_BRANCH="@@$(echo $PS_GIT_BRANCH | sed 's/(HEAD detached at //' | sed 's/)//')"
fi;
echo -n $PS_GIT_BRANCH
fi;
}
# [Golang]
export GOPATH=~/projects/go
# [NodeJS]
alias z.node.debug='node --enable-source-maps --inspect=0.0.0.0:2333'
alias z.node.pretty='node --enable-source-maps'
export N=./node_modules/.bin
# [GPG]
export GPG_TTY=$(tty)
alias x.gpg.keys.list-secret='gpg --list-secret-keys --keyid-format=long'
alias x.gpg.keys.export-public='gpg --armor --export'
# [Git]
if [[ -f /usr/share/git/completion/git-completion.bash ]]; then
source /usr/share/git/completion/git-completion.bash
fi
z.file.new.gitignore() {
NEW_FILE_PATH=$PWD/.gitignore
# Create a new file
if [[ -f "$NEW_FILE_PATH" ]]; then
echo "File $NEW_FILE_PATH already exists."
return 1
fi
if [[ -d "$NEW_FILE_PATH" ]]; then
echo "$NEW_FILE_PATH is a directory"
return 1
fi
if ! touch "$NEW_FILE_PATH"; then
echo "Failed to create file $NEW_FILE_PATH."
return 1
fi
echo 'node_modules/' >> $NEW_FILE_PATH
echo 'lib/' >> $NEW_FILE_PATH
echo 'app/' >> $NEW_FILE_PATH
echo 'tsconfig.tsbuildinfo' >> $NEW_FILE_PATH
echo '*.js' >> $NEW_FILE_PATH
echo '*.js.map' >> $NEW_FILE_PATH
echo '*.d.ts' >> $NEW_FILE_PATH
echo '*.ts.map' >> $NEW_FILE_PATH
echo '!commitlint.config.js' >> $NEW_FILE_PATH
}
alias z.git.ssh-init='ssh-add ~/.ssh/git.pem'
alias z.git.workspace.init='git init && touch .gitignore'
alias z.git.workspace.get='git clone'
alias z.git.workspace.clean='git clean -dfx .'
alias z.git.workspace.status='git status'
alias z.git.file.recover='git checkout'
alias z.git.file.stage='git add'
alias z.git.file.unstage='git restore --staged'
alias z.git.commit.reverse='git reset --soft'
alias z.git.commit.diff='git diff'
alias z.git.commit.submit='git commit -m'
alias z.git.commit.goto='git checkout'
alias z.git.commit.ls.verbose='git log --show-signature'
alias z.git.commit.ls='git log'
alias z.git.commit.quick-submit='git commit --no-verify -m'
alias z.git.branch.ls='git branch -a'
alias z.git.branch.fork.to='git checkout -b'
alias z.git.branch.fork.from='git checkout -b rebase-$(z.git.branch.now)'
alias z.git.branch.rebase.start='git rebase'
alias z.git.branch.rebase.continue='git rebase --continue'
alias z.git.branch.rebase.abort='git rebase --abort'
alias z.git.branch.goto='git switch'
alias z.git.branch.rm='git branch -d'
alias z.git.branch.del='git branch -d'
alias z.git.branch.del-force='git branch -D'
alias z.git.branch.merge='git merge'
alias z.git.branch.sync.down='git pull'
alias z.git.branch.sync.up='git push'
z.git.branch.sync.up-bind() {
THE_ORIGIN=$1
if [[ -z "$THE_ORIGIN" ]]; then
THE_ORIGIN=$(git remote | head -n 1);
fi
if [[ -z "$THE_ORIGIN" ]]; then
echo 'No available origin';
exit -1;
fi
git push -u $THE_ORIGIN $(git branch --show-current)
}
z.git.tag.release.sandbox() {
local ver=$1
if [[ -z "$2" ]]; then
echo "Usage: g.tag.release.sandbox <ver> <msg> [suffix]"
return 1
fi
if [ "$(echo $ver | grep -E '^v[0-9]+[.][0-9]+[.][0-9]+$')" != "$ver" ]; then
echo "Usage: g.tag.release.sandbox <ver> <msg> [suffix]"
return 1
fi
if [[ ! -z "$3" ]]; then
if [ "$(echo $3 | grep -E '^[-_.a-z0-9]+$')" != "$3" ]; then
echo "Usage: g.tag.release.sandbox <ver> <msg> [suffix]"
return 1
fi
fi
local tagName="sandbox-$ver-$(date +%Y%m%d)"
local foundTags=$(git tag | grep $tagName)
if [[ -n "$foundTags" ]]; then
echo "Tag $tagName already exists"
return 1
fi
if [[ ! -z "$3" ]]; then
tagName="$tagName-$3"
fi
echo "Current git status:"
echo " Branch: $(git branch --show-current)"
echo " Commit Hash: $(git log --format='%h' -1)"
echo " Commit Time: $(git log --format='%ai' -1)"
echo " Commit Message: $(git log --format='%s' -1)"
echo " Commit Author: $(git log --format='%an <%ae>' -1)"
echo "New Tag:"
echo " Tag Name: $tagName"
echo " Tag Message: $2"
echo -n "Do you want to continue? [y/N] "
read -r answer
if [[ "$answer" != "y" ]]; then
echo "WARNING: Cancelled creating sandbox release $tagName"
return 1
fi
if git tag -m "$2" $tagName; then
echo "Tag $tagName created successfully"
return 0
else
echo "Failed to create tag $tagName"
return 1
fi
}
z.git.tag.release.production() {
if [[ -z "$1" ]]; then
echo "Usage: g.tag.release.production <sandbox-tag>"
return 1
fi
if [[ "$1" != "$(echo $1 | grep -E '^sandbox-v[0-9]+[.][0-9]+[.][0-9]+-[0-9]{8}(-[-_.a-z0-9]+)?$')" ]]; then
echo "Usage: g.tag.release.production <sandbox-tag>"
echo "ERROR: Invalid sandbox tag $1"
return 1
fi;
local sandboxTagPrefix=$(echo $1 | grep -Eo '^sandbox-v[0-9]+[.][0-9]+[.][0-9]+-[0-9]{8}')
local foundTags=$(git tag | grep -o $1)
if [[ -z "$foundTags" ]]; then
echo "Tag $1 not found"
return 1
fi
if ! git checkout $1; then
echo "Failed to checkout tag $1"
return 1
fi;
local ver=$(echo $1 | grep -Eo 'v[0-9]+.[0-9]+.[0-9]+')
local tagName="production-$ver-$(date +%Y%m%d)"
if [[ "$sandboxTagPrefix" != "$1" ]]; then
local tagSuffix=${1#"$sandboxTagPrefix"}
tagName="$tagName$tagSuffix"
fi
foundTags=$(git tag | grep $tagName)
if [[ -n "$foundTags" ]]; then
echo "Tag $tagName already exists"
return 1
fi
echo -n "Creating tag $tagName, do you want to continue? [y/N] "
read -r answer
if [[ "$answer" != "y" ]]; then
return 1
fi
if git tag -m "$2" $tagName; then
echo "Tag $tagName created from tag $1 successfully"
return 0
else
echo "Failed to create tag $tagName"
return 1
fi
}
z.git.branch.sync.up2all() {
for o in $(git remote); do
git push $o $(z.git.branch.now) ${@:1}
done
}
z.git.branch.now() {
git branch --show-current
}
alias z.git.tag.ls='git tag'
alias z.git.tag.add='git tag -m'
alias z.git.tag.goto='git checkout'
alias z.git.tag.rm='git tag -d'
alias z.git.tag.del='git tag -d'
alias z.git.tag.up='git push --tag'
z.git.tag.up2all() {
for o in $(git remote); do
git push --tag $o $(z.git.branch.now)
done
}
alias z.git.remote.ls='git remote -v'
alias z.git.remote.sync='git fetch --all'
alias z.git.remote.add='git remote add'
alias z.git.remote.rm='git remote remove'
alias z.git.memory.save='git stash'
alias z.git.memory.recover='git stash pop'
# [Docker]
source <(docker completion bash)
alias z.docker.service.ls='docker ps'
alias z.docker.service.names="docker ps --format '{{.ID}} {{.Names}}'"
alias z.docker.service.rm='docker rm'
alias z.docker.service.rm-force='docker rm -f'
alias z.docker.service.start='docker start'
alias z.docker.service.stop='docker stop'
alias z.docker.service.restart='docker restart'
alias z.docker.service.exec='docker exec'
alias z.docker.service.exec-it='docker exec -it'
alias z.docker.service.create='docker run -d'
alias z.docker.service.show='docker inspect'
alias z.docker.service.go='docker run -it --rm --network=host -v$PWD:$PWD -w$PWD'
alias z.docker.image.ls='docker images'
alias z.docker.image.rm='docker rmi'
alias z.docker.image.clean='docker image prune -a'
alias z.docker.image.build='docker build -t'
alias z.docker.image.push='docker push'
alias z.docker.image.pull='docker pull'
alias z.docker.logs.all='docker logs'
alias z.docker.logs.last-n='docker logs --tail'
alias z.docker.add-tag='docker tag'
alias z.docker.find-by-pid="docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Name}}' | grep -P"
alias z.docker.compose.up='docker compose up'
alias z.docker.compose.down='docker compose down'
alias z.docker.compose.ls='docker compose ps'
alias z.docker.compose.restart='docker compose restart'
alias z.docker.compose.start='docker compose start'
alias z.docker.compose.stop='docker compose stop'
alias z.docker.compose.top='docker compose top'
alias z.docker.compose.config='docker compose config'
alias z.docker.compose.exec='docker compose exec'
alias z.docker.compose.exec-it='docker compose exec -it'
alias z.docker.compose.logs.last-n='docker compose logs --tail'
alias z.docker.compose.logs.all='docker compose logs'
alias z.docker.compose.image.pull='docker compose pull'
# [SSH]
z.ssh.list() {
if [[ ! -z "$1" ]]; then
cat ~/.ssh/$1/config | grep '^Host' | cut -d ' ' -f 2
else
cat ~/.ssh/config | grep '^Host' | cut -d ' ' -f 2
fi
}
z.ssh.map-port-to-local() {
local LOCAL_HOST=$SSH_MAP_LOCAL_HOST
local LOCAL_PORT=$SSH_MAP_LOCAL_PORT
if [[ -z "$SSH_MAP_REMOTE_HOST" ]] || [[ -z "$SSH_MAP_REMOTE_PORT" ]]; then
echo "Usage: ssh-map-port-to-local"
echo " env export [email protected] [Required]"
echo " env export SSH_MAP_REMOTE_PORT=12345 [Required]"
echo " env export SSH_MAP_REMOTE_HOST=127.0.0.1 [Default: 127.0.0.1]"
echo " env export SSH_MAP_LOCAL_HOST=0.0.0.0 [Default: 127.0.0.1]"
echo " env export SSH_MAP_LOCAL_PORT=23456 [Default: \$SSH_MAP_REMOTE_PORT]"
return 1
fi
if [[ -z "$LOCAL_HOST" ]]; then
LOCAL_HOST=127.0.0.1
fi
if [[ -z "$LOCAL_PORT" ]]; then
LOCAL_PORT=$SSH_MAP_REMOTE_PORT
fi
if [[ -z "$SSH_MAP_REMOTE_HOST" ]]; then
LOCAL_PORT=127.0.0.1
fi
echo "Mapping $SSH_MAP_REMOTE_HOST:$SSH_MAP_REMOTE_PORT on $SSH_MAP_REMOTE_SERVER [Remote] -> $LOCAL_HOST:$LOCAL_PORT [Local]"
ssh \
-o StrictHostKeyChecking=no \
-4 \
$SSH_MAP_REMOTE_SERVER \
-N \
-L $LOCAL_HOST:$LOCAL_PORT:$SSH_MAP_REMOTE_HOST:$SSH_MAP_REMOTE_PORT
}
z.ssh.map-docker-debug-port-to-local() {
if [ -z "$DOCKER_NAME_KEYWORD" ] || [ -z "$DOCKER_BIND_PORT" ] || [ -z "$SSH_MAP_REMOTE_SERVER" ]; then
echo "Usage: ssh-map-docker-debug-port-to-local"
echo " env export DOCKER_NAME_KEYWORD=event-collector [Required]"
echo " env export DOCKER_BIND_PORT=12345 [Required]"
echo " env export [email protected] [Required]"
echo " env export DOCKER_BIND_HOST=127.0.0.1 [Default: 0.0.0.0]"
echo " env export SSH_MAP_LOCAL_HOST=0.0.0.0 [Default: 127.0.0.1]"
echo " env export SSH_MAP_LOCAL_PORT=23456 [Default: Same port in remote]"
return 1
fi
if [[ -z "$DOCKER_BIND_HOST" ]]; then
DOCKER_BIND_HOST=0.0.0.0
fi
COUNT=$(ssh $SSH_MAP_REMOTE_SERVER docker ps | grep "$DOCKER_NAME_KEYWORD" | wc -l)
if [[ $COUNT -ne 1 ]]; then
echo "Found $COUNT containers with name '$DOCKER_NAME_KEYWORD', please speficy a more clear name."
return 1
fi
export SSH_MAP_REMOTE_PORT=$(
ssh $SSH_MAP_REMOTE_SERVER docker ps |
grep "$DOCKER_NAME_KEYWORD" |
grep -Eo "$DOCKER_BIND_HOST:[0-9]+->$DOCKER_BIND_PORT" |
cut -d ':' -f 2 |
grep -Eo '^[0-9]+'
)
if [[ -z "$SSH_MAP_REMOTE_PORT" ]]; then
echo "Cannot find port mapping for $DOCKER_NAME_KEYWORD:$DOCKER_BIND_PORT"
return 1
fi
ssh-map-port-to-local
}
alias z.ssh.agent.ls='ssh-add -l'
alias z.ssh.agent.add='ssh-add'
alias z.ssh.agent.restart='systemctl --user restart ssh-agent'
alias z.ssh.keys.change-passwd='ssh-keygen -p -f'
alias z.ssh.keys.get-public-key='ssh-keygen -y -f'
alias z.ssh.keys.new.ed25519='ssh-keygen -t ed25519 -C'
alias z.ssh.keys.new.rsa.2048='ssh-keygen -t rsa -b 2048 -C'
alias z.ssh.keys.new.rsa.4096='ssh-keygen -t rsa -b 4096 -C'
# [OpenSSL]
alias z.openssl.random='openssl rand -out'
alias z.openssl.x509.show='openssl x509 -noout -text -in'
alias z.openssl.keys.new.rsa.2048='openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048'
alias z.openssl.keys.new.rsa.4096='openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096'
alias z.openssl.keys.new.ecdsa.p256='openssl ecparam -name prime256v1 -genkey'
alias z.openssl.keys.new.ecdsa.p384='openssl ecparam -name secp384r1 -genkey'
alias z.openssl.keys.new.ecdsa.p521='openssl ecparam -name secp521r1 -genkey'
alias z.openssl.keys.new.ed25519='openssl genpkey -algorithm ed25519'
alias z.openssl.keys.new.x25519='openssl genpkey -algorithm X25519'
alias z.openssl.keys.get-public-key='openssl pkey -pubout -in'
ps_mk_color() {
CUR_PS_COLOR=$(shuf -i 1-230 -n 1)
if [ $CUR_PS_COLOR -gt 230 ]; then
CUR_PS_COLOR=1
elif [ "$CUR_PS_COLOR" == "8" ]; then
CUR_PS_COLOR=9
elif [ "$CUR_PS_COLOR" == "15" ]; then
CUR_PS_COLOR=27
elif [ "$CUR_PS_COLOR" == "51" ]; then
CUR_PS_COLOR=63
elif [ "$CUR_PS_COLOR" == "85" ]; then
CUR_PS_COLOR=93
fi
echo -n $CUR_PS_COLOR
}
PS1='\[\e[0;1;38;5;$(ps_mk_color)m\][\t] \u@\H \W$(ps_get_git_branch) \$\[\e[0m\] '
# PS1='\[\e[0;1;38;5;$(ps_mk_color)m\][\t] \u@\H \W \$\[\e[0m\] '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment