dotar/zsh/functions.zsh

307 lines
6.9 KiB
Bash
Raw Normal View History

2017-03-08 22:55:07 +08:00
function secure_source () {
if [ -e $1 ]; then
source $1
fi
}
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
function getIP () {
echo $(grep $1 ~/.ssh/config -A 1 | tail -1 | tr -s ' ' | cut -d ' ' -f 3)
}
function getServerName () {
echo $(grep $1 ~/.ssh/config -B 1 | head -1 | tr -s ' ' | cut -d ' ' -f 3)
}
function ssh_exec_by_file () {
ssh -t $1 "bash -s" -- < $2
}
function cp_container() {
sourceName=$1
targetName=$2
cp $sourceName $targetName -r
cd $targetName
mv ${sourceName}.scss ${targetName}.scss
mv ${sourceName}.js ${targetName}.js
cd ..
}
function set_proxy() {
2018-06-26 18:12:49 +08:00
export http_proxy=http://127.0.0.1:8119;export https_proxy=http://127.0.0.1:8119;
2017-03-08 22:55:07 +08:00
}
function set_ss_proxy() {
export https_proxy=socks5://127.0.0.1:1080/
export http_proxy=socks5://127.0.0.1:1080/
}
function unset_proxy() {
2017-03-21 11:49:16 +08:00
unset all_proxy
2017-03-08 22:55:07 +08:00
}
function post {
curl -H "Content-Type: application/json" -X POST -d $1 $2
}
function git-set-remote {
git remote rm origin
git remote rm upstream
git remote add origin $1
git remote add upstream $1
}
function search_installed_packages {
dpkg --get-selections | grep $1
}
function delete_packages {
sudo apt remove `_search_installed_packages $1 | cut -f 1 | tr "\n" " "`
}
function gem_source_to_taobao {
gem source -r https://rubygems.org/
gem source -a https://ruby.taobao.org
}
function set_anonymous {
git filter-branch --env-filter '
if [ "$GIT_AUTHOR_NAME" = "ralletstellar" ]; then \
export GIT_AUTHOR_NAME="Anonymous" GIT_AUTHOR_EMAIL="anonymous@anonymous.com"; \
fi
'
}
function genAndroidKeystore {
keytool -genkey -v -keystore /tmp/.keystore -alias rallets -keyalg RSA -keysize 2048 -validity 10000
}
function delete_except_latest {
total=`ll | wc -l`
num_to_delete=$((total-$1))
ls -t | tail -n $num_to_delete | xargs rm
}
function strip_color() {
sed "s,$(printf '\033')\\[[0-9;]*[a-zA-Z],,g"
}
function docker_rm_all() {
docker rm -f `docker ps --no-trunc -aq`
}
function Replace () {
2017-12-04 16:01:42 +08:00
if [[ "$(uname)" == "Darwin" ]]; then
if [ "$#" -eq 3 ]; then
ag $2 -l -G "$1" | xargs sed -i '' s/$2/$3/g
elif [ "$#" -eq 2 ]; then
ag $1 -l | xargs sed -i '' s/$1/$2/g
fi
2017-12-04 16:01:42 +08:00
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
if [ "$#" -eq 3 ]; then
ag $2 -l -G $1 | xargs sed -i s/$2/$3/g
elif [ "$#" -eq 2 ]; then
ag $1 -l | xargs sed -i s/$1/$2/g
fi
2017-03-08 22:55:07 +08:00
fi
}
function git-change-module-remote() {
git config --file=.gitmodules submodule.$1.url $2
git config --file=.gitmodules submodule.$1.branch $3
git submodule sync
git submodule update --init --recursive --remote
}
function replace() {
ag -l -G $1 | xargs sed -ri.bak -e "s/$2/$3/g"
}
function markdown-preview() {
cat $1 | instant-markdown-d > /dev/null 2>&1
}
function swap() {
if [ $# -ne 2 ]; then
echo "Usage: swap file1 file2"
else
local TMPFILE=$(mktemp)
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
fi
}
2018-12-04 14:05:34 +08:00
function init_db() {
dc exec $1 rails db:drop db:create db:migrate db:seed
}
function cmd_exists() {
cmd=$1
$cmd &> /dev/null;
if [[ $? == 0 ]]; then
echo Y
else
echo N
fi
}
function qshell_setup() {
if [[ $(cmd_exists qshell) == 'N' ]]; then
curl https://raw.githubusercontent.com/paiyou-network/scripts/master/install-qshell.sh | bash
fi
~/Projects/paiyou-hub/bin/setup-qshell-account.sh
}
function qshell_upload() {
qshell_setup
bucket=assets
while getopts "b:f:k:t" o; do
2019-07-23 16:40:37 +08:00
case "${o}" in
b)
bucket=${OPTARG}
;;
f)
filepath=${OPTARG}
;;
k)
key=${OPTARG}
;;
t)
timestamp=true
;;
2019-07-23 16:40:37 +08:00
*)
echo "Usage: qshell_upload [-b BUCKET] -f FILEPATH [-k KEY] [-t]"
2019-07-23 16:40:37 +08:00
return
;;
esac
done
if [[ $key == '' ]]; then
key=$(basename $filepath)
fi
if [[ $timestamp == 'true' ]];then
key=`date +%Y%m%dT%H%M%S`_${key}
fi
qshell rput $bucket $key $filepath
2019-07-23 16:40:37 +08:00
}
function gitcopy() {
n=1
while getopts "c:n:t:T" o; do
2019-06-18 19:35:57 +08:00
case "${o}" in
c)
commit=${OPTARG}
;;
n)
n=${OPTARG}
;;
t)
trelloCardName=${OPTARG}
;;
T)
trelloCardName=`git log $commit --pretty="✔️ %s" | head -n $n`
;;
2019-06-18 19:35:57 +08:00
*)
usage
;;
esac
done
prefix=`git remote get-url origin | sed -E 's/git@github.com:/https:\/\/github.com\//g' | sed -E 's/(.*)\.git/\1/'`
project_name=`echo $prefix | sed -E 's/.*\/(.*)/\1/'`
commits=`git log $commit -n $n --stat --pretty="
* [$project_name]($prefix/commit/%H) %an: **%s**" | sed 's/^[^*]/> /'`
2019-07-15 19:42:00 +08:00
echo $commits
which pbcopy &> /dev/null
if [[ $? == '0' ]]; then
echo $commits | pbcopy
fi
2019-06-18 19:35:57 +08:00
if [[ "$trelloCardName" != '' ]]; then
ruby ~/Projects/paiyou-hub/bin/create_trello_todo_card.rb -n $trelloCardName -d "$commits" -l `git config user.name`
2019-06-18 19:35:57 +08:00
fi
2019-01-18 16:48:53 +08:00
}
2019-03-12 16:30:22 +08:00
function h() {
helm $* --tls
}
2019-03-21 08:44:53 +08:00
function kexec {
2019-06-14 12:33:09 +08:00
RAN=false
while getopts ":rp:" opt; do
case "${opt}" in
r)
RAN=true
;;
p)
PROJECT=$OPTARG
;;
*)
echo "Usage: cmd [-h]"
return
;;
esac
done
shift $(($OPTIND - 1))
echo 'waiting...'
while true; do
2019-06-14 12:33:09 +08:00
POD_NAME=`k get pods | grep $PROJECT | awk '{print $1}'`
POD_NAMES=("${(f)POD_NAME}")
echo ${POD_NAMES[@]}
2019-06-11 19:20:08 +08:00
OK=true
for i in $POD_NAMES; do
2019-06-14 12:33:09 +08:00
READY=`k get pods $i | grep $PROJECT | awk '{print $2}'`
READYS=("${(@s|/|)READY}")
2019-06-14 12:33:09 +08:00
STATE=`k get pods $i | grep $PROJECT | awk '{print $3}'`
2019-06-11 19:20:08 +08:00
if [[ ${READYS[1]} -eq 0 || "${STATE}" -ne "Running" ]]; then
OK=false
break
fi
done
2019-06-14 12:33:09 +08:00
if [[ $OK == 'false' ]];then
2019-06-11 19:20:08 +08:00
sleep 2
continue
fi
2019-06-14 12:33:09 +08:00
if [[ $RAN == 'true' ]];then
input=`shuf -i 1-${#POD_NAMES[@]} -n 1`
break
fi
2019-06-11 19:20:08 +08:00
if [ ${#POD_NAMES[@]} -eq 1 ];then
input=1
break
2019-06-11 19:20:08 +08:00
elif [ ${#POD_NAMES[@]} -gt 1 ];then
echo 'Pods:'
INDEX=1
2019-06-11 19:20:08 +08:00
for i in $POD_NAMES;do
echo "[$INDEX] $i"
let INDEX=${INDEX}+1
done
echo "select option to execute: "
while true;do
read input
2019-06-11 19:20:08 +08:00
if [[ $input -gt 0 && $input -le ${#POD_NAMES[@]} ]];then
break
else
echo 'invalid option...'
fi
done
break
fi
done
2019-06-14 12:33:09 +08:00
echo "executing pod $POD_NAMES[$input]..."
2019-06-11 19:20:08 +08:00
kubectl exec -it $POD_NAMES[$input] $@
2019-03-21 08:44:53 +08:00
}
2019-03-25 17:33:45 +08:00
function klogs {
TEXT=$1
POD_NAME=`k get pods | grep $TEXT | awk '{print $1}'`
kubectl logs -f $POD_NAME
}
function rgm {
args=("${(@s/,/)1}")
regex=${(j:.*\n.*:)args}
echo "Rip Search with $regex..."
rg -U $regex
}
function dc {
if [ -e ./docker-compose-dev.yml ]; then
docker_file_path=./docker-compose-dev.yml
elif [ -e ./docker-compose.yml ]; then
docker_file_path=./docker-compose.yml
else
2019-07-16 11:45:26 +08:00
docker_file_path=~/Projects/paiyou-hub/docker-compose-dev.yml
fi
docker-compose -f $docker_file_path $@
}