[refactor] clean, readable (#19)

* rebase zshrc, introduce DFS_NO_WALL

* improve ci

* improve ci

* improve ci

* update antigen url for DFS_NO_WALL

* OK

* improve ci for macos

* improve test.zsh

* staged

* fix ci

* fix ci

* use local vars

* introduce DFS_QUIET

* mass mod install.sh

* minor change

* set DFS_DEV=1 to prevent update

* debug: done -> fi

* fix DFS_DEV

* ubuntu.sh: set-mirror (ref: tuna)

* preinstall_check

* install.sh: -q to be quiet

* install.sh: -d to DFS_DEV

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
This commit is contained in:
Dict Xiong 2022-11-05 19:45:21 +08:00 committed by GitHub
parent 0739410aef
commit b6ff4116c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 260 additions and 142 deletions

View File

@ -22,7 +22,7 @@ jobs:
run: |
rev=`git rev-parse HEAD`
pwd
export DFS_NO_COMPILE=1 DFS_NO_UPDATE=1
export DFS_NO_COMPILE=1 DFS_DEV=1
./install.sh
test `git rev-parse HEAD` = "$rev"
@ -32,6 +32,21 @@ jobs:
echo $SHELL
antigen list
- name: run tests and reset
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: |
source tools/test.zsh
antigen reset
rm -rf $ANTIGEN $HOME/.antigen
- name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0}
env:
DFS_NO_WALL: 1
run: |
echo $SHELL
antigen list
- name: run tests
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: source tools/test.zsh
@ -55,7 +70,7 @@ jobs:
run: |
rev=`git rev-parse HEAD`
pwd
export DFS_NO_COMPILE=1 DFS_NO_UPDATE=1
export DFS_NO_COMPILE=1 DFS_DEV=1
./install.sh
test `git rev-parse HEAD` = "$rev"
@ -65,6 +80,21 @@ jobs:
echo $SHELL
antigen list
- name: run tests and reset
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: |
source tools/test.zsh
antigen reset
rm -rf $ANTIGEN $HOME/.antigen
- name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0}
env:
DFS_NO_WALL: 1
run: |
echo $SHELL
antigen list
- name: run tests
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: source tools/test.zsh

View File

@ -25,7 +25,11 @@ if [[ "$(git rev-parse HEAD)" == "$dfs_commit" ]]; then
post_log "INFO" "$THIS_FILE" "nothing to do"
else
fmt_info "checking out to commit $dfs_commit ..."
git -c advice.detachedHead=false checkout $dfs_commit
if [[ -z "$DFS_DEV" ]]; then
git -c advice.detachedHead=false checkout $dfs_commit
else
fmt_warning "won't really checkout in dev mode"
fi
post_log "INFO" "$THIS_FILE" "will check out to commit $dfs_commit"
cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit
fi

48
.zshrc2
View File

