24 Commits

Author SHA1 Message Date
ff8ac21424 [dev] refactor log.py -> logger.sh (#32)
* 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 <xiongdian.me@bytedance.com>
2022-12-03 20:37:20 +08:00
56e2b217eb [dev] dfs config; trash (#30)
* install.sh: fix install dep

* update.sh: alternative channel 'dev'

* zshrc: dfs config

* lint: remove endline \

* if trash, disable rm

* update.sh: fallback to main if dev not exists

* bug fix

* to-install.sh; v2ray -> v2fly

* gitconfig: quotepath=false

* git log using iso date; dfs vversion
2022-11-24 20:51:35 +08:00
f39193f28f [core] parse func; gbes; .config/dotfiles/env (#29)
* common.sh: arg parse init

* bug fix

* bug fix

* bug fix. now common.sh won't parse arg autoly

* improve git-branches

* improve git-branches; util-linux

* accelerate ci

* improve gbes; PARSE_ARG_RET

* ci: fix gbes

* ssh: add key ltp1.bd

* Revert "ssh: add key ltp1.bd"

This reverts commit c2433a0549.

* install column; apt -> apt-get

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
2022-11-17 20:02:35 +08:00
c354f980bb WIP: piv fix (#28)
* piv logi fix

* dogo: $DFS

* snasp; doll: $2 for the name

* DFS_UPDATE_CHANNEL?

* git-branches

* ci: git-branches

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
2022-11-15 22:18:16 +08:00
3ce5a7ade8 zshrc: key bindings and piv-agent (#27)
* piv-agent; macos key bindings; doll fix

* ci: piv-agent
2022-11-15 01:28:38 +08:00
6e33fc0875 scripts: docker scripts (#26)
* dogo/doll

dogo supports zsh/bash/sh; doll is introduced to launch a new docker
container; update $SUDO in command.sh

* update ci

* fix regex

* dogo: enhance, support -[0-9]*

* dogo&doll: enhanced

* zshrc: sibd and sob
2022-11-14 01:37:48 +08:00
995899f3a3 install.sh: rerun when updated (#25)
* install.sh: rerun when updated

* update ci
2022-11-12 20:05:29 +08:00
9a7f0b1b06 install.sh: support combined options (#24)
* install.sh: support combined short options

* ci: use combined opt

* debug

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
2022-11-11 10:36:50 +08:00
300b3a29b7 install.sh: cmake, less, and -l=DFS_LITE (#23)
* install.sh: cmake and less

* install.sh: -l=DFS_LITE

* ci: support -l=DFS_LITE

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
2022-11-09 23:19:55 +08:00
6dce3cfa18 install.sh: autodep, improve note (#22)
* install.sh: --auto

* ci: test --auto

* ci: remove dep when test --auto

* debug

* ok. improve the install notes
2022-11-08 22:58:17 +08:00
5b00add073 [core] update (#21)
* vim quiet install

* do not set LESS

* gitconfig and envs
2022-11-06 14:00:25 +08:00
b191403798 ssh: add ip14 (#20) 2022-11-06 02:29:52 +08:00
b6ff4116c8 [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>
2022-11-05 19:45:21 +08:00
0739410aef git: push.autoSetupRemote=true (#18) 2022-10-28 12:59:12 +08:00
9778c69d78 degrade log 403 to warning (#17) 2022-10-27 22:57:53 +08:00
83695d48fb fix update & docker notify (#16)
* update.sh improve
* docker notify
2022-10-27 01:16:45 +08:00
5bd6d9f822 vim: backspace fix (#15)
vim backspace fix
2022-09-01 23:22:18 +08:00
3421d27be9 ci: hotfix (#14)
hotfix
2022-08-28 21:07:10 +08:00
18dfca5f39 ci: better structure (#13)
* struct ci; staged

* seems good

* ci try macos

* debug

* debug

* debug

* debug

* debug

* debug

* debug

* debug

* debug

* debug
2022-08-28 20:51:44 +08:00
fa737d482f ci: init ci (#12)
* test.yml init

* debug

* debug

* debug

* debug

* debug

* debug

* debug

* debug
2022-08-27 01:42:15 +08:00
8782121dcc struct: standard file structure (#11)
* mass change. staged, not tested

* still mass changed without test

* bug fix

* bug fix
2022-08-26 20:43:00 +08:00
f313a6887e zshrc: ls multisystem fix (#10)
1. alias l according to the local os type
2. ci sync triggered by all branches
2022-08-25 00:23:29 +08:00
7804f196b4 ci: sync fzf (#9)
* ci: sync fzf

* tools/install.sh update fzf
2022-08-20 14:24:52 +08:00
c834980b29 Dev (#8)
* use update.sh to update safely

* add unknown command "version". available: update, force-update, reset, cd and replace tabs with spaces

* bug fix

* bug fix

* uninstall will rm update.sh

* new force-update

* bug fix

* remove thefuck

just use `pls`

* init post-log.py

* update uuid filepath

* update.sh will post log

* dfs log

* bug fix

* bug fix

* use logging

* logging format

* bug fix

* standarlize the config path

* bug fix

* add ssh key: ltp2
2022-08-14 20:34:18 +08:00
20 changed files with 1183 additions and 443 deletions

View File

@@ -3,3 +3,18 @@
name = Dict Xiong
[core]
editor = vim
# test this by `git update-index --test-untracked-cache`
# disable this by setting `GIT_DISABLE_UNTRACKED_CACHE`
untrackedCache = true
quotepath = false # chinese chars
[push]
autoSetupRemote = true
[branch]
# Show most recently changed branches first.
sort = -committerdate
[help]
autocorrect = 1
[init]
defaultBranch = main
[log]
date = iso

View File

@@ -1,9 +1,6 @@
name: sync repos to gitee
on:
push:
branches:
- main
- dev
push: ~
schedule:
- cron: 10 4 * * *
workflow_dispatch: ~

View File

@@ -106,3 +106,20 @@ jobs:
force_update: true # 启用后,强制同步,即强制覆盖目的端仓库
static_list: "onedark.vim" # 静态同步列表,在此填写需要同步的仓库名称,可填写多个
timeout: '600s' # git超时设置,超时后会自动重试git操作
fzf:
runs-on: ubuntu-20.04
steps:
- name: junegunn/fzf # 名字随便起
uses: Yikun/hub-mirror-action@v1.2 # 使用Yikun/hub-mirror-action
with:
src: github/junegunn # 源端账户名(github)
dst: gitee/dictxiong # 目的端账户名(gitee)
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} # SSH密钥对中的私钥
dst_token: ${{ secrets.GITEE_TOKEN }} # Gitee账户的私人令牌
src_account_type: user
dst_account_type: user # 账户类型
clone_style: "https" # 使用https方式进行clone,也可以使用ssh
debug: true # 启用后会显示所有执行命令
force_update: true # 启用后,强制同步,即强制覆盖目的端仓库
static_list: "fzf" # 静态同步列表,在此填写需要同步的仓库名称,可填写多个
timeout: '600s' # git超时设置,超时后会自动重试git操作

118
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,118 @@
name: test
on:
push: ~
workflow_dispatch: ~
jobs:
test-ubuntu:
name: test on ubuntu
runs-on: ubuntu-latest
steps:
- name: checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: install dfs
run: |
rev=`git rev-parse HEAD`
pwd
set -x
./install.sh -adl
test `git rev-parse HEAD` = "$rev"
- name: antigen build
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: |
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
./install.sh -dl
- 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
test-macos:
name: test on macos
runs-on: macos-12
steps:
- name: checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: install dfs
run: |
rev=`git rev-parse HEAD`
pwd
set -x
./install.sh -adl
test `git rev-parse HEAD` = "$rev"
- name: antigen build
shell: /bin/zsh -ileo PIPE_FAIL {0}
run: |
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
./install.sh -dl
- 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
test-autodep:
name: test of auto-install dependencies
runs-on: ubuntu-latest
steps:
- name: remove dependencies
run: |
sudo apt-get -y remove curl vim python3
- name: checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: install dfs
run: |
rev=`git rev-parse HEAD`
set -x
./install.sh -dal
test `git rev-parse HEAD` = "$rev"
- name: antigen build with DFS_NO_WALL
shell: /bin/zsh -ileo PIPE_FAIL {0}
env:
DFS_NO_WALL: 1
run: |
echo $SHELL
antigen list

1
.gitignore vendored
View File

@@ -1 +1,2 @@
*.zwc
update.sh

View File

@@ -19,6 +19,8 @@ ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFO4k0tJ+Bfu95Uavg/5P3EXMKNcq+bMqaTqzkvRZ7ji
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN8g34WHLEix6Qt0J/ClYbZeb9wh+p1IOcZkz/vNbZrv ltp1/windows
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBN24pvN1RMN+iSZyHPdyExA1Rvt8pdr3e6ih6iX+KrQ ltp2
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGZFTyR+R86fUpZBDkDR9yYzJpmeNsIv2CMHTh6EuEeq pc0/windows
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID8WHIpwdZl7+HIZfQpwIAhpxT1huI9sd6Ydeokilg5l pc1/windows
@@ -26,3 +28,5 @@ ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID8WHIpwdZl7+HIZfQpwIAhpxT1huI9sd6Ydeokilg5l
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJ/+BWSTv3w1Ru9ksfse3EUbu4ukJruUU94fCG7Sk+5g3+F80KqJGykP9tHueU07iZfAuaYj7Zcy+IWx9Ih+eYA= ph0/sep
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNvHya5i3GNniGww9Yx5ikos4Z5jevu8zz8hCQi05++SgejFPnbw9UjbHFjODK2M+ZHuhRF5mdf996r4/BxWyP8= pad0/sep
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC5gu+U4picxVvCNYjLlmxGLpJl7svj3LklO7jSpwqdrNxlE+/xkx07PJx76AbA77dDM1Dxmm/VOvVQCKTRLTgk= ip14/sep

45
.update.sh Normal file
View File

@@ -0,0 +1,45 @@
#!/bin/bash
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
THIS_FILE=$(basename "${BASH_SOURCE}")
source "$THIS_DIR/tools/common.sh"
DFS_UPDATED_RET=${DFS_UPDATED_RET:-0}
DFS_UPDATE_CHANNEL=${DFS_UPDATE_CHANNEL:-"main"}
# fetch origin
cd $DOTFILES
git fetch --all --prune
if [[ -n "$(git status -s)" ]]; then
fmt_error "directory not clean"
apost_beacon "dfs.dirty"
exit
fi
# get the specified commit id
case $DFS_UPDATE_CHANNEL in
"main" ) DFS_COMMIT=$(curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id) ;;
"dev" ) DFS_COMMIT=$(git rev-parse origin/dev 2> /dev/null) || DFS_COMMIT=$(git rev-parse origin/main) ;;
"latest" ) DFS_COMMIT=$(git for-each-ref --sort=-committerdate refs/heads refs/remotes --format='%(objectname)' | head -n 1) ;;
* ) fmt_fatal "invalid update channel: $DFS_UPDATE_CHANNEL" ;;
esac
if [[ ${#DFS_COMMIT} != 40 ]]; then
fmt_error "invalid commit id"
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"
else
fmt_info "checking out to commit $DFS_COMMIT ..."
if [[ -z "$DFS_DEV" ]]; then
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
fmt_warning "won't really checkout in dev mode"
fi
fi

View File

@@ -16,6 +16,7 @@ set autoindent
set smartindent
set tabstop=4
set expandtab "spaces instead of tabs
set backspace=indent,eol,start
set number "line numbers
set history=1000
set ignorecase "when searching

212
.zshrc2
View File

@@ -1,22 +1,38 @@
# env for shell
export TERM="xterm-256color"
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'
# env for apps
export PYTHONIOENCODING='UTF-8'
export GPG_TTY=$(tty)
export LESS_TERMCAP_md=$'\E[01;33m'
# 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"
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,69 +56,43 @@ if [ ! -f "$ANTIGEN" ]; then
fi
# config and enable antigen
source "$ANTIGEN"
antigen use oh-my-zsh
# enable plugins
antigen_plugins=( \
"command-not-found" \
"docker" \
"extract" \
"fzf" \
"git" \
"ripgrep" \
"thefuck" \
"tmux" \
"ufw" \
"z" \
"https://gitee.com/dictxiong/zsh-syntax-highlighting" \
"https://gitee.com/dictxiong/zsh-completions" \
"https://gitee.com/dictxiong/zsh-autosuggestions" \
antigen use oh-my-zsh
ANTIGEN_PLUGINS+=(
"command-not-found"
"docker"
"extract"
"fzf"
"git"
"ripgrep"
"tmux"
"ufw"
"z"
)
for i in ${antigen_plugins[*]}
do
for i in ${ANTIGEN_PLUGINS[*]}; do
if [[ -z $ANTIGEN_EXCLUDE || ! $i =~ $ANTIGEN_EXCLUDE ]]; then
antigen bundle $i
fi
done
# select theme
antigen theme ${ZSH_THEME:-ys}
TMP_THEME=ys
if [[ -e /.dockerenv ]]; then
TMP_THEME=blinks
echo "==========================="
echo "This is in docker container"
echo "==========================="
fi
antigen theme ${ZSH_THEME:-$TMP_THEME}
# apply
antigen apply
# end of antigen config
# functions
export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
SUDO=''
if (( $EUID != 0 )); then
SUDO='sudo'
fi
dfs()
{
case $1 in
update ) (cd "$DOTFILES" && git pull) ;;
force-update ) (cd "$DOTFILES" && git fetch --all && git reset --hard origin/main && git pull) ;;
reset )
antigen reset 1> /dev/null
rm -rf $HOME/.antigen
$DOTFILES/install.sh -r
dfs update
$DOTFILES/install.sh -i
echo 'Done. Please open a new shell to see the changes.'
;;
cd ) cd "$DOTFILES" ;;
* ) echo "unknown command \"$1\". available: update, force-update, reset, cd" ;;
esac
}
dogo()
{
if [[ -z "$1" || "$1" =~ "-h|--help" ]]; then
echo "usage: dogo <container>"
# beacon
if [[ "$EUID" == "0" ]]; then
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.root"
else
$SUDO docker exec -it $1 zsh
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.user"
fi
}
# alias
alias "pls"='sudo $(fc -ln -1)'
@@ -110,14 +100,92 @@ alias "se"='sudo -sE'
alias "pbd"='ping baidu.com'
alias "p114"='ping 114.114.114.114'
alias "p666"='ping6 2001:da8::666'
alias l='ls -lAGh --time-style="+%y-%m-%d %H:%M"'
case $(bash "$DOTFILES/tools/common.sh" get_os_type) in
macos ) alias l='ls -lAGh -D "%y-%m-%d %H:%M"' ;;
* ) alias l='ls -lAGh --time-style="+%y-%m-%d %H:%M"' ;;
esac
if [[ -x $(command -v trash) ]]; then
alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash"
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 }
piv-agent()
{
eval $(ssh-agent -k)
# they are: ubuntu, macos, respectively
local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" )
for i in ${SO_PATHS[*]}; do
if [[ -f "$i" ]]; then
echo "using: $i"
eval $(ssh-agent -P "$i")
ssh-add -s "$i"
echo "now available keys:"
ssh-add -l
return 0
fi
done
echo "opensc-pkcs11.so not found"
return 1
}
# key bindings
bindkey "^b" beginning-of-line
bindkey "^e" end-of-line
bindkey "^[[H" beginning-of-line
bindkey "^[[F" end-of-line
bindkey "^[[1~" beginning-of-line
bindkey "^[[4~" end-of-line
bindkey "^[OH" beginning-of-line
bindkey "^[OF" end-of-line
bindkey "^b" beginning-of-line # ctrl+b
bindkey "\eOH" beginning-of-line
bindkey "\e[H" beginning-of-line # home; termius
bindkey "\e[1~" beginning-of-line
bindkey "^e" end-of-line # ctrl+e
bindkey "\eOF" end-of-line
bindkey "\e[F" end-of-line # end; termius
bindkey "\e[4~" end-of-line
bindkey "\ef" forward-word # alt+f
bindkey "\e[5C" forward-word
bindkey "\e\e[C" forward-word # alt+right_arrow; iterm2
bindkey "\e[1;5C" forward-word # alt+right_arrow; termius
bindkey "\eb" backward-word # alt+e
bindkey "\e[5D" backward-word
bindkey "\e\e[D" backward-word # alt+left_arrow; iterm2
bindkey "\e[1;5D" backward-word # alt+left_arrow; termius
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" ;;
version ) (cd "$DOTFILES" && git rev-parse HEAD) ;;
vversion ) (cd "$DOTFILES" && git show) ;;
config )
mkdir -p ~/.config/dotfiles
$EDITOR ~/.config/dotfiles/env
;;
force-update ) (
cd "$DOTFILES"
git fetch --all
ref=$(git symbolic-ref --short HEAD 2> /dev/null) || ref=$(git rev-parse --short HEAD 2> /dev/null) || return 0
for i in $(git for-each-ref refs/heads --format='%(refname)') ; do git checkout ${i#refs/heads/} ; git pull --ff-only ; done
git checkout -c advice.detachedHead=false $ref)
;;
reset )
antigen reset 1> /dev/null
rm -rf $HOME/.antigen
"$DOTFILES/install.sh" -r
dfs update
"$DOTFILES/install.sh" -i
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
}
# clean
unset i

View File

@@ -1,243 +1,267 @@
#!/bin/bash
# Color settings
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
if [ -t 1 ]; then
is_tty() {
true
}
else
is_tty() {
false
}
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/tools/common.sh"
if [[ ! "$DOTFILES" == "${HOME}"* ]]; then
fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..."
fi
DOTFILE_TILDE=${DOTFILES/"$HOME"/\~}
supports_truecolor() {
case "$COLORTERM" in
truecolor|24bit) return 0 ;;
esac
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\""
case "$TERM" in
iterm |\
tmux-truecolor |\
linux-truecolor |\
xterm-truecolor |\
screen-truecolor) return 0 ;;
esac
declare -a HOME_SYMLINKS_SRC
declare -a HOME_SYMLINKS_DST
HOME_SYMLINKS_SRC[0]=".ssh/authorized_keys2"
HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
return 1
}
fmt_fatal() {
printf '%sfatal: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "$FMT_RESET" >&2
exit
}
fmt_error() {
printf '%serror: %s%s\n' "${FMT_RED}" "$*" "$FMT_RESET" >&2
}
fmt_warning() {
echo "${FMT_YELLOW}warning: $1 ${FMT_RESET}"
}
fmt_note() {
echo "${FMT_GREEN}$1 ${FMT_RESET}"
}
setup_color() {
# Only use colors if connected to a terminal
if ! is_tty; then
FMT_RAINBOW=""
FMT_RED=""
FMT_GREEN=""
FMT_YELLOW=""
FMT_BLUE=""
FMT_BOLD=""
FMT_RESET=""
return
fi
if supports_truecolor; then
FMT_RAINBOW="
$(printf '\033[38;2;255;0;0m')
$(printf '\033[38;2;255;97;0m')
$(printf '\033[38;2;247;255;0m')
$(printf '\033[38;2;0;255;30m')
$(printf '\033[38;2;77;0;255m')
$(printf '\033[38;2;168;0;255m')
$(printf '\033[38;2;245;0;172m')
"
else
FMT_RAINBOW="
$(printf '\033[38;5;196m')
$(printf '\033[38;5;202m')
$(printf '\033[38;5;226m')
$(printf '\033[38;5;082m')
$(printf '\033[38;5;021m')
$(printf '\033[38;5;093m')
$(printf '\033[38;5;163m')
"
fi
FMT_RED=$(printf '\033[31m')
FMT_GREEN=$(printf '\033[32m')
FMT_YELLOW=$(printf '\033[33m')
FMT_BLUE=$(printf '\033[34m')
FMT_BOLD=$(printf '\033[1m')
FMT_RESET=$(printf '\033[0m')
}
# END of color settings
dotfile_path=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
home_slashes=${HOME//\//\\\/}
if [[ ! $dotfile_path == ${home_slashes}* ]]; then
fmt_fatal "\"$dotfile_path\" is not under \"$HOME\". aborting ..."
fi
dotfile_home_path=${dotfile_path/${home_slashes}/\~}
dotfile_relative_path=${dotfile_path#${home_slashes}\/}
crontab_job="0 * * * * cd ${dotfile_path} && env git pull"
ask_for_yN()
install_dependencies()
{
while true; do
read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
case $yn in
[Yy]* ) return 1;;
[Nn]* ) return 0;;
* ) return 0;;
fmt_note "installing dependencies ..."
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" )
DFS_LITE=$DFS_LITE "$DOTFILES/tools/macos.sh" brew-install
;;
"msys" )
DFS_LITE=$DFS_LITE "$DOTFILES/tools/msys2.sh" pacman-S
;;
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name)"
esac
}
preinstall_check()
{
fmt_note "checking requirements ..."
local mandatory_commands=( "git" "zsh" "curl" "grep" "cat" "cp" "bash" "mkdir" )
local optional_commands=( "vim" "tmux" "ping" )
for i in "${mandatory_commands[@]}"; do
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 ! command -v $i 1>/dev/null; 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
done
}
insert_if_not_exist()
install_file_content()
{
filename=$1
line=$2
fmt_note "installing \"$line\" into \"$filename\" ..."
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]}
fmt_info "installing \"$content\" into \"$filename\" ..."
mkdir -p $(dirname "$filename")
if [ ! -f "$filename" ]; then
touch $filename
fi
grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename"
grep -qxF -- "$content" "$filename" || echo "$content" >> "$filename"
done
}
delete_if_exist()
uninstall_file_content()
{
filename=$1
line=$2
fmt_note "removing \"$line\" from \"$filename\" ..."
fmt_note "uninstalling 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_info "removing \"$content\" from \"$filename\" ..."
if [ -f "$filename" ]; then
grep -vxF -- "$line" "$filename" | tee "$filename"
grep -vxF -- "$content" "$filename" | tee "$filename"
fi
done
}
create_symlink()
install_symlink()
{
src=$1
dest=$2
fmt_note "creating symlink \"$dest\" --> \"$src\" ..."
fmt_note "installing symlinks ..."
for ((i=0; i<${#HOME_SYMLINKS_SRC[@]}; i++)); do
local src="$DOTFILES/${HOME_SYMLINKS_SRC[$i]}"
local dst="$HOME/${HOME_SYMLINKS_DST[$i]}"
fmt_info "creating symlink \"$dst\" --> \"$src\" ..."
if [ ! -f "$src" ]; then
fmt_error "\"$src\" does not exist! aborting this job ..."
return 1
continue
fi
mkdir -p $(dirname "$dest")
if [ -f "$dest" ]; then
if [ "$(readlink $dest)" -ef "$src" ]; then
return 0
mkdir -p $(dirname "$dst")
if [ -f "$dst" ]; then
if [ "$(readlink $dst)" -ef "$src" ]; then
continue
fi
fmt_warning "\"$dest\" already exists! stat output:"
fmt_warning "\"$dst\" already exists! stat output:"
echo ----------
stat $dest
stat $dst
echo ----------
ask_for_yN "would you like to replace ${dest}?"
ask_for_yN "would you like to replace ${dst}?"
if [ $? -eq 1 ]; then
rm $dest
rm $dst
else
fmt_error "\"$dest\" already exists! aborting this job ..."
return 1
fmt_error "aborting this job ..."
continue
fi
fi
ln -s $src $dest
return $?
ln -s $src $dst
done
}
delete_link_if_match()
uninstall_symlink()
{
src=$1
dest=$2
if [ "$(readlink $dest)" -ef "$src" ]; then
fmt_note "removing symlink \"$dest\" ..."
fmt_note "uninstalling symlinks ..."
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_info "removing symlink \"$dst\" ..."
echo ----------
stat $dest
stat $dst
echo ----------
rm $dest
rm $dst
fi
done
}
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 -
else
fmt_warning "crontab does not exist. skipping ..."
fi
}
install_crontab(){
fmt_note "installing \"$crontab_job\" to crontab ..."
( crontab -l | grep -vxF "${crontab_job}" | grep -v "no crontab for"; echo "$crontab_job" ) | crontab -
uninstall_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
}
uninstall_crontab(){
fmt_note "removing \"$crontab_job\" from crontab ..."
( crontab -l | grep -vxF "$crontab_job" ) | crontab -
}
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 ..."
git clone https://gitee.com/dictxiong/tpm "$TMUX_TPM"
if [[ -x $(command -v g++) && -x $(command -v cmake) && -x $(command -v make) ]]; then
if [[ -z "$DFS_LITE" || "$DFS_LITE" == "0" ]]; then
fmt_note "initializing tmux plugins ..."
~/.tmux/plugins/tpm/bin/install_plugins
else
fmt_warning "in lite mode, tmux plugins are downloaded but not complied"
fmt_info "try <prefix + I> or ~/.tmux/plugins/tpm/bin/install_plugins to complie manually"
fi
else
fmt_warning "pls install g++,cmake,make and then init tmux plugins by <prefix + I> or ~/.tmux/plugins/tpm/bin/install_plugins"
fi
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 ..."
git clone https://gitee.com/dictxiong/Vundle.vim "$VIM_VUNDLE"
fmt_note "initializing vim plugins ..."
vim +PluginInstall +qall
echo | vim +PluginInstall +qall
fi
}
install(){
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=85 ${DOTFILES}/update.sh
if [[ $? == 85 ]]; then
fmt_note "dfs updated. re-running install.sh ..."
"${DOTFILES}/install.sh" "$ORIGIN_ARGS" && exit
fi
}
uninstall_update()
{
fmt_note "removing update.sh ..."
rm "${DOTFILES}/update.sh"
}
install()
{
if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi
install_update
preinstall_check
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 "${dotfile_path}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
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
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 "${dotfile_path}/.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
apost_beacon "dfs.uninstalled"
fmt_note "done uninstalling!"
}
setup_color
case $1 in
""|-i ) install ;;
-r ) uninstall ;;
* ) fmt_warning "unknown command \"$1\". available: -i, -r" ;;
ORIGIN_ARGS="$@"
parse_arg "$@"
FUNC=install
INSTALL_DEP=0
for i in ${PARSE_ARG_RET[@]}; do
case $i in
-i ) FUNC=install ;;
-r ) FUNC=uninstall ;;
-d|--dev ) export DFS_DEV=1 ;;
-l|--lite ) export DFS_LITE=1 ;;
-a|--auto ) INSTALL_DEP=1 ;;
-s|--secure ) export DFS_DEV=0 ;;
* ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a, -s" ;;
esac
done
$FUNC

