From f39193f28f59c21988dfd5f62c70c24b8e0d6c79 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Thu, 17 Nov 2022 20:02:35 +0800 Subject: [PATCH] [core] parse func; gbes; .config/dotfiles/env (#29) * common.sh: arg parse init * bug fix * bug fix * bug fix. now common.sh won't parse arg autoly * improve git-branches * improve git-branches; util-linux * accelerate ci * improve gbes; PARSE_ARG_RET * ci: fix gbes * ssh: add key ltp1.bd * Revert "ssh: add key ltp1.bd" This reverts commit c2433a05499203b0da530a902e51a44e4ae1b4ca. * install column; apt -> apt-get Co-authored-by: xiongdian.me --- .zshrc2 | 8 ++++---- install.sh | 35 ++++++++++++++--------------------- tools/alpine.sh | 2 +- tools/common.sh | 33 ++++++++++++++++++++++++++++----- tools/test.zsh | 4 ++-- tools/ubuntu.sh | 8 ++++---- 6 files changed, 53 insertions(+), 37 deletions(-) diff --git a/.zshrc2 b/.zshrc2 index 4db1ef9..63dc6a3 100644 --- a/.zshrc2 +++ b/.zshrc2 @@ -88,14 +88,14 @@ alias "se"='sudo -sE' alias "pbd"='ping baidu.com' alias "p114"='ping 114.114.114.114' alias "p666"='ping6 2001:da8::666' -alias "git-branches"='git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:short) %(color:red)%(objectname:short) %(color:yellow)%(refname:short)%(color:reset) (%(color:green)%(committerdate:relative)%(color:reset)) %(authorname)"' -sibd() { ssh -p 12022 root@$1${1:+.}ibd.ink } -sob() { ssh -p 24022 root@$1${1:+.}ob.ac.cn } -snasp() { ssh -o ProxyJump="ssh@nasp.ob.ac.cn:36022" dictxiong@$1 } case $(bash "$DOTFILES/tools/common.sh" get_os_type) in macos ) alias l='ls -lAGh -D "%y-%m-%d %H:%M"' ;; * ) alias l='ls -lAGh --time-style="+%y-%m-%d %H:%M"' ;; esac +sibd() { ssh -p 12022 root@$1${1:+.}ibd.ink } +sob() { ssh -p 24022 root@$1${1:+.}ob.ac.cn } +snasp() { ssh -o ProxyJump="ssh@nasp.ob.ac.cn:36022" dictxiong@$1 } +gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX } piv-agent() { eval $(ssh-agent -k) diff --git a/install.sh b/install.sh index e94d4d2..9f354a3 100755 --- a/install.sh +++ b/install.sh @@ -34,18 +34,18 @@ install_dependencies() case $(get_linux_dist) in "ubuntu"|"debian" ) $SUDO apt-get update - $SUDO apt-get install -y git zsh bash tmux vim python3 python3-pip curl inetutils-ping cmake less + $SUDO apt-get install -y git zsh bash tmux vim python3 python3-pip curl inetutils-ping cmake less bsdmainutils ;; "alpine" ) $SUDO apk update - $SUDO apk add zsh bash git tmux vim curl python3 py3-pip fzf iputils coreutils + $SUDO apk add zsh bash git tmux vim curl python3 py3-pip fzf iputils coreutils util-linux ;; * ) fmt_error "dfs auto-install is not implemented on linux distribution: $(get_linux_dist)" esac ;; "macos" ) $SUDO brew update - $SUDO brew install git python3 zsh curl tmux vim + $SUDO brew install git python3 zsh curl tmux vim util-linux ;; "msys" ) pacman -Syu @@ -256,23 +256,16 @@ uninstall(){ fmt_note "done uninstalling!" } -BIN=install -ARG="" -while [[ $# > 0 || -n "$ARG" ]]; do - if [[ -z "$ARG" ]]; then ARG=$1 ORIGIN_ARG=$1; shift; fi - case $ARG in - -i* ) BIN=install ;; - -r* ) BIN=uninstall ;; - -q*|--quite ) export DFS_QUIET=1 ;; - -d*|--dev ) export DFS_DEV=1 ;; - -l*|--lite ) export DFS_LITE=1 ;; - -a*|--auto ) install_dependencies ;; - * ) fmt_warning "unknown command \"$ORIGIN_ARG\". available: -i, -r, -q, -d, -l, -a"; exit 1 ;; +parse_arg "$@" +FUNC=install +for i in ${PARSE_ARG_RET[@]}; do + case $i in + -i ) FUNC=install ;; + -r ) FUNC=uninstall ;; + -d|--dev ) export DFS_DEV=1 ;; + -l|--lite ) export DFS_LITE=1 ;; + -a|--auto ) install_dependencies ;; + * ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a" ;; esac - if [[ "$ARG" == "--"* || ${#ARG} == 2 ]]; then - ARG="" - else - ARG=-${ARG:2} - fi done -$BIN +$FUNC diff --git a/tools/alpine.sh b/tools/alpine.sh index b98b6e1..0879e84 100755 --- a/tools/alpine.sh +++ b/tools/alpine.sh @@ -14,7 +14,7 @@ apk_add() apk update # mass installation - apk add zsh git tmux vim curl wget bash python3 py3-pip htop gcc g++ cmake make fzf perl linux-headers bind-tools iputils man-db coreutils + apk add zsh git tmux vim curl wget bash python3 py3-pip htop gcc g++ cmake make fzf perl linux-headers bind-tools iputils man-db coreutils util-linux #for i in {fzf,ripgrep}; do apk add $i -y; done } diff --git a/tools/common.sh b/tools/common.sh index 525d878..c376518 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -2,11 +2,7 @@ THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd ) - -SUDO='' -if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then - SUDO='sudo' -fi +if [[ -f ~/.config/dotfiles/env ]]; then source ~/.config/dotfiles/env; fi # Color settings # Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh @@ -101,6 +97,31 @@ setup_color() { } # END of color settings +SUDO='' +if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then + SUDO='sudo' +fi + +parse_arg() +{ + local ARG="" + PARSE_ARG_RET=() + while [[ $# > 0 || -n "$ARG" ]]; do + if [[ -z "$ARG" ]]; then ARG=$1; shift; fi + case $ARG in + -q*|--quite ) DFS_QUIET=1 ;; + --* ) PARSE_ARG_RET+=("$ARG") ;; + -* ) PARSE_ARG_RET+=("${ARG:0:2}") ;; + * ) PARSE_ARG_RET+=("$ARG") ;; + esac + if [[ "$ARG" == "--"* || ! "$ARG" == "-"* || ${#ARG} -le 2 ]]; then + ARG="" + else + ARG=-${ARG:2} + fi + done +} + ask_for_yN() { while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do @@ -170,3 +191,5 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then else setup_color fi + +unset THIS_DIR_COMMON_SH diff --git a/tools/test.zsh b/tools/test.zsh index 381f039..634f07e 100644 --- a/tools/test.zsh +++ b/tools/test.zsh @@ -27,9 +27,9 @@ tools/common.sh get_linux_dist # check alias alias p114 -sibd || which sibd +which sibd piv-agent || which piv-agent -git-branches || which git-branches +gbes || which gbes # check update DFS_VERSION=`dfs version` diff --git a/tools/ubuntu.sh b/tools/ubuntu.sh index d15fbf8..c9309f9 100755 --- a/tools/ubuntu.sh +++ b/tools/ubuntu.sh @@ -13,12 +13,12 @@ set_mirror() apt_install() { # basic packages - apt update - for i in {man-db,vim,ca-certificates}; do apt install $i -y; done + apt-get update + for i in {man-db,vim,ca-certificates}; do apt-get install $i -y; done # mass installation - apt install git tmux zsh curl wget dialog net-tools dnsutils netcat traceroute sudo python3 python3-pip cron inetutils-ping openssh-client openssh-server htop gcc g++ cmake make zip less - for i in {fzf,ripgrep}; do apt install $i -y; done + apt-get install git tmux zsh curl wget dialog net-tools dnsutils netcat traceroute sudo python3 python3-pip cron inetutils-ping openssh-client openssh-server htop gcc g++ cmake make zip less bsdmainutils + for i in {fzf,ripgrep}; do apt-get install $i -y; done } set_timezone()