[dev] common.sh parses args; riot; getdfs; install.sh -x KEY=VAL (#35)

* common.sh: export vars in env; zshrc: gdebug

* fix error when locale not exists

* DFS_COLOR

* common.sh: parse args automatically

* ci: test common.sh getopts

* common.sh: argparser supports spaces

* ciot: init

* ciot -> diot; remove sibd, sob and snasp

* rename diot -> riot

* update home0 ssh pubkey; fix ci temperarily

* gdebug will record time

* gdebug supports empty; --dry-run wip

* get.dotfiles.cn

* bug fix (Thu Jan  5 20:53:58 CST 2023)

* fix ci

* fix ci

* install.sh: -d will set -x

* ci: -asl

* getdfs: install for another user using -u <uname>

* try fix when su doesnot exist

* bug fix (Thu Jan  5 22:58:40 CST 2023)

* bug fix (Thu Jan  5 22:59:33 CST 2023)

* introduce SUDOE and so debug

* ask_for_yn now use stdout to return

* getdfs: support multiple users

* install.sh: -x to set dfs config; ci

* fix ci

* bug fix (Fri Jan  6 15:11:24 CST 2023)

* auto-detect DFS_NO_WALL

* bug fix (Fri Jan  6 15:43:25 CST 2023)

* getdfs: ${repo}

* bug fix (Fri Jan  6 16:08:41 CST 2023)

* getdfs: prompt user

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
This commit is contained in:
2023-01-06 16:14:41 +08:00
committed by GitHub
parent 952bfabea4
commit 9a4c9556f6
17 changed files with 350 additions and 113 deletions

View File

@@ -15,7 +15,7 @@ apk_add()
# lite
apk add zsh bash git tmux vim curl fzf iputils coreutils util-linux
# full
if [[ -z "$DFS_LITE" ]]; then
if [[ -z "$DFS_LITE" || "$DFS_LITE" == "0" ]]; then
apk add wget python3 py3-pip htop gcc g++ cmake make perl linux-headers bind-tools man-db
fi
}
@@ -34,8 +34,8 @@ router()
apk-add ) apk_add ;;
set-timezone | set-tz ) set_timezone ;;
set-mirror ) set_mirror $2 ;;
* ) echo unknown command "$1". available: apk-add, set-timezone;;
* ) echo unknown command \"$1\". available: apk-add, set-timezone;;
esac
}
router $@
router "${GOT_OPTS[@]}"

View File

@@ -4,9 +4,37 @@ THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi
# parse args and set env, when it is sourced
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
ORIGIN_ARGS=("$@")
ARG=""
GOT_OPTS=()
while [[ $# > 0 || -n "$ARG" ]]; do
if [[ -z "$ARG" ]]; then ARG=$1; shift; fi
case $ARG in
-q*|--quite ) export DFS_QUIET=1 ;;
-l*|--lite ) export DFS_LITE=1 ;;
--color ) export DFS_COLOR=1 ;;
--dry-run ) export DFS_DRY_RUN=1 ;; # TODO!!!
--*=* ) GOT_OPTS+=("${ARG%%=*}" "${ARG#*=}") ;;
--* ) GOT_OPTS+=("$ARG") ;;
-* ) GOT_OPTS+=("${ARG:0:2}") ;;
* ) GOT_OPTS+=("$ARG") ;;
esac
if [[ "$ARG" == "--"* || ! "$ARG" == "-"* || ${#ARG} -le 2 ]]; then
ARG=""
else
ARG=-${ARG:2}
fi
done
set -- "${ORIGIN_ARGS[@]}"
unset ARG
unset ORIGIN_ARGS
fi
# Color settings
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
if [ -t 1 ]; then
if [[ -t 1 || "$DFS_COLOR" == "1" ]]; then
is_tty() {
true
}
@@ -98,52 +126,36 @@ setup_color() {
# END of color settings
SUDO=''
SUDOE=''
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
SUDO='sudo'
SUDOE='sudo -E'
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 ) export 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
if [[ "$DFS_QUIET" == "1" ]]; then
echo 0
else
read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
case $yn in
[Yy]* ) return 1;;
* ) return 0;;
[Yy]* ) echo 1;;
* ) echo 0;;
esac
done
return 0
fi
}
ask_for_Yn()
{
while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do
if [[ "$DFS_QUIET" == "1" ]]; then
echo 1
else
read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn
case $yn in
[Nn]* ) return 0;;
* ) return 1;;
[Nn]* ) echo 0;;
* ) echo 1;;
esac
done
return 1
fi
}
post_log()

62
tools/get.dotfiles.cn Normal file
View File