30
scripts/dogo Executable file
View File

@@ -0,0 +1,30 @@
#!/bin/bash
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/../tools/common.sh"
DOCKER_FORMAT="{{.ID}} {{.Image}} {{.Names}}"
if [[ -z "$1" || "$1" =~ ^(-h|--help)$ ]]; then
fmt_info "usage: $0 <container> or -[0-9]*"
exit
elif [[ "$1" =~ ^-[0-9]*$ ]]; then
LAST="${1:1}"
if [[ "$LAST" == "0" ]]; then
# the latest running container
CONTAINER_META=($($SUDO docker container ls -l --filter "status=running" --format "$DOCKER_FORMAT"))
else
# the last nth container (all status)
CONTAINER_META=($($SUDO docker container ls -n ${LAST:-1} --format "$DOCKER_FORMAT" | tail -n 1))
fi
CONTAINER=${CONTAINER_META[0]}
else
CONTAINER_META="$1"
CONTAINER="$1"
fi
if [[ -z "$CONTAINER" ]]; then
fmt_fatal "container not found"
else
echo "--> ${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'
fi

25
scripts/doll Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/../tools/common.sh"
DOCKER_FORMAT="{{.ID}} {{.Repository}}:{{.Tag}}"
if [[ -z "$1" || "$1" =~ ^(-h|--help)$ ]]; then
fmt_info "usage: $0 <image> or -[0-9]*"
exit
elif [[ "$1" =~ ^-[0-9]*$ ]]; then
LAST="${1:1}"
# the last nth image
IMAGE_META=($($SUDO docker image ls --format "$DOCKER_FORMAT" | head -n ${LAST:-1} | tail -n 1))
IMAGE=${IMAGE_META[0]}
else
IMAGE_META="$1"
IMAGE="$1"
fi
if [[ -z "$IMAGE" ]]; then
fmt_fatal "image not found"
else
echo "--> ${IMAGE_META[@]}"
$SUDO docker run ${2:+"--name"} $2 -itd $IMAGE sh
fi

View File

@@ -1,29 +1,23 @@
#!/bin/bash
if [[ $USER != "root" ]]; then
echo "must run as root!"
exit 1
fi
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/(archive|security).ubuntu.com/${MIRROR}/g' /etc/apt/sources.list
echo "to-do ..."
MIRROR=${1:-"mirrors.tuna.tsinghua.edu.cn"}
sed -i "s@dl-cdn.alpinelinux.org@${MIRROR}@g" /etc/apk/repositories
}
apk_add()
{
apk update
# mass installation
apk add zsh git tmux vim curl wget bash python3 htop gcc g++ cmake make fzf perl linux-headers bind-tools iputils man-db
#for i in {fzf,ripgrep}; do apk add $i -y; done
# who am i
git config --global user.email "me@beardic.cn"
git config --global user.name "Dict Xiong"
# 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()
@@ -39,7 +33,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
}

227
tools/common.sh Executable file
View File

@@ -0,0 +1,227 @@
#!/bin/bash
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 source ~/.config/dotfiles/env; fi
# Color settings
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
if [ -t 1 ]; then
is_tty() {
true
}
else
is_tty() {
false
}
fi
supports_truecolor() {
case "$COLORTERM" in
truecolor|24bit) return 0 ;;
esac
case "$TERM" in
iterm |\
tmux-truecolor |\
linux-truecolor |\
xterm-truecolor |\
screen-truecolor) return 0 ;;
esac
return 1
}
fmt_fatal() {
printf '%sfatal: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "${FMT_RESET}" >&2
exit 1
}
fmt_error() {
printf '%serror: %s%s\n' "${FMT_RED}" "$*" "${FMT_RESET}" >&2
}
fmt_warning() {
printf '%swarning: %s%s\n' "${FMT_YELLOW}" "$*" "${FMT_RESET}" >&2
}
fmt_info() {
printf '%sinfo: %s\n' "${FMT_RESET}" "$*" >&1
}
fmt_note() {
printf '%s%s%s\n' "${FMT_GREEN}" "$*" "${FMT_RESET}" >&1
}
setup_color() {
# Only use colors if connected to a terminal
if ! is_tty; then
FMT_RAINBOW=""
FMT_RED=""
FMT_GREEN=""
FMT_YELLOW=""
FMT_BLUE=""
FMT_BOLD=""
FMT_RESET=""
return
fi
if supports_truecolor; then
FMT_RAINBOW="
$(printf '\033[38;2;255;0;0m')
$(printf '\033[38;2;255;97;0m')
$(printf '\033[38;2;247;255;0m')
$(printf '\033[38;2;0;255;30m')
$(printf '\033[38;2;77;0;255m')
$(printf '\033[38;2;168;0;255m')
$(printf '\033[38;2;245;0;172m')
"
else
FMT_RAINBOW="
$(printf '\033[38;5;196m')
$(printf '\033[38;5;202m')
$(printf '\033[38;5;226m')
$(printf '\033[38;5;082m')
$(printf '\033[38;5;021m')
$(printf '\033[38;5;093m')
$(printf '\033[38;5;163m')
"
fi
FMT_RED=$(printf '\033[31m')
FMT_GREEN=$(printf '\033[32m')
FMT_YELLOW=$(printf '\033[33m')
FMT_BLUE=$(printf '\033[34m')
FMT_BOLD=$(printf '\033[1m')
FMT_RESET=$(printf '\033[0m')
}
# END of color settings
SUDO=''
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
SUDO='sudo'
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
read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
case $yn in
[Yy]* ) return 1;;
* ) return 0;;
esac
done
return 0
}
ask_for_Yn()
{
while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do
read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn
case $yn in
[Nn]* ) return 0;;
* ) return 1;;
esac
done
return 1
}
post_log()
{
if [[ $# != 3 || -z "$1" || -z "$2" || -z "$3" ]]; then
fmt_fatal "usage: post_log <level> <section> <content>"
fi
"${DOTFILES}/tools/logger.sh" "log" "[$1][$2] $3"
}
apost_log()
{
post_log "$@" 1>/dev/null &
}
post_beacon()
{
if [[ $# != 1 || -z "$1" ]]; then
fmt_fatal "usage: post_beacon <beacon>"
fi
"${DOTFILES}/tools/logger.sh" "beacon" "$1"
}
apost_beacon()
{
post_beacon "$@" 1>/dev/null &
}
get_os_type()
{
local ans="unknown"
case "$(uname -s)" in
Darwin*) ans="MacOS";;
CYGWIN*) ans="Cygwin";;
MSYS* ) ans="MSYS";;
Linux* ) ans="Linux";;
*) ans="unknown";;
esac
echo $ans | tr '[:upper:]' '[:lower:]'
}
get_linux_dist()
{
local ans="unknown"
if [ -f /etc/os-release ]; then
. /etc/os-release
ans="$ID"
elif type lsb_release >/dev/null 2>&1; then
ans="$(lsb_release -si)"
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
ans="$DISTRIB_ID"
elif [ -f /etc/debian_version ]; then
ans="Debian"
elif [ -f /etc/SuSe-release ]; then
ans="SUSE"
elif [ -f /etc/redhat-release ]; then
ans="RedHat"
else
ans="unknown"
fi
echo $ans | tr '[:upper:]' '[:lower:]'
}
get_os_name()
{
local ans=$(get_os_type)
if [[ "$ans" == "linux" ]]; then
ans=$(get_linux_dist)
fi
echo $ans
}
# if bash-ed, else source-d
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
$1 "${@:2}"
else
setup_color
fi
unset THIS_DIR_COMMON_SH

100
tools/logger.sh Executable file
View File

@@ -0,0 +1,100 @@
#!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
if [[ -x $(command -v hostname) ]]; then
hostname=$(hostname)
elif [[ -x $(command -v uname) ]]; then
hostname=$(uname -n)
elif [[ -x $(command -v hostnamectl) ]]; then
hostname=$(hostnamectl --static)
elif [[ -n "$HOSTNAME" ]]; then
hostname=$HOSTNAME
elif [[ -f /proc/sys/kernel/hostname ]]; then
hostname=$(cat /proc/sys/kernel/hostname)
elif [[ -f /etc/hostname ]]; then
hostname=$(cat /etc/hostname)
else
fmt_fatal "unable to get hostname"
fi
init_uuid()
{
if [[ -f ~/.config/dotfiles/uuid ]]; then
uuid=$(cat ~/.config/dotfiles/uuid)
else
if [[ -x $(command -v uuidgen) ]]; then
uuid=$(uuidgen)
elif [[ -f /proc/sys/kernel/random/uuid ]]; then
uuid=$(cat /proc/sys/kernel/random/uuid)
else
fmt_fatal "unable to generate uuid"
fi
mkdir -p ~/.config/dotfiles
echo "$uuid" > ~/.config/dotfiles/uuid
fi
}
post_beacon()
{
local beacon_type=$1
if [[ -z "$beacon_type" ]]; then
fmt_fatal "beacon type is required"
fi
resp=$(curl -sSL -X POST "https://api.beardic.cn/post-beacon?hostname=$hostname&beacon=$beacon_type")
if grep -q "200" <<< "$resp"; then
echo $resp
else
echo $resp >&2
fmt_fatal "error posting beacon"
fi
}
post_log()
{
local log_content=$1
if [[ -z "$log_content" ]]; then
fmt_fatal "log content is required"
fi
init_uuid
resp=$(curl -sSL -X POST -H "Content-Type: text/plain" -d "$1" "https://api.beardic.cn/post-log?hostname=$hostname&uuid=$uuid")
if grep -q "200" <<< "$resp"; then
echo $resp
elif grep -q "403" <<< "$resp"; then
echo $resp >&2
fmt_error "error posting log: authentification failed"
fmt_info "try to register you hostname and uuid"
fmt_info "hostname: $hostname"
fmt_info "uuid: $uuid"
else
echo $resp >&2
fmt_fatal "error posting log"
fi
}
print_help()
{
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
}
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

20
tools/macos.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
brew_install()
{
brew update
brew install git zsh curl tmux vim util-linux
}
router()
{
case $1 in
brew-install ) brew_install ;;
* ) echo unknown command "$1". available: brew-install;;
esac
}
router $@

View File

@@ -1,9 +1,7 @@
#!/bin/bash
if [[ $USER != "root" ]]; then
echo "must run as root!"
exit 1
fi
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
set_mirror()
{
@@ -16,7 +14,12 @@ 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
# lite
pacman -S tmux git zsh bash curl vim
# full
if [[ -z "$DFS_LITE" ]]; then
pacman -S wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip man-pages-posix
fi
}
router()

52
tools/test.zsh Normal file
View File

@@ -0,0 +1,52 @@
#!/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
grep -q ".zshrc2" ~/.zshrc
# check scripts and functions
dfs version
dfs log 1
dfs beacon gh.ci
z ~
test ~ -ef "$(pwd)"
dogo
doll
dfs cd
tools/common.sh get_os_type
tools/common.sh get_linux_dist
# check alias
alias p114
which sibd
piv-agent || which piv-agent
gbes || which gbes
# 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
git reset --hard $DFS_VERSION
# then check install.sh
./install.sh -l
dfs version
test `git rev-parse HEAD` = `curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id`
# clean
git reset --hard $DFS_VERSION
set +x

View File

@@ -1,13 +1,13 @@
#!/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://github.com/junegunn/fzf.git ~/.fzf && ~/.fzf/install" \
[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}" \
[oh-my-zsh]='sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"' \
[oh-my-tuna]='wget https://tuna.moe/oh-my-tuna/oh-my-tuna.py && sudo python oh-my-tuna.py --global' \
[v2ray]="bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) #--remove" \
[v2fly]="bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) #--remove" \
[zerotier-one]='curl -s https://install.zerotier.com | sudo bash' \
[docker-ce]='curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh #--mirror Aliyun #--dry-run' \
[lemonbench]='curl -fsSL https://ilemonra.in/LemonBenchIntl | bash -s fast # or full' \
@@ -15,7 +15,7 @@ install_commands=(\
install()
{
echo -e ${install_commands[$1]}
echo -e ${INSTALL_COMMANDS[$1]}
}
install $1

View File

@@ -1,26 +1,25 @@
#!/bin/bash
if [[ $USER != "root" ]]; then
echo "must run as root!"
exit 1
fi
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/(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()
{
# basic packages
apt update
for i in {man-db,vim,ca-certificates}; do apt install $i -y; done
# mass installation
apt install git tmux zsh curl wget dialog net-tools dnsutils netcat traceroute sudo python3 python3-pip cron inetutils-ping openssh-client openssh-server htop gcc g++ cmake make zip
for i in {fzf,ripgrep}; do apt install $i -y; done
apt-get update -y
# lite
apt-get install -y git zsh bash tmux vim curl inetutils-ping less bsdmainutils
# full
if [[ -z "$DFS_LITE" ]]; 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
}
set_timezone()