Skip to content

Instantly share code, notes, and snippets.

@kvpbldsck
Last active July 8, 2022 09:42
Show Gist options
  • Save kvpbldsck/b151a86bc94dd676265731f179b53101 to your computer and use it in GitHub Desktop.
Save kvpbldsck/b151a86bc94dd676265731f179b53101 to your computer and use it in GitHub Desktop.
Base scripts
  1. Расположить все файлы в ~/scripts/.
  2. Создать файл ~/scripts/constants.sh в который записать код установки переменных DEFAULT_PROJECT_PATH, EMAIL, MAIN
  3. Создать файл ~/scripts/constants.ps1 в который записать код установки переменных env:DEFAULT_PROJECT_PATH
  4. В bash выполнить
  echo . ~/scripts/constants.sh > ~/.bashrc
  echo . ~/scripts/base.sh >> ~/.bashrc
  1. В PowerShell выполнить
  New-Item $profile -Type File -Force
  Add-Content $profile '~/scripts/constants.ps1'
  Add-Content $profile 'Import-Module ~/scripts/base.psm1'
function hi {
Set-Location $env:DEFAULT_PROJECT_PATH
}
export DEFAULT_IFS=$'\n\t'
function check_variable() {
if [[ ! -v $1 ]]
then
echo Variable $1 isn\'t set
fi
}
check_variable DEFAULT_PROJECT_PATH
check_variable EMAIL
check_variable MAIN
set -Eo pipefail
IFS=$DEFAULT_IFS
# git aliases and functions
alias default_branch="git remote show origin | sed -n '/HEAD branch/s/.*: //p'"
alias root='git checkout $MAIN; git pull origin $MAIN;'
alias commits='git log --pretty=format:"%h - %an, %ar : %s" -20'
alias graph='git log --pretty=format:"%h %s" --graph -20'
alias branches='git branch -l'
alias branch='git branch --show-current'
alias stash='git stash --include-untracked' # stash -m "<stash message>"
alias unstash='git stash apply'
alias fdiff='git diff --name-only'
alias acom='git add *; git commit'
alias push='git push origin $(git branch --show-current)'
alias forget='git update-index --assume-unchanged' # forget <path-to-file>
alias unforget='git update-index --no-assume-unchanged'
function pull() {
local mode=$1
case "$mode" in
m | -m)
git pull origin $MAIN
;;
M | -M)
git pull origin MAIN
;;
c | -c)
git pull origin $(git branch --show-current)
;;
a | -a)
pull -m
pull -c
;;
*)
pull -c
;;
esac
}
function list_branches() {
local template=$1
git branch -a --list "*$template*" | sed 's\* \\g; s\remotes/origin/\\g; s\ \\' | uniq
}
function clear_branches() {
local branches=$(git branch | sed 's\* \\g; s\remotes/origin/\\g; s\ \\' | uniq)
for b in $branches
do
if [[ "$b" != "$MAIN" && "$b" != "MAIN" ]]
then
git branch -d $b
fi
done
}
function ggo() {
local template=$1
local need_pull=${2+$2} # Safe way to get arg or empty string
local branch=$(list_branches $template | sed '2,$d')
if [[ -z $branch ]]
then
echo no branch found by your pattern
return
fi
git checkout $branch
if [[ -n $need_pull ]]
then
pull $need_pull
fi
}
# docker related functions
function run_docker() {
local path=$1
local container_name=${$2:-container-$RANDOM}
echo run container $container_name
docker build --tag $container_name-image $path
docker run --name $container_name $container_name-image
}
function run_compose() {
if [[ -v $1 ]]
then
local config_file="docker-compose.$1.yml"
else
local config_file="docker-compose.yml"
fi
docker-compose -f $config_file up --force-recreate --build --remove-orphans --renew-anon-volumes
}
# other aliases and functions
alias hi='cd $DEFAULT_PROJECT_PATH; export MAIN=`default_branch`'
function ssh_gen() {
local pub_key=~/.ssh/id_rsa.pub
if [[ ! -e $pub_key ]]
then
ssh-keygen -t rsa -b 4096 -C $EMAIL
fi
cat $pub_key
}
function echoerr() {
echo "$@" 1>&2;
}
function unset_global() {
local variable=$1
sed -i -e "/export $variable=/d" ~/scripts/constants.sh
export -n "$variable"
}
function set_global() {
local variable=${1%=*}
local value=${1#*=}
local full_set_statement=$1
unset_global $variable
echo export $full_set_statement >> ~/scripts/constants.sh
export "$variable"="$value"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment