Skip to content

Commit

Permalink
[dev] refactor log.py -> logger.sh (#32)
Browse files Browse the repository at this point in the history
* update.sh: fetch will prune

* log: use hostname directly

* refactor log.py to logger.sh (#31)

* logger.sh: refactor log.py to bash script

* update.sh use beacon; ci test beacon

* install.sh: install dep first

* install.sh: DFS_UPDATED_RET=85

* more beacon; use stderr

* remove py3 dep

* install.sh: remove py3

* install.sh: -s|--secure

* async log; beacon add sys.login

* update use sync beacon

* fix ci minor bug; macos: uuidgen/e2fsprogs

* logger.sh: not need to use uuid5

* bug fix

* dfs err_return

* common.sh: post_log and post_beacon will check #args

* bug fix

* zshrc: fix locale when C.UTF-8 not exists

* WIP: combine install -a and tools/ scripts

* finish combination; get_os_name; macos.sh

* test.ci: now all use -a to install deps

* bug fix

* bug fix; ci: install.sh run with -x

* install.sh: bug fix that re-run install.sh after updated needs orgin args

* format

Co-authored-by: xiongdian.me <[email protected]>
  • Loading branch information
DictXiong and xiongdian.me authored Dec 3, 2022
1 parent 56e2b21 commit ff8ac21
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 161 deletions.
23 changes: 8 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ jobs:
name: test on ubuntu
runs-on: ubuntu-latest
steps:
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get install -y git python3 python3-pip zsh curl inetutils-ping
sudo pip3 install requests
- name: checkout repo
uses: actions/checkout@v3
with:
Expand All @@ -22,7 +16,8 @@ jobs:
run: |
rev=`git rev-parse HEAD`
pwd
./install.sh -dl
set -x
./install.sh -adl
test `git rev-parse HEAD` = "$rev"
- name: antigen build
Expand All @@ -37,6 +32,7 @@ jobs:
source tools/test.zsh
antigen reset
rm -rf $ANTIGEN $HOME/.antigen
./install.sh -dl
- name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0}
Expand All @@ -54,12 +50,6 @@ jobs:
name: test on macos
runs-on: macos-12
steps:
- name: install dependencies
run: |
brew update
brew install git python3 zsh curl
sudo pip3 install requests
- name: checkout repo
uses: actions/checkout@v3
with:
Expand All @@ -69,7 +59,8 @@ jobs:
run: |
rev=`git rev-parse HEAD`
pwd
./install.sh -dl
set -x
./install.sh -adl
test `git rev-parse HEAD` = "$rev"
- name: antigen build
Expand All @@ -84,6 +75,7 @@ jobs:
source tools/test.zsh
antigen reset
rm -rf $ANTIGEN $HOME/.antigen
./install.sh -dl
- name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0}
Expand All @@ -103,7 +95,7 @@ jobs:
steps:
- name: remove dependencies
run: |
sudo apt-get -y remove curl vim
sudo apt-get -y remove curl vim python3
- name: checkout repo
uses: actions/checkout@v3
Expand All @@ -113,6 +105,7 @@ jobs:
- name: install dfs
run: |
rev=`git rev-parse HEAD`
set -x
./install.sh -dal
test `git rev-parse HEAD` = "$rev"
Expand Down
10 changes: 5 additions & 5 deletions .update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ DFS_UPDATE_CHANNEL=${DFS_UPDATE_CHANNEL:-"main"}

# fetch origin
cd $DOTFILES
git fetch --all
git fetch --all --prune
if [[ -n "$(git status -s)" ]]; then
fmt_error "directory not clean"
post_log "ERROR" "$THIS_FILE" "directory not clean"
apost_beacon "dfs.dirty"
exit
fi

