[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

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