@@ -0,0 +1,62 @@
#!/bin/bash
set -e
ARG=""
GOT_OPTS=()
DFS_USER="$(whoami)"
while [[ $# > 0||-n "$ARG" ]];do
if [[ -z "$ARG" ]];then ARG=$1;shift;fi
case $ARG in
-s*|--secure)DFS_SECURE=1;;
-u*|--user)if [[ "$ARG" == --*=* ]];then
DFS_USER="${ARG#*=}"
else
DFS_USER=$1
shift
fi
if [[ -z "$DFS_USER" ]];then
echo "install user is required"
exit 1
fi;;
--*=*)GOT_OPTS+=("${ARG%%=*}" "${ARG#*=}");;
--*)GOT_OPTS+=("$ARG");;
-*)GOT_OPTS+=("${ARG:0:2}");;
*)GOT_OPTS+=("$ARG");;
esac
if [[ "$ARG" == "--"*||! "$ARG" == "-"*||${#ARG} -le 2 ]];then
ARG=""
else
ARG=-${ARG:2}
fi
done
IFS=',' read -r -a DFS_USERS<<<"$DFS_USER"
for u in "${DFS_USERS[@]}";do
if [[ -z "$u" ]];then
continue
fi
if ! id -u "$u" >/dev/null 2>&1;then
echo "user $u not exists"
exit 1
fi
if [[ "$u" == "$(whoami)" ]];then
SUCMD="bash"
else
SUCMD="su $u"
fi
echo "install for user $u in 3 seconds ..."
sleep 3
$SUCMD<<EOF
cd
if [[ ! -f ~/dotfiles/install.sh ]];then
git clone ${repo}
fi
if [[ "$DFS_SECURE" == "1" ]];then
echo "enter secure mode"
cd dotfiles
git fetch --all
git -c advice.detachedHead=false checkout $(curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id)
cd
fi
./dotfiles/install.sh ${GOT_OPTS[@]}
zsh -c "source ~/.zshrc"
EOF
done

View File

@@ -79,22 +79,27 @@ print_help()
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
}
if [[ $# != 2 ]]; then
print_help >&2
exit 1
fi
router()
{
if [[ $# != 2 ]]; then
print_help >&2
exit 1
fi
case "$1" in
-h|--help)
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
;;
beacon)
post_beacon "$2"
;;
log)
post_log "$2"
;;
*)
fmt_fatal "invalid argument"
;;
esac
case "$1" in
-h|--help)
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
;;
beacon)
post_beacon "$2"
;;
log)
post_log "$2"
;;
*)
fmt_fatal "invalid argument"
;;
esac
}
router "${GOT_OPTS[@]}"

View File

@@ -5,7 +5,7 @@ source "$THIS_DIR/common.sh"
brew_install()
{
brew update
# brew update
brew install git zsh curl tmux vim util-linux
}
@@ -13,8 +13,8 @@ router()
{
case $1 in
brew-install ) brew_install ;;
* ) echo unknown command "$1". available: brew-install;;
* ) echo unknown command \"$1\". available: brew-install;;
esac
}
router $@
router "${GOT_OPTS[@]}"

View File

@@ -17,7 +17,7 @@ pacman_S()
# lite
pacman -S tmux git zsh bash curl vim
# full
if [[ -z "$DFS_LITE" ]]; then
if [[ -z "$DFS_LITE" || "$DFS_LITE" == "0" ]]; then
pacman -S wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip man-pages-posix
fi
}
@@ -27,8 +27,8 @@ router()
case $1 in
pacman-S ) pacman_S ;;
set-mirror ) set_mirror $2 ;;
* ) echo unknown command "$1". available: pacman-S, set-mirror ;;
* ) echo unknown command \"$1\". available: pacman-S, set-mirror ;;
esac
}
router $@
router "${GOT_OPTS[@]}"

21
tools/test-getopts.sh Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
set -ex
OPTS='-a -bcl --color --arg1=1 --arg2 2 " 1 2" yes'
TARGET_OPTS='-a -b -c --arg1 1 --arg2 2 1 2 yes'
eval set -- $OPTS
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
test "${GOT_OPTS[*]}" = "$TARGET_OPTS"
test $# -eq 8
test "$*" = "${OPTS//\"/}"
test "$DFS_LITE" = "1"
is_tty
test -z "$DFS_QUIET"
set +x
echo "test passed, args:"
for i in "${GOT_OPTS[@]}"; do
echo "$i"
done

View File

@@ -23,12 +23,13 @@ test ~ -ef "$(pwd)"
dogo
doll
dfs cd
./tools/test-getopts.sh
tools/common.sh get_os_type
tools/common.sh get_linux_dist
# check alias
alias p114
which sibd
which riot
piv-agent || which piv-agent
gbes || which gbes
@@ -42,7 +43,9 @@ test `git rev-parse HEAD` = `curl -fsSL https://api.beardic.cn/get-var/dfs-commi
git reset --hard $DFS_VERSION
# then check install.sh
./install.sh -l
./install.sh -dx DFS_CI=1
grep -q "DFS_CI=1" ~/.config/dotfiles/env
./install.sh -l -x DFS_CI=1
dfs version
test `git rev-parse HEAD` = `curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id`

View File

@@ -16,7 +16,7 @@ apt_install()
# lite
apt-get install -y git zsh bash tmux vim curl inetutils-ping less bsdmainutils
# full
if [[ -z "$DFS_LITE" ]]; then
if [[ -z "$DFS_LITE" || "$DFS_LITE" == "0" ]]; then
apt-get install wget dialog net-tools dnsutils netcat traceroute sudo python3 python3-pip cron openssh-client openssh-server htop gcc g++ cmake make zip
for i in {fzf,ripgrep,man-db}; do apt-get install -y $i; done
fi
@@ -35,8 +35,8 @@ router()
set-mirror ) set_mirror $2 ;;
set-timezone\
| set-tz ) set_timezone $2 ;;
* ) echo unknown command "$1". available: apt-install, set-mirror, set-timezone;;
* ) echo unknown command \"$1\". available: apt-install, set-mirror, set-timezone;;
esac
}
router $@
router "${GOT_OPTS[@]}"