[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:
Dict Xiong 2023-01-06 16:14:41 +08:00 committed by GitHub
parent 952bfabea4
commit 9a4c9556f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 350 additions and 113 deletions

View File

@ -17,7 +17,7 @@ jobs:
rev=`git rev-parse HEAD` rev=`git rev-parse HEAD`
pwd pwd
set -x set -x
./install.sh -adl DFS_NO_WALL=0 ./install.sh -adl
test `git rev-parse HEAD` = "$rev" test `git rev-parse HEAD` = "$rev"
- name: antigen build - name: antigen build
@ -31,13 +31,11 @@ jobs:
run: | run: |
source tools/test.zsh source tools/test.zsh
antigen reset antigen reset
rm -rf $ANTIGEN $HOME/.antigen rm -rf $ANTIGEN $HOME/.antigen $HOME/.config/dotfiles
./install.sh -dl ./install.sh -dl
- name: antigen build with DFS_NO_WALL - name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0} shell: /bin/zsh -ileo PIPE_FAIL {0}
env:
DFS_NO_WALL: 1
run: | run: |
echo $SHELL echo $SHELL
antigen list antigen list
@ -60,7 +58,7 @@ jobs:
rev=`git rev-parse HEAD` rev=`git rev-parse HEAD`
pwd pwd
set -x set -x
./install.sh -adl DFS_NO_WALL=0 ./install.sh -adl
test `git rev-parse HEAD` = "$rev" test `git rev-parse HEAD` = "$rev"
- name: antigen build - name: antigen build
@ -74,13 +72,11 @@ jobs:
run: | run: |
source tools/test.zsh source tools/test.zsh
antigen reset antigen reset
rm -rf $ANTIGEN $HOME/.antigen rm -rf $ANTIGEN $HOME/.antigen $HOME/.config/dotfiles
./install.sh -dl ./install.sh -dl
- name: antigen build with DFS_NO_WALL - name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0} shell: /bin/zsh -ileo PIPE_FAIL {0}
env:
DFS_NO_WALL: 1
run: | run: |
echo $SHELL echo $SHELL
antigen list antigen list
@ -109,10 +105,27 @@ jobs:
./install.sh -dal ./install.sh -dal
test `git rev-parse HEAD` = "$rev" test `git rev-parse HEAD` = "$rev"
- name: antigen build with DFS_NO_WALL - name: antigen build
shell: /bin/zsh -ileo PIPE_FAIL {0} shell: /bin/zsh -ileo PIPE_FAIL {0}
env:
DFS_NO_WALL: 1
run: | run: |
echo $SHELL echo $SHELL
antigen list antigen list
test-get-dotfiles-cn:
name: test of get.dotfiles.cn script
runs-on: ubuntu-latest
steps:
- name: checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: run the script
run: |
repo=https://github.com/DictXiong/dotfiles bash tools/get.dotfiles.cn -asl
- name: run tests
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: |
dfs cd
source tools/test.zsh

View File

@ -1,5 +1,3 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQD5VPD5c/pBtQM5mcKMzJ6vC2jDdVJU6ifkP1EvAeV/dKEyy5hOc+EXC8Ws7bAp3a7twvsQGSqNbeXuAO4PzX2KHhI49KiFyRugo8LsUF3TVVluOXtGWKgw+36TBO3rNFgWOnhw5B4IlNtxo2nk4grCkgEsE6w+Ignd+FrEFec457oal2SPN02TjUfRULaEPSpZTl7cA/5Re9/QmlcYjuyA9zbkO98KYA0M71W/eUcjfjNEaWXjn2pcloakoWwEEdi4v/yJ1Fk+gQYcbpoftxpWmWLvFhDGP+qvTZY5QHxDxiPbc3HDdx0qBvMOQW9XSt0J/L/btnxcZvA1lDtkC2bm8zvHxtLBZ9JBRutMbRhdSgkd3+5DwHDIO0EIUqckgCfX3t7avj5RsPAl0j4V40yB637YQim7saN3FWq43SpR3i6buNbQLPdgy2SDN6fAZcAPWvifE1KG7I2BtmiFFYjAy3iU1KD2Hgn6mIpOOCZOJV30/cf3Oq8ZSDfD7PmDu72yOea3h9JlvkKjjqknnF4NdQ5cy1FuIfyebnF6+ugbdA9UqY9PZdnuwy5RkwkmJwC4AVO3GaOfuQDU61UMSoTfYH1yB8x8HHUQcJzLtfExrYpVPAo89tyN1lcIIBUBoHhNMCwVBZipfV/H41cIxs5wkHsolQ02zZaAX6Cgbii9FQ== home
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPmMImRSAhksHUfCwNpcj61izGn0Xkv1KRIuxb0OSdJl pc0/old ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPmMImRSAhksHUfCwNpcj61izGn0Xkv1KRIuxb0OSdJl pc0/old
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO4K5S3zDYbxlw7B45zOdmKud95nXU1V6kSxEvbTOsy2 pc1/old ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO4K5S3zDYbxlw7B45zOdmKud95nXU1V6kSxEvbTOsy2 pc1/old
@ -30,3 +28,5 @@ ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJ/+BWST
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNvHya5i3GNniGww9Yx5ikos4Z5jevu8zz8hCQi05++SgejFPnbw9UjbHFjODK2M+ZHuhRF5mdf996r4/BxWyP8= pad0/sep ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNvHya5i3GNniGww9Yx5ikos4Z5jevu8zz8hCQi05++SgejFPnbw9UjbHFjODK2M+ZHuhRF5mdf996r4/BxWyP8= pad0/sep
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC5gu+U4picxVvCNYjLlmxGLpJl7svj3LklO7jSpwqdrNxlE+/xkx07PJx76AbA77dDM1Dxmm/VOvVQCKTRLTgk= ip14/sep ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC5gu+U4picxVvCNYjLlmxGLpJl7svj3LklO7jSpwqdrNxlE+/xkx07PJx76AbA77dDM1Dxmm/VOvVQCKTRLTgk= ip14/sep
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF52mPmbLEriZ+DfmrMHFtvg2kiO2JpRpitvs7PbJowh home0

View File

@ -35,7 +35,7 @@ if [[ "$(git rev-parse HEAD)" == "$DFS_COMMIT" ]]; then
fmt_info "nothing to do" fmt_info "nothing to do"
else else
fmt_info "checking out to commit $DFS_COMMIT ..." fmt_info "checking out to commit $DFS_COMMIT ..."
if [[ -z "$DFS_DEV" ]]; then if [[ -z "$DFS_DEV" || "$DFS_DEV" == "0" ]]; then
post_beacon "dfs.updated" post_beacon "dfs.updated"
git -c advice.detachedHead=false checkout $DFS_COMMIT git -c advice.detachedHead=false checkout $DFS_COMMIT
cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit $DFS_UPDATED_RET cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit $DFS_UPDATED_RET

25
.zshrc2
View File

@ -14,17 +14,11 @@ export EDITOR='vim'
export PYTHONIOENCODING='UTF-8' export PYTHONIOENCODING='UTF-8'
export GPG_TTY=$(tty) export GPG_TTY=$(tty)
export LESS_TERMCAP_md=$'\E[01;33m' export LESS_TERMCAP_md=$'\E[01;33m'
# env for dfs
if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi
# antigen # antigen
if [[ -z "$DFS_NO_WALL" ]]; then if [[ "$DFS_NO_WALL" == "1" ]]; then
ANTIGEN_URL="https://gitee.com/dictxiong/antigen/raw/develop/bin/antigen.zsh"
ANTIGEN_OMZ_REPO_URL="https://gitee.com/dictxiong/ohmyzsh.git"
ANTIGEN_PLUGINS=(
"https://gitee.com/dictxiong/zsh-syntax-highlighting"
"https://gitee.com/dictxiong/zsh-completions"
"https://gitee.com/dictxiong/zsh-autosuggestions"
)
else
ANTIGEN_URL="https://raw.githubusercontent.com/zsh-users/antigen/develop/bin/antigen.zsh" ANTIGEN_URL="https://raw.githubusercontent.com/zsh-users/antigen/develop/bin/antigen.zsh"
ANTIGEN_OMZ_REPO_URL="https://github.com/ohmyzsh/ohmyzsh.git" ANTIGEN_OMZ_REPO_URL="https://github.com/ohmyzsh/ohmyzsh.git"
ANTIGEN_PLUGINS=( ANTIGEN_PLUGINS=(
@ -32,6 +26,14 @@ else
"https://github.com/zsh-users/zsh-completions" "https://github.com/zsh-users/zsh-completions"
"https://github.com/zsh-users/zsh-autosuggestions" "https://github.com/zsh-users/zsh-autosuggestions"
) )
else
ANTIGEN_URL="https://gitee.com/dictxiong/antigen/raw/develop/bin/antigen.zsh"
ANTIGEN_OMZ_REPO_URL="https://gitee.com/dictxiong/ohmyzsh.git"
ANTIGEN_PLUGINS=(
"https://gitee.com/dictxiong/zsh-syntax-highlighting"
"https://gitee.com/dictxiong/zsh-completions"
"https://gitee.com/dictxiong/zsh-autosuggestions"
)
fi fi
ANTIGEN="$HOME/antigen.zsh" ANTIGEN="$HOME/antigen.zsh"
# Install antigen.zsh if not exist # Install antigen.zsh if not exist
@ -100,7 +102,7 @@ alias "se"='sudo -sE'
alias "pbd"='ping baidu.com' alias "pbd"='ping baidu.com'
alias "p114"='ping 114.114.114.114' alias "p114"='ping 114.114.114.114'
alias "p666"='ping6 2001:da8::666' alias "p666"='ping6 2001:da8::666'
alias "gdebug"='git add -A; git commit -m "bug fix"' alias "gdebug"='git add -A; git commit --allow-empty -m "bug fix ($(date))"'
case $(bash "$DOTFILES/tools/common.sh" get_os_type) in case $(bash "$DOTFILES/tools/common.sh" get_os_type) in
macos ) alias l='ls -lAGh -D "%y-%m-%d %H:%M"' ;; macos ) alias l='ls -lAGh -D "%y-%m-%d %H:%M"' ;;
* ) alias l='ls -lAGh --time-style="+%y-%m-%d %H:%M"' ;; * ) alias l='ls -lAGh --time-style="+%y-%m-%d %H:%M"' ;;
@ -108,9 +110,6 @@ esac
if [[ -x $(command -v trash) ]]; then if [[ -x $(command -v trash) ]]; then
alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash" alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash"
fi fi
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 } 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() piv-agent()
{ {

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/tools/common.sh" source "$THIS_DIR/tools/common.sh"
@ -10,6 +10,7 @@ fi
DOTFILE_TILDE=${DOTFILES/"$HOME"/\~} DOTFILE_TILDE=${DOTFILES/"$HOME"/\~}
CRON_JOB="0 * * * * ${DOTFILES}/update.sh" CRON_JOB="0 * * * * ${DOTFILES}/update.sh"
declare -a DFS_CONFIGS
declare -a HOME_FILES_PATH declare -a HOME_FILES_PATH
declare -a HOME_FILES_CONTENT declare -a HOME_FILES_CONTENT
HOME_FILES_PATH[0]=".zshrc" HOME_FILES_PATH[0]=".zshrc"
@ -28,22 +29,32 @@ HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
install_dependencies() install_dependencies()
{ {
local ret=0
fmt_note "installing dependencies ..." fmt_note "installing dependencies ..."
set +e
case $(get_os_name) in case $(get_os_name) in
"ubuntu"|"debian" ) "ubuntu"|"debian" )
$SUDO "$DOTFILES/tools/ubuntu.sh" apt-install $SUDOE "$DOTFILES/tools/ubuntu.sh" apt-install
ret=$?
;; ;;
"alpine" ) "alpine" )
$SUDO "$DOTFILES/tools/alpine.sh" apk-add $SUDOE "$DOTFILES/tools/alpine.sh" apk-add
ret=$?
;; ;;
"macos" ) "macos" )
"$DOTFILES/tools/macos.sh" brew-install "$DOTFILES/tools/macos.sh" brew-install
ret=$?
;; ;;
"msys" ) "msys" )
"$DOTFILES/tools/msys2.sh" pacman-S "$DOTFILES/tools/msys2.sh" pacman-S
ret=$?
;; ;;
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name)" * ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name). skipping ..."
esac esac
set -e
if [[ "$ret" != "0" ]]; then
fmt_error "failed to install dependencies."
fi
} }
preinstall_check() preinstall_check()
@ -61,8 +72,8 @@ preinstall_check()
for i in "${optional_commands[@]}"; do for i in "${optional_commands[@]}"; do
if ! command -v $i 1>/dev/null; then if ! command -v $i 1>/dev/null; then
fmt_warning "\"$i\" not found" fmt_warning "\"$i\" not found"
ask_for_Yn "continue anyway?" yn=$(ask_for_Yn "continue anyway?")
if [[ "$?" == "0" ]]; then if [[ "$yn" == "0" ]]; then
fmt_info "all this utils are suggested: ${optional_commands[@]}" fmt_info "all this utils are suggested: ${optional_commands[@]}"
fmt_info "install them manually or check scripts in tools/" fmt_info "install them manually or check scripts in tools/"
fmt_fatal "aborting ..." fmt_fatal "aborting ..."
@ -71,12 +82,39 @@ preinstall_check()
done done
} }
prepare_config()
{
local remote=$(cd "$DOTFILES" && git remote get-url origin)
if [[ -z "$DFS_NO_WALL" && $remote == *github* ]]; then
DFS_CONFIGS+=("DFS_NO_WALL=1")
fi
if [[ ${#DFS_CONFIGS[@]} == 0 ]]; then
return
fi
fmt_note "preparing dotfiles configurations ..."
local key value
for i in "${DFS_CONFIGS[@]}"; do
if [[ "$i" == *"="* ]]; then
key=${i%%=*}
value=${i#*=}
else
key=$i
value=$(eval echo \$$key)
fi
HOME_FILES_PATH+=(".config/dotfiles/env")
HOME_FILES_CONTENT+=("$key=$value")
echo -n "$key=$value "
export $key=$value
done
echo
}
install_file_content() install_file_content()
{ {
fmt_note "installing file content ..." fmt_note "installing file content ..."
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
local filename="$HOME/${HOME_FILES_PATH[$i]}" local filename="$HOME/${HOME_FILES_PATH[$i]}"
local content=${HOME_FILES_CONTENT[$i]} local content="${HOME_FILES_CONTENT[$i]}"
fmt_info "installing \"$content\" into \"$filename\" ..." fmt_info "installing \"$content\" into \"$filename\" ..."
mkdir -p $(dirname "$filename") mkdir -p $(dirname "$filename")
if [ ! -f "$filename" ]; then if [ ! -f "$filename" ]; then
@ -119,8 +157,8 @@ install_symlink()
echo ---------- echo ----------
stat $dst stat $dst
echo ---------- echo ----------
ask_for_yN "would you like to replace ${dst}?" yn=$(ask_for_yN "would you like to replace ${dst}?")
if [ $? -eq 1 ]; then if [[ "$yn" == "1" ]]; then
rm $dst rm $dst
else else
fmt_error "aborting this job ..." fmt_error "aborting this job ..."
@ -211,7 +249,7 @@ install_update()
RET=$? RET=$?
if [[ $RET == 85 ]]; then if [[ $RET == 85 ]]; then
fmt_note "dfs updated. re-running install.sh ..." fmt_note "dfs updated. re-running install.sh ..."
"${DOTFILES}/install.sh" "$ORIGIN_ARGS" && exit "${DOTFILES}/install.sh" "$@" && exit
elif [[ $RET != 0 ]]; then elif [[ $RET != 0 ]]; then
fmt_fatal "update.sh failed with exit code $RET" fmt_fatal "update.sh failed with exit code $RET"
fi fi
@ -229,6 +267,7 @@ install()
if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi
install_update install_update
preinstall_check preinstall_check
prepare_config
install_crontab install_crontab
install_file_content install_file_content
install_symlink install_symlink
@ -241,10 +280,9 @@ install()
uninstall() uninstall()
{ {
ask_for_yN "do you really want to uninstall?" yn=$(ask_for_yN "do you really want to uninstall?")
if [[ $? != 1 ]]; then if [[ "$yn" != "1" ]]; then
fmt_error "aborting this job ..." fmt_fatal "aborting this job ..."
return
fi fi
uninstall_update uninstall_update
uninstall_crontab uninstall_crontab
@ -254,19 +292,23 @@ uninstall()
fmt_note "done uninstalling!" fmt_note "done uninstalling!"
} }
ORIGIN_ARGS="$@"
parse_arg "$@"
FUNC=install FUNC=install
INSTALL_DEP=0 INSTALL_DEP=0
for i in ${PARSE_ARG_RET[@]}; do store_config=0
for i in ${GOT_OPTS[@]}; do
if [[ "$store_config" == "1" ]]; then
store_config=0
DFS_CONFIGS+=("$i")
continue
fi
case $i in case $i in
-i ) FUNC=install ;; -i ) FUNC=install ;;
-r ) FUNC=uninstall ;; -r ) FUNC=uninstall ;;
-d|--dev ) export DFS_DEV=1 ;; -d|--dev ) export DFS_DEV=1; set -x ;;
-l|--lite ) export DFS_LITE=1 ;;
-a|--auto ) INSTALL_DEP=1 ;; -a|--auto ) INSTALL_DEP=1 ;;
-s|--secure ) export DFS_DEV=0 ;; -s|--secure ) export DFS_DEV=0 ;;
* ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a, -s" ;; -x ) store_config=1 ;;
* ) fmt_fatal "unknown option \"$i\"" ;;
esac esac
done done
$FUNC $FUNC

View File

@ -25,6 +25,6 @@ fi
if [[ -z "$CONTAINER" ]]; then if [[ -z "$CONTAINER" ]]; then
fmt_fatal "container not found" fmt_fatal "container not found"
else else
echo "--> ${CONTAINER_META[@]}" fmt_note "--> ${CONTAINER_META[@]}"
$SUDO docker exec -it $CONTAINER sh -c 'export DFS="bash <(curl dfs.beardic.cn) -a"; if [ -x "$(command -v zsh)" ]; then echo "--> zsh"; unset DFS; zsh; elif [ -x "$(command -v bash)" ]; then echo "--> bash"; bash; else echo "--> sh"; sh; fi' $SUDO docker exec -it $CONTAINER sh -c 'export DFS="bash <(curl dfs.beardic.cn) -a"; if [ -x "$(command -v zsh)" ]; then echo "--> zsh"; unset DFS; zsh; elif [ -x "$(command -v bash)" ]; then echo "--> bash"; bash; else echo "--> sh"; sh; fi'
fi fi

View File

@ -20,6 +20,6 @@ fi
if [[ -z "$IMAGE" ]]; then if [[ -z "$IMAGE" ]]; then
fmt_fatal "image not found" fmt_fatal "image not found"
else else
echo "--> ${IMAGE_META[@]}" fmt_note "--> ${IMAGE_META[@]}"
$SUDO docker run ${2:+"--name"} $2 -itd $IMAGE sh $SUDO docker run ${2:+"--name"} $2 -itd $IMAGE sh
fi fi

80
scripts/riot Executable file
View File

@ -0,0 +1,80 @@
#!/bin/bash
# connect to iot services
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/../tools/common.sh"
# get target settings
# provides:
SERVER=""
PORT=""
SSH_USERNAME=""
SSH_OPTIONS=""
get_server_meta()
{
local domain=${1##*.}
local host=${1%.*}
if [[ "$host" == "$domain" ]]; then
domain=""
fi
case $domain in
ibd )
SERVER=$host.ibd.ink
PORT=12022
SSH_USERNAME=root
;;
ob )
SERVER=$host.ob.ac.cn
PORT=24022
SSH_USERNAME=root
;;
nasp )
SERVER=$host
PORT=22
SSH_USERNAME=dictxiong
SSH_OPTIONS='-o ProxyJump="ssh@nasp.ob.ac.cn:36022"'
;;
"" )
SERVER=bj1.ob.ac.cn
local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]")
tmp=${tmp:0:4}
PORT=$((tmp+36000))
SSH_USERNAME=root
;;
* )
fmt_fatal "unknown domain: $domain"
esac
}
# ssh
_ssh()
{
get_server_meta "$1"
fmt_note "--> ssh to $SERVER:$PORT"
eval ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER
}
# main
print_help()
{
fmt_info "usage: $0 <command> <service> [options]"
echo "available commands: ssh"
}
router()
{
test $# -eq 2 || (print_help && fmt_fatal "invalid arguments")
case $1 in
-h|--help)
print_help
;;
ssh )
_ssh "$2"
;;
* )
print_help
fmt_fatal "unknown command: $1"
;;
esac
}
router "${GOT_OPTS[@]}"

