From 79d376265eb8588429200d15620a6a5712a1b47c Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Wed, 6 Dec 2023 21:52:54 +0800 Subject: [PATCH] (experimental) riot config in a single file --- riot-config.sh | 38 +++++++++++++++++++++++++++++++++++ scripts/riot | 30 ++++++++++++++++++--------- scripts/riot.d/default.domain | 1 - scripts/riot.d/i.domain | 4 ---- scripts/riot.d/j.remote | 4 ---- scripts/riot.d/nasp.domain | 5 ----- scripts/riot.d/x.domain | 6 ------ tools/common.sh | 4 ++++ 8 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 riot-config.sh delete mode 120000 scripts/riot.d/default.domain delete mode 100644 scripts/riot.d/i.domain delete mode 100644 scripts/riot.d/j.remote delete mode 100644 scripts/riot.d/nasp.domain delete mode 100644 scripts/riot.d/x.domain diff --git a/riot-config.sh b/riot-config.sh new file mode 100644 index 0000000..b0bc063 --- /dev/null +++ b/riot-config.sh @@ -0,0 +1,38 @@ +#!/bin/false + +# remotes +j.remote() { + remote=sir0.ibd.ink + RET_PORT=${RET_PORT:-36122} + RET_USERNAME=${RET_USERNAME:-root} + RET_TRUST_SERVER=1 +} + +# domains +i.domain() { + RET_HOSTNAME=$host.ibd.ink + RET_PORT=${RET_PORT:-12022} + RET_USERNAME=${RET_USERNAME:-root} + RET_TRUST_SERVER=1 +} + +x.domain() { + RET_HOSTNAME=ssh.beardic.cn + local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]") + tmp=${tmp:0:4} + RET_PORT=$((10#$tmp+36000)) + RET_USERNAME=root + RET_TRUST_SERVER=1 +} + +nasp.domain() { + RET_HOSTNAME=$host + RET_PORT=${RET_PORT:-12022} + RET_USERNAME=${RET_USERNAME:-dictxiong} + RET_JUMP_SERVER="ssh@nasp.ob.ac.cn:36022" + RET_TRUST_SERVER=1 +} + +default.domain() { + i.domain +} diff --git a/scripts/riot b/scripts/riot index c0720f7..10c7561 100755 --- a/scripts/riot +++ b/scripts/riot @@ -4,9 +4,20 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) source "$THIS_DIR/../tools/common.sh" RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}} RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0} -RIOT_CONFIG_DIR="$THIS_DIR/riot.d" RIOT_EXTRA_OPTIONS="" +# config +RIOT_CONFIG_FILES=( + "$DOTFILES/riot-config.sh" + "$HOME/.config/riot-config.sh" + "riot-config.sh" +) +for file in "${RIOT_CONFIG_FILES[@]}"; do + if [[ -f "$file" ]]; then + source "$file" + fi +done + # check if port number valid check_port() { ( echo $1 | grep -qxE "[1-9][0-9]{0,4}" ) || return 1 @@ -44,9 +55,9 @@ get_server_meta() { check_port $RET_PORT || fmt_fatal invalid port number \"$RET_PORT\" fi # presets -- match remote - local remote_config="$RIOT_CONFIG_DIR/$remote.remote" - if [[ -f "$remote_config" ]]; then - source "$remote_config" + local remote_func="$remote.remote" + if is_function "$remote_func"; then + "$remote_func" fi # presets -- match domain RET_HOSTNAME=${remote} @@ -56,13 +67,12 @@ get_server_meta() { if [[ "$host" == "$domain" && "$host" != "["*"]" ]]; then domain="default" fi - local root_domain_config="$RIOT_CONFIG_DIR/.domain" - if [[ -f "$root_domain_config" ]]; then - source "$root_domain_config" + if is_function ".domain"; then + ".domain" fi - local domain_config="$RIOT_CONFIG_DIR/$domain.domain" - if [[ -n "$$domain" && -f "$domain_config" ]]; then - source "$domain_config" + local domain_func="$domain.domain" + if [[ -n "$domain" ]] && is_function "$domain_func"; then + "$domain_func" fi } diff --git a/scripts/riot.d/default.domain b/scripts/riot.d/default.domain deleted file mode 120000 index 04cc35d..0000000 --- a/scripts/riot.d/default.domain +++ /dev/null @@ -1 +0,0 @@ -./i.domain \ No newline at end of file diff --git a/scripts/riot.d/i.domain b/scripts/riot.d/i.domain deleted file mode 100644 index 6315e28..0000000 --- a/scripts/riot.d/i.domain +++ /dev/null @@ -1,4 +0,0 @@ -RET_HOSTNAME=$host.ibd.ink -RET_PORT=${RET_PORT:-12022} -RET_USERNAME=${RET_USERNAME:-root} -RET_TRUST_SERVER=1 \ No newline at end of file diff --git a/scripts/riot.d/j.remote b/scripts/riot.d/j.remote deleted file mode 100644 index eb2a647..0000000 --- a/scripts/riot.d/j.remote +++ /dev/null @@ -1,4 +0,0 @@ -remote=sir0.ibd.ink -RET_PORT=${RET_PORT:-36122} -RET_USERNAME=${RET_USERNAME:-root} -RET_TRUST_SERVER=1 \ No newline at end of file diff --git a/scripts/riot.d/nasp.domain b/scripts/riot.d/nasp.domain deleted file mode 100644 index d33036a..0000000 --- a/scripts/riot.d/nasp.domain +++ /dev/null @@ -1,5 +0,0 @@ -RET_HOSTNAME=$host -RET_PORT=${RET_PORT:-12022} -RET_USERNAME=${RET_USERNAME:-dictxiong} -RET_JUMP_SERVER="ssh@nasp.ob.ac.cn:36022" -RET_TRUST_SERVER=1 \ No newline at end of file diff --git a/scripts/riot.d/x.domain b/scripts/riot.d/x.domain deleted file mode 100644 index 8389866..0000000 --- a/scripts/riot.d/x.domain +++ /dev/null @@ -1,6 +0,0 @@ -RET_HOSTNAME=ssh.beardic.cn -local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]") -tmp=${tmp:0:4} -RET_PORT=$((10#$tmp+36000)) -RET_USERNAME=root -RET_TRUST_SERVER=1 \ No newline at end of file diff --git a/tools/common.sh b/tools/common.sh index ddae15f..2705aca 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -258,6 +258,10 @@ get_free_port() { echo $port } +is_function() { + test "$(type -t "$1")" = "function" +} + # if bash-ed, else source-d if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then $1 "${@:2}"