Expand All @@ -25,18 +25,18 @@ case $DFS_UPDATE_CHANNEL in
esac
if [[ ${#DFS_COMMIT} != 40 ]]; then
fmt_error "invalid commit id"
post_log "ERROR" "$THIS_FILE" "invalid commit id: ${DFS_COMMIT}"
apost_beacon "dfs.invalid-commit"
apost_log "ERROR" "$THIS_FILE" "invalid commit id: ${DFS_COMMIT}"
exit
fi

# update
if [[ "$(git rev-parse HEAD)" == "$DFS_COMMIT" ]]; then
fmt_info "nothing to do"
post_log "INFO" "$THIS_FILE" "nothing to do"
else
fmt_info "checking out to commit $DFS_COMMIT ..."
if [[ -z "$DFS_DEV" ]]; then
post_log "INFO" "$THIS_FILE" "will check out to commit $DFS_COMMIT"
post_beacon "dfs.updated"
git -c advice.detachedHead=false checkout $DFS_COMMIT
cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit $DFS_UPDATED_RET
else
Expand Down
18 changes: 16 additions & 2 deletions .zshrc2
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# env for shell
export TERM="xterm-256color"
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
if ( locale -a | grep -qxE "C.UTF-8|C.utf8" ); then
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
else
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
fi
export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export PATH="$PATH:$DOTFILES/scripts"
export EDITOR='vim'
Expand Down Expand Up @@ -82,6 +87,13 @@ antigen theme ${ZSH_THEME:-$TMP_THEME}
antigen apply
# end of antigen config

# beacon
if [[ "$EUID" == "0" ]]; then
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.root"
else
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.user"
fi

# alias
alias "pls"='sudo $(fc -ln -1)'
alias "se"='sudo -sE'
Expand Down Expand Up @@ -144,6 +156,7 @@ bindkey "\ed" delete-char # alt+d
# dfs
dfs()
{
setopt local_options err_return pipe_fail
case $1 in
cd ) cd "$DOTFILES" ;;
update ) "$DOTFILES/update.sh" ;;
Expand All @@ -169,6 +182,7 @@ dfs()
echo 'Done. Please open a new shell to see the changes.'
;;
log ) "$DOTFILES/tools/common.sh" "post_log" "INFO" "dfs" "$2" ;;
beacon ) "$DOTFILES/tools/common.sh" "post_beacon" "$2" ;;
* ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;;
esac
}
Expand Down
82 changes: 38 additions & 44 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,37 @@ HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
install_dependencies()
{
fmt_note "installing dependencies ..."
case $(get_os_type) in
"linux" )
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 bsdmainutils
;;
"alpine" )
$SUDO apk update
$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
case $(get_os_name) in
"ubuntu"|"debian" )
$SUDO DFS_LITE=$DFS_LITE "$DOTFILES/tools/ubuntu.sh" apt-install
;;
"alpine" )
$SUDO DFS_LITE=$DFS_LITE "$DOTFILES/tools/alpine.sh" apk-add
;;
"macos" )
$SUDO brew update
$SUDO brew install git python3 zsh curl tmux vim util-linux
DFS_LITE=$DFS_LITE "$DOTFILES/tools/macos.sh" brew-install
;;
"msys" )
pacman -Syu
pacman -S tmux git zsh bash curl vim python3 python3-pip
SUDO=""
DFS_LITE=$DFS_LITE "$DOTFILES/tools/msys2.sh" pacman-S
;;
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_type)"
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name)"
esac

if [[ -x $(command -v pip3) ]]; then
$SUDO pip3 install requests
elif [[ -x $(command -v pip) ]]; then
$SUDO pip install requests
else
fmt_error "pip3 and pip not found. is pip correctly installed?"
fi
}

preinstall_check()
{
fmt_note "checking requirements ..."
local mandatory_commands=( "git" "zsh" "curl" )
local optional_commands=( "python3" "vim" "tmux" "ping" )
local mandatory_commands=( "git" "zsh" "curl" "grep" "cat" "cp" "bash" "mkdir" )
local optional_commands=( "vim" "tmux" "ping" )
for i in "${mandatory_commands[@]}"; do
if [[ ! -x "$(command -v $i)" ]]; then
if ! command -v $i 1>/dev/null; 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
done
for i in "${optional_commands[@]}"; do
if [[ ! -x "$(command -v $i)" ]]; then
if ! command -v $i 1>/dev/null; then
fmt_warning "\"$i\" not found"
ask_for_Yn "continue anyway?"
if [[ "$?" == "0" ]]; then
Expand Down Expand Up @@ -167,7 +149,8 @@ uninstall_symlink()
done
}