View File

@ -15,7 +15,7 @@ apk_add()
# lite # lite
apk add zsh bash git tmux vim curl fzf iputils coreutils util-linux apk add zsh bash git tmux vim curl fzf iputils coreutils util-linux
# full # 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 apk add wget python3 py3-pip htop gcc g++ cmake make perl linux-headers bind-tools man-db
fi fi
} }
@ -34,8 +34,8 @@ router()
apk-add ) apk_add ;; apk-add ) apk_add ;;
set-timezone | set-tz ) set_timezone ;; set-timezone | set-tz ) set_timezone ;;
set-mirror ) set_mirror $2 ;; set-mirror ) set_mirror $2 ;;
* ) echo unknown command "$1". available: apk-add, set-timezone;; * ) echo unknown command \"$1\". available: apk-add, set-timezone;;
esac 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 ) export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi 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 # Color settings
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh # Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
if [ -t 1 ]; then if [[ -t 1 || "$DFS_COLOR" == "1" ]]; then
is_tty() { is_tty() {
true true
} }
@ -98,52 +126,36 @@ setup_color() {
# END of color settings # END of color settings
SUDO='' SUDO=''
SUDOE=''
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
SUDO='sudo' SUDO='sudo'
SUDOE='sudo -E'
fi 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() 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 read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
case $yn in case $yn in
[Yy]* ) return 1;; [Yy]* ) echo 1;;
* ) return 0;; * ) echo 0;;
esac esac
done fi
return 0
} }
ask_for_Yn() 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 read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn
case $yn in case $yn in
[Nn]* ) return 0;; [Nn]* ) echo 0;;
* ) return 1;; * ) echo 1;;
esac esac
done fi
return 1
} }
post_log() 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,6 +79,8 @@ print_help()
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>" fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
} }
router()
{
if [[ $# != 2 ]]; then if [[ $# != 2 ]]; then
print_help >&2 print_help >&2
exit 1 exit 1
@ -98,3 +100,6 @@ case "$1" in
fmt_fatal "invalid argument" fmt_fatal "invalid argument"
;; ;;
esac esac
}
router "${GOT_OPTS[@]}"

View File

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

View File

@ -17,7 +17,7 @@ pacman_S()
# lite # lite
pacman -S tmux git zsh bash curl vim pacman -S tmux git zsh bash curl vim
# full # 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 pacman -S wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip man-pages-posix
fi fi
} }
@ -27,8 +27,8 @@ router()
case $1 in case $1 in
pacman-S ) pacman_S ;; pacman-S ) pacman_S ;;
set-mirror ) set_mirror $2 ;; set-mirror ) set_mirror $2 ;;
* ) echo unknown command "$1". available: pacman-S, set-mirror ;; * ) echo unknown command \"$1\". available: pacman-S, set-mirror ;;
esac 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 dogo
doll doll
dfs cd dfs cd
./tools/test-getopts.sh
tools/common.sh get_os_type tools/common.sh get_os_type
tools/common.sh get_linux_dist tools/common.sh get_linux_dist
# check alias # check alias
alias p114 alias p114
which sibd which riot
piv-agent || which piv-agent piv-agent || which piv-agent
gbes || which gbes 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 git reset --hard $DFS_VERSION
# then check install.sh # 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 dfs version
test `git rev-parse HEAD` = `curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id` 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 # lite
apt-get install -y git zsh bash tmux vim curl inetutils-ping less bsdmainutils apt-get install -y git zsh bash tmux vim curl inetutils-ping less bsdmainutils
# full # 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 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 for i in {fzf,ripgrep,man-db}; do apt-get install -y $i; done
fi fi
@ -35,8 +35,8 @@ router()
set-mirror ) set_mirror $2 ;; set-mirror ) set_mirror $2 ;;
set-timezone\ set-timezone\
| set-tz ) set_timezone $2 ;; | 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 esac
} }
router $@ router "${GOT_OPTS[@]}"