@ -1,22 +1,29 @@
# env
export TERM="xterm-256color"
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export PATH="$PATH:$DOTFILES/scripts"
export LESS="-N"
# antigen
# if test -z "$NO_WALL"; then
# ANTIGEN_OMZ_REPO_URL="https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git"
# ANTIGEN_URL="https://raw.fastgit.org/zsh-users/antigen/master/bin/antigen.zsh"
# GIHUB_PROXY="https://hub.fastgit.xyz/"
# else
# ANTIGEN_URL="git.io/antigen"
# GIHUB_PROXY=""
# fi
# antigen bundle ${GIHUB_PROXY}zsh-users/zsh-syntax-highlighting
# antigen bundle ${GIHUB_PROXY}zsh-users/zsh-completions
# antigen bundle ${GIHUB_PROXY}zsh-users/zsh-autosuggestions
ANTIGEN_OMZ_REPO_URL="https://gitee.com/dictxiong/ohmyzsh.git"
ANTIGEN_URL="https://gitee.com/dictxiong/antigen/raw/develop/bin/antigen.zsh"
if [[ -z "$DFS_NO_WALL" ]]; 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_OMZ_REPO_URL="https://github.com/ohmyzsh/ohmyzsh.git"
ANTIGEN_PLUGINS=( \
"https://github.com/zsh-users/zsh-syntax-highlighting" \
"https://github.com/zsh-users/zsh-completions" \
"https://github.com/zsh-users/zsh-autosuggestions" \
)
fi
ANTIGEN="$HOME/antigen.zsh"
# Install antigen.zsh if not exist
if [ ! -f "$ANTIGEN" ]; then
@ -40,9 +47,9 @@ if [ ! -f "$ANTIGEN" ]; then
fi
# config and enable antigen
source "$ANTIGEN"
antigen use oh-my-zsh
# enable plugins
antigen_plugins=( \
antigen use oh-my-zsh
ANTIGEN_PLUGINS+=( \
"command-not-found" \
"docker" \
"extract" \
@ -52,11 +59,8 @@ antigen_plugins=( \
"tmux" \
"ufw" \
"z" \
"https://gitee.com/dictxiong/zsh-syntax-highlighting" \
"https://gitee.com/dictxiong/zsh-completions" \
"https://gitee.com/dictxiong/zsh-autosuggestions" \
)
for i in ${antigen_plugins[*]}
for i in ${ANTIGEN_PLUGINS[*]}
do
if [[ -z $ANTIGEN_EXCLUDE || ! $i =~ $ANTIGEN_EXCLUDE ]]; then
antigen bundle $i
@ -75,10 +79,6 @@ antigen theme ${ZSH_THEME:-$TMP_THEME}
antigen apply
# end of antigen config
# env
export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export PATH=$PATH:$DOTFILES/scripts
# alias
alias "pls"='sudo $(fc -ln -1)'
alias "se"='sudo -sE'

View File

@ -3,87 +3,144 @@
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/tools/common.sh"
home_slashes=${HOME//\//\\\/}
if [[ ! $DOTFILES == ${home_slashes}* ]]; then
if [[ ! "$DOTFILES" == "${HOME}"* ]]; then
fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..."
fi
dotfile_home_path=${DOTFILES/${home_slashes}/\~}
dotfile_relative_path=${DOTFILES#${home_slashes}\/}
crontab_job="0 * * * * ${DOTFILES}/update.sh"
DOTFILE_TILDE=${DOTFILES/"$HOME"/\~}
insert_if_not_exist()
{
filename=$1
line=$2
fmt_note "installing \"$line\" into \"$filename\" ..."
mkdir -p $(dirname "$filename")
if [ ! -f "$filename" ]; then
touch $filename
fi
grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename"
}
CRON_JOB="0 * * * * ${DOTFILES}/update.sh"
declare -a HOME_FILES_PATH
declare -a HOME_FILES_CONTENT
HOME_FILES_PATH[0]=".zshrc"
HOME_FILES_CONTENT[0]="source ${DOTFILE_TILDE}/.zshrc2"
HOME_FILES_PATH[1]=".tmux.conf"
HOME_FILES_CONTENT[1]="source-file ${DOTFILE_TILDE}/.tmux.conf2"
HOME_FILES_PATH[2]=".vimrc"
HOME_FILES_CONTENT[2]="source ${DOTFILE_TILDE}/.vimrc2"
HOME_FILES_PATH[3]=".gitconfig"
HOME_FILES_CONTENT[3]="[include] path = \"${DOTFILE_TILDE}/.gitconfig2\""
delete_if_exist()
{
filename=$1
line=$2
fmt_note "removing \"$line\" from \"$filename\" ..."
if [ -f "$filename" ]; then
grep -vxF -- "$line" "$filename" | tee "$filename"
fi
}
declare -a HOME_SYMLINKS_SRC
declare -a HOME_SYMLINKS_DST
HOME_SYMLINKS_SRC[0]=".ssh/authorized_keys2"
HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
create_symlink()
preinstall_check()
{
src=$1
dest=$2
fmt_note "creating symlink \"$dest\" --> \"$src\" ..."
if [ ! -f "$src" ]; then
fmt_error "\"$src\" does not exist! aborting this job ..."
return 1
fi
mkdir -p $(dirname "$dest")
if [ -f "$dest" ]; then
if [ "$(readlink $dest)" -ef "$src" ]; then
return 0
mandatory_commands=( "git" "zsh" "curl" "ping" )
optional_commands=( "python3" "vim" "tmux" )
for i in "${mandatory_commands[@]}"; do
if [[ ! -x "$(command -v $i)" ]]; then
fmt_info "all this utils are required: ${mandatory_commands[@]}"
fmt_info "install them manually or check scripts in tools/"
fmt_fatal "\"$i\" not found. aborting ..."
fi
fmt_warning "\"$dest\" already exists! stat output:"
echo ----------
stat $dest
echo ----------
ask_for_yN "would you like to replace ${dest}?"
if [ $? -eq 1 ]; then
rm $dest
else
fmt_error "\"$dest\" already exists! aborting this job ..."
return 1
done
for i in "${optional_commands[@]}"; do
if [[ ! -x "$(command -v $i)" ]]; then
fmt_warning "\"$i\" not found"
ask_for_Yn "continue anyway?"
if [[ "$?" == "0" ]]; then
fmt_info "all this utils are suggested: ${optional_commands[@]}"
fmt_info "install them manually or check scripts in tools/"
fmt_fatal "aborting ..."
fi
fi
fi
ln -s $src $dest
return $?
done
}
delete_link_if_match()
install_file_content()
{
src=$1
dest=$2
if [ "$(readlink $dest)" -ef "$src" ]; then
fmt_note "removing symlink \"$dest\" ..."
echo ----------
stat $dest
echo ----------
rm $dest
fi
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
local filename="$HOME/${HOME_FILES_PATH[$i]}"
local content=${HOME_FILES_CONTENT[$i]}
fmt_note "installing \"$content\" into \"$filename\" ..."
mkdir -p $(dirname "$filename")
if [ ! -f "$filename" ]; then
touch $filename
fi
grep -qxF -- "$content" "$filename" || echo "$content" >> "$filename"
done
}
uninstall_file_content()
{
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
local filename="$HOME/${HOME_FILES_PATH[$i]}"
local content=${HOME_FILES_CONTENT[$i]}
fmt_note "removing \"$content\" from \"$filename\" ..."
if [ -f "$filename" ]; then
grep -vxF -- "$content" "$filename" | tee "$filename"
fi
done
}
install_symlink()
{
for ((i=0; i<${#HOME_SYMLINKS_SRC[@]}; i++)); do
local src="$DOTFILES/${HOME_SYMLINKS_SRC[$i]}"
local dst="$HOME/${HOME_SYMLINKS_DST[$i]}"
fmt_note "creating symlink \"$dst\" --> \"$src\" ..."
if [ ! -f "$src" ]; then
fmt_error "\"$src\" does not exist! aborting this job ..."
continue
fi
mkdir -p $(dirname "$dst")
if [ -f "$dst" ]; then
if [ "$(readlink $dst)" -ef "$src" ]; then
continue
fi
fmt_warning "\"$dst\" already exists! stat output:"
echo ----------
stat $dst
echo ----------
ask_for_yN "would you like to replace ${dst}?"
if [ $? -eq 1 ]; then
rm $dst
else
fmt_error "aborting this job ..."
continue
fi
fi
ln -s $src $dst
done
}
uninstall_symlink()
{
local src
for src in "${!HOME_SYMLINKS[@]}"; do
local dst=${HOME_SYMLINKS[$src]}
src="$DOTFILES/$src"
dst="$HOME/$dst"
if [ "$(readlink $dst)" -ef "$src" ]; then
fmt_note "removing symlink \"$dst\" ..."
echo ----------
stat $dst
echo ----------
rm $dst
fi
done
}
install_crontab(){
fmt_note "installing \"$crontab_job\" to crontab ..."
( crontab -l | grep -vxF "${crontab_job}" | grep -v "no crontab for"; echo "$crontab_job" ) | crontab -
if [[ -x $(command -v crontab) ]]; then
fmt_note "installing \"$CRON_JOB\" to crontab ..."
( crontab -l | grep -vxF "${CRON_JOB}" | grep -v "no crontab for"; echo "$CRON_JOB" ) | crontab -
else
fmt_warning "crontab does not exist. skipping ..."
fi
}
uninstall_crontab(){
fmt_note "removing \"$crontab_job\" from crontab ..."
( crontab -l | grep -vxF "$crontab_job" ) | crontab -
if [[ -x $(command -v crontab) ]]; then
fmt_note "removing \"$CRON_JOB\" from crontab ..."
( crontab -l | grep -vxF "$CRON_JOB" ) | crontab -
else
fmt_note "crontab does not exist. skipping ..."
fi
}
install_tmux_tpm(){
@ -118,10 +175,8 @@ install_update(){
fmt_note "installing update.sh ..."
cp "${DOTFILES}/.update.sh" "${DOTFILES}/update.sh"
chmod +x "${DOTFILES}/update.sh"
if [[ -z "$DFS_NO_UPDATE" ]]; then
fmt_note "running update.sh ..."
${DOTFILES}/update.sh
fi
fmt_note "running update.sh ..."
${DOTFILES}/update.sh
}
uninstall_update(){
@ -130,13 +185,11 @@ uninstall_update(){
}
install(){
preinstall_check
install_update
install_crontab
insert_if_not_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2"
insert_if_not_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2"
insert_if_not_exist "${HOME}/.vimrc" "source ${dotfile_home_path}/.vimrc2"
insert_if_not_exist "${HOME}/.gitconfig" "[include] path = \"${dotfile_home_path}/.gitconfig2\""
create_symlink "${DOTFILES}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
install_file_content
install_symlink
# those that won't be uninstalled in the future
install_tmux_tpm
install_vim_vundle
@ -145,21 +198,26 @@ install(){
uninstall(){
ask_for_yN "do you really want to uninstall?"
if [[ $? == 1 ]]; then
uninstall_update
uninstall_crontab
delete_if_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2"
delete_if_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2"
delete_if_exist "${HOME}/.vimrc" "source ${dotfile_home_path}/.vimrc2"
delete_if_exist "${HOME}/.gitconfig" "[include] path = \"${dotfile_home_path}/.gitconfig2\""
delete_link_if_match "${DOTFILES}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
fmt_note "done uninstalling!"
if [[ $? != 1 ]]; then
fmt_error "aborting this job ..."
return
fi
uninstall_update
uninstall_crontab
uninstall_file_content
uninstall_symlink
fmt_note "done uninstalling!"
}
case $1 in
""|-i ) install ;;
-r ) uninstall ;;
* ) fmt_warning "unknown command \"$1\". available: -i, -r" ;;
esac
BIN=install
while [[ $# > 0 ]]; do
case $1 in
-i ) BIN=install ;;
-r ) BIN=uninstall ;;
-q ) export DFS_QUIET=1 ;;
-d ) export DFS_DEV=1 ;;
* ) fmt_warning "unknown command \"$1\". available: -i, -r, -q, -d"; exit 1 ;;
esac
shift
done
$BIN

View File

@ -1,8 +1,7 @@
#!/bin/bash
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
DOTFILES=${DOTFILES:-$( cd "$THIS_DIR/.." && pwd )}
source "$DOTFILES/tools/common.sh"
source "$THIS_DIR/../tools/common.sh"
if [[ -z "$1" || "$1" =~ "-h|--help" ]]; then
fmt_info "usage: dogo <container>"

View File

@ -4,10 +4,9 @@ set -e
set_mirror()
{
#MIRROR=${1:="mirrors.tuna.tsinghua.edu.cn"}
#MIRROR=${MIRROR//\//\\\/}
#sed -i 's/(archive|security).ubuntu.com/${MIRROR}/g' /etc/apt/sources.list
echo "to-do ..."
MIRROR=${1:-"mirrors.tuna.tsinghua.edu.cn"}
MIRROR=${MIRROR//\//\\\/}
sed -i "s/dl-cdn.alpinelinux.org/$MIRROR/g" /etc/apk/repositories
}
apk_add()
@ -32,7 +31,7 @@ router()
case $1 in
apk-add ) apk_add ;;
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;;
esac
}

View File

@ -1,7 +1,7 @@
#!/bin/bash
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export DOTFILES=$( cd "$THIS_DIR/.." && pwd )
THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
SUDO=''
if (( $EUID != 0 )); then
@ -38,7 +38,7 @@ supports_truecolor() {
fmt_fatal() {
printf '%sfatal: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "${FMT_RESET}" >&2
exit
exit 1
}
fmt_error() {
@ -103,14 +103,26 @@ setup_color() {
ask_for_yN()
{
while true; do
while [[ -z "$DFS_QUIET" ]]; do
read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
case $yn in
[Yy]* ) return 1;;
[Nn]* ) return 0;;
* ) return 0;;
esac
done
return 0
}
ask_for_Yn()
{
while [[ -z "$DFS_QUIET" ]]; do
read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn
case $yn in
[Nn]* ) return 0;;
* ) return 1;;
esac
done
return 1
}
post_log()
@ -119,6 +131,7 @@ post_log()
}
get_os_type() {
local ans="unknown"
case "$(uname -s)" in
Darwin*) ans="MacOS";;
CYGWIN*) ans="Cygwin";;
@ -130,26 +143,28 @@ get_os_type() {
}
get_linux_dist() {
local ans="unknown"
if [ -f /etc/os-release ]; then
. /etc/os-release
ans=$ID
ans="$ID"
elif type lsb_release >/dev/null 2>&1; then
ans=$(lsb_release -si)
ans="$(lsb_release -si)"
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
ans=$DISTRIB_ID
ans="$DISTRIB_ID"
elif [ -f /etc/debian_version ]; then
ans=Debian
ans="Debian"
elif [ -f /etc/SuSe-release ]; then
ans=SUSE
ans="SUSE"
elif [ -f /etc/redhat-release ]; then
ans=RedHat
ans="RedHat"
else
ans=unknown
ans="unknown"
fi
echo $ans | tr '[:upper:]' '[:lower:]'
}
# if bash-ed, else source-d
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
$1 "${@:2}"
else