install_crontab(){
install_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 -
Expand All @@ -176,7 +159,8 @@ install_crontab(){
fi
}

uninstall_crontab(){
uninstall_crontab()
{
if [[ -x $(command -v crontab) ]]; then
fmt_note "removing \"$CRON_JOB\" from crontab ..."
( crontab -l | grep -vxF "$CRON_JOB" ) | crontab -
Expand All @@ -185,7 +169,8 @@ uninstall_crontab(){
fi
}

install_tmux_tpm(){
install_tmux_tpm()
{
TMUX_TPM="$HOME/.tmux/plugins/tpm"
if [[ -x $(command -v tmux) && ! -d "$TMUX_TPM" ]]; then
fmt_note "installing tmux tpm ..."
Expand All @@ -204,7 +189,8 @@ install_tmux_tpm(){
fi
}

install_vim_vundle(){
install_vim_vundle()
{
VIM_VUNDLE="$HOME/.vim/bundle/Vundle.vim"
if [[ -x $(command -v vim) && ! -d "$VIM_VUNDLE" ]]; then
fmt_note "installing vim vundle ..."
Expand All @@ -214,37 +200,42 @@ install_vim_vundle(){
fi
}

install_update(){
install_update()
{
fmt_note "installing update.sh ..."
cp "${DOTFILES}/.update.sh" "${DOTFILES}/update.sh"
chmod +x "${DOTFILES}/update.sh"
fmt_note "running update.sh ..."
DFS_UPDATED_RET=1 ${DOTFILES}/update.sh
if [[ $? == 1 ]]; then
DFS_UPDATED_RET=85 ${DOTFILES}/update.sh
if [[ $? == 85 ]]; then
fmt_note "dfs updated. re-running install.sh ..."
"${DOTFILES}/install.sh" && exit
"${DOTFILES}/install.sh" "$ORIGIN_ARGS" && exit
fi
}

uninstall_update(){
uninstall_update()
{
fmt_note "removing update.sh ..."
rm "${DOTFILES}/update.sh"
}

install(){
install_update
install()
{
if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi
install_update
preinstall_check
install_crontab
install_file_content
install_symlink
apost_beacon "dfs.installed"
# those that won't be uninstalled in the future
install_tmux_tpm
install_vim_vundle
fmt_note "done installing!"
}

uninstall(){
uninstall()
{
ask_for_yN "do you really want to uninstall?"
if [[ $? != 1 ]]; then
fmt_error "aborting this job ..."
Expand All @@ -254,9 +245,11 @@ uninstall(){
uninstall_crontab
uninstall_file_content
uninstall_symlink
apost_beacon "dfs.uninstalled"
fmt_note "done uninstalling!"
}

ORIGIN_ARGS="$@"
parse_arg "$@"
FUNC=install
INSTALL_DEP=0
Expand All @@ -267,7 +260,8 @@ for i in ${PARSE_ARG_RET[@]}; do
-d|--dev ) export DFS_DEV=1 ;;
-l|--lite ) export DFS_LITE=1 ;;
-a|--auto ) INSTALL_DEP=1 ;;
* ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a" ;;
-s|--secure ) export DFS_DEV=0 ;;
* ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a, -s" ;;
esac
done
$FUNC
16 changes: 9 additions & 7 deletions tools/alpine.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#!/bin/bash

set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"

set_mirror()
{
MIRROR=${1:-"mirrors.tuna.tsinghua.edu.cn"}
MIRROR=${MIRROR//\//\\\/}
sed -i "s/dl-cdn.alpinelinux.org/$MIRROR/g" /etc/apk/repositories
sed -i "[email protected]@${MIRROR}@g" /etc/apk/repositories
}

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 util-linux
#for i in {fzf,ripgrep}; do apk add $i -y; done
# lite
apk add zsh bash git tmux vim curl fzf iputils coreutils util-linux
# full
if [[ -z "$DFS_LITE" ]]; then
apk add wget python3 py3-pip htop gcc g++ cmake make perl linux-headers bind-tools man-db
fi
}

set_timezone()
Expand Down
Loading

0 comments on commit ff8ac21

Please sign in to comment.