View File

@ -1,7 +1,7 @@
#!/bin/bash
declare -A install_commands
install_commands=(\
declare -A INSTALL_COMMANDS
INSTALL_COMMANDS=(\
[git]="apt update && apt install git" \
[fzf]="git clone --depth 1 https://gitee.com/dictxiong/fzf.git ~/.fzf && ~/.fzf/install" \
[acme.sh]="curl https://get.acme.sh | sh -s email=${EMAIL:-me@beardic.cn}" \
@ -15,7 +15,7 @@ install_commands=(\
install()
{
echo -e ${install_commands[$1]}
echo -e ${INSTALL_COMMANDS[$1]}
}
install $1

View File

@ -13,13 +13,13 @@ set_mirror()
pacman_S()
{
pacman -Syu
pacman -S tmux git zsh curl vim wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip
pacman -S tmux git zsh curl vim wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip
}
router()
{
case $1 in
pacman-S ) pacman_S ;;
pacman-S ) pacman_S ;;
set-mirror ) set_mirror $2 ;;
* ) echo unknown command "$1". available: pacman-S, set-mirror ;;
esac

View File

@ -1,10 +1,14 @@
#!/bin/false "This script should be sourced in a shell, not executed directly"
#!/bin/false "This script should be sourced in zsh, not executed directly"
set -ex
# check files
cd /
l
cd ~
l
dfs cd
l
pwd
test -f .zshrc2
diff -q ./.ssh/authorized_keys2 ~/.ssh/authorized_keys2
@ -13,8 +17,10 @@ grep -q ".zshrc2" ~/.zshrc
# check scripts and functions
dfs version
dfs log 1
l
z ~
test ~ -ef "$(pwd)"
dogo
dfs cd
tools/common.sh get_os_type
tools/common.sh get_linux_dist
@ -22,6 +28,12 @@ tools/common.sh get_linux_dist
alias p114 > /dev/null
# check update
DFS_VERSION=`dfs version`
dfs update
dfs version
test `git rev-parse HEAD` = `curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id`
# clean
dfs cd
git reset --hard $DFS_VERSION
set +x

View File

@ -6,7 +6,8 @@ set_mirror()
{
MIRROR=${1:-"mirrors.tuna.tsinghua.edu.cn"}
MIRROR=${MIRROR//\//\\\/}
sed -i 's/(archive|security).ubuntu.com/${MIRROR}/g' /etc/apt/sources.list
sed -i "s@http://.*archive.ubuntu.com@https://${MIRROR}@g" /etc/apt/sources.list
sed -i "s@http://.*security.ubuntu.com@https://${MIRROR}@g" /etc/apt/sources.list
}
apt_install()