From 19f5dcb738bf098cb2cf6985369e55df9728c797 Mon Sep 17 00:00:00 2001 From: Recolic <git@me.recolic.net> Date: Fri, 4 Apr 2025 15:18:03 -0700 Subject: [PATCH] .rename lcf_xx to lc_xx, bump ver --- examples/archlinux-gnome/conf.d/10_desktop.sh | 4 +- examples/template/masterconf.sh | 4 +- linuxconf | 66 +++++++++---------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/examples/archlinux-gnome/conf.d/10_desktop.sh b/examples/archlinux-gnome/conf.d/10_desktop.sh index f589be1..a84241d 100644 --- a/examples/archlinux-gnome/conf.d/10_desktop.sh +++ b/examples/archlinux-gnome/conf.d/10_desktop.sh @@ -45,7 +45,7 @@ user_pref("browser.tabs.hoverPreview.enabled", false);' [[ -d "$dir" ]] && echo "$firefox_config" > "$dir/user.js" done - lcf_bgrun /tmp/ddns.log auto_restart bash -c "curl https://recolic.net/tmp/example-ddns-api.log ; sleep 300" + lc_bgrun /tmp/ddns.log auto_restart bash -c "curl https://recolic.net/tmp/example-ddns-api.log ; sleep 300" [[ -f /usr/bin/az ]] && az config set core.login_experience_v2=off || true } @@ -54,6 +54,6 @@ lc_login () { # echo _:1 | bash /usr/mybin/unlock_keyrings echo "$(date) test-only: lc_login called" >> /tmp/note chmod 777 /tmp/note - lcf_echo2 "detected login as $(whoami). it's running as expected!" + lc_echo2 "detected login as $(whoami). it's running as expected!" } diff --git a/examples/template/masterconf.sh b/examples/template/masterconf.sh index c7bf86b..1b282a7 100644 --- a/examples/template/masterconf.sh +++ b/examples/template/masterconf.sh @@ -36,9 +36,9 @@ lc_startup () { # You want desktop env? Use lc_login() # want some daemon in background? - lcf_bgrun /tmp/server.log my_server --arg1 123 --arg2 "hello world !" + lc_bgrun /tmp/server.log my_server --arg1 123 --arg2 "hello world !" # Guaranteed! linuxconf dir is your current workdir. - lcf_bgrun /dev/null auto_restart important_service --config files/test.conf + lc_bgrun /dev/null auto_restart important_service --config files/test.conf } lc_login () { diff --git a/linuxconf b/linuxconf index 67f7d04..c76f136 100755 --- a/linuxconf +++ b/linuxconf @@ -1,20 +1,20 @@ #!/bin/bash # Useful built-in functions for both linuxconf and user scripts. -function lcf_echo2 () { +function lc_echo2 () { echo "$@" 1>&2 } -function lcf_err () { - lcf_echo2 Error: "$@" +function lc_err () { + lc_echo2 Error: "$@" return 1 } -function lcf_die () { - lcf_err "$@" +function lc_die () { + lc_err "$@" exit 1 } -function lcf_bgrun () { - # Usage: lcf_bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx - # Usage: lcf_bgrun /var/log/your.log auto_restart frpc -c my_server.ini +function lc_bgrun () { + # Usage: lc_bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx + # Usage: lc_bgrun /var/log/your.log auto_restart frpc -c my_server.ini # v202504-1 local logF="$1" shift @@ -44,13 +44,13 @@ function lc_include () { # print the return code of the eval-ed function to stderr, but this function should always return success. for script in "$@"; do - lcf_echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)" + lc_echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)" ( source "$script" declare -F "$LCI_SUBSHELL_OP" >/dev/null 2>&1 || exit 0 eval "$LCI_SUBSHELL_OP" ) - lcf_echo2 "$script -- $LCI_SUBSHELL_OP completed with status $?" + lc_echo2 "$script -- $LCI_SUBSHELL_OP completed with status $?" # TODO: should I abort on failure? It will stop running other include files. Don't do it now. done @@ -123,7 +123,7 @@ function lci_overwrite_conf () { local confpath="$2" # user masterconf script path local newpath="$(realpath "$confpath")" - [ "$newpath" != "" ] || lcf_err "lci_overwrite_conf: cannot unfold path $confpath. Permission error?" || return $? + [ "$newpath" != "" ] || lc_err "lci_overwrite_conf: cannot unfold path $confpath. Permission error?" || return $? # Don't re-create if old config already looks good. In this case, init_done should be preserved. TODO: rename lc register to lc init, then should I remove this behavior?? if [[ -f "$fname" ]]; then @@ -131,26 +131,26 @@ function lci_overwrite_conf () { [[ "$oldpath" = "$newpath" ]] && return 0 fi - echo -e "#autogenerated config, could be overwritten without warning.\nmasterconf=$newpath" | tee "$fname" > /dev/null || lcf_err "lci_overwrite_conf: unable to create $fname" || return $? + echo -e "#autogenerated config, could be overwritten without warning.\nmasterconf=$newpath" | tee "$fname" > /dev/null || lc_err "lci_overwrite_conf: unable to create $fname" || return $? chmod ugo+rw "$fname" } function lci_register () { local confpath="$1" - [[ -f "$confpath" ]] || lcf_die "lci_register: $confpath not exist" + [[ -f "$confpath" ]] || lc_die "lci_register: $confpath not exist" # For new register (or register a different dir), clear all existing conf. This will trigger init again. - lci_overwrite_conf /etc/linuxconf.conf "$confpath" || lcf_die "lci_register cannot write new conf" + lci_overwrite_conf /etc/linuxconf.conf "$confpath" || lc_die "lci_register cannot write new conf" } function lci_call () { # calls an lc function in masterconf (and included subconf) - [ "$1" = "" ] && lcf_die "logic error: lci_call without arg" + [ "$1" = "" ] && lc_die "logic error: lci_call without arg" export LCI_SUBSHELL_OP="$1" - local masterconf="$(lci_conf_get_masterconf_path /etc/linuxconf.conf)" || lcf_die "unable to call lc_init. Cannot read masterconf path from /etc/linuxconf.conf" + local masterconf="$(lci_conf_get_masterconf_path /etc/linuxconf.conf)" || lc_die "unable to call lc_init. Cannot read masterconf path from /etc/linuxconf.conf" local workdir="$(dirname "$masterconf")" - cd "$workdir" || lcf_die "unable to enter config directory: $workdir" + cd "$workdir" || lc_die "unable to enter config directory: $workdir" lc_include "$masterconf" export LCI_SUBSHELL_OP=__lc_operation_undefined } @@ -161,7 +161,7 @@ function lci_init_if_needed () { lci_call lc_init - lci_state_file_append /etc/linuxconf.conf init_done "$uname" || lcf_die "lc_init functions succeeded, but unable to update /etc/linuxconf.conf" + lci_state_file_append /etc/linuxconf.conf init_done "$uname" || lc_die "lc_init functions succeeded, but unable to update /etc/linuxconf.conf" } function lci_startup_if_needed () { @@ -169,20 +169,20 @@ function lci_startup_if_needed () { local state_file="/tmp/.linuxconf-state-$uname" if [[ ! -f $state_file ]]; then - touch $state_file && chmod ugo+rw $state_file || lcf_die "failed to create tmp file $state_file" + touch $state_file && chmod ugo+rw $state_file || lc_die "failed to create tmp file $state_file" fi lci_state_file_contains $state_file startup_done "$uname" && return 0 lci_call lc_startup - lci_state_file_append $state_file startup_done "$uname" || lcf_die "lc_startup functions succeeded, but unable to update $state_file" + lci_state_file_append $state_file startup_done "$uname" || lc_die "lc_startup functions succeeded, but unable to update $state_file" } function lci_usage () { - lcf_echo2 "linuxconf v$lci_version" - lcf_echo2 "This tool helps you manage all linux customization in one centralized directory, making backup/sync much easier." - lcf_echo2 "Usage: Run '$0 register <path/to/masterconf.sh>', then it will work out-of-box." + lc_echo2 "linuxconf v$lci_version" + lc_echo2 "This tool helps you manage all linux customization in one centralized directory, making backup/sync much easier." + lc_echo2 "Usage: Run '$0 register <path/to/masterconf.sh>', then it will work out-of-box." exit 1 } @@ -207,14 +207,14 @@ function lci_install_startup_hook () { elif [ -f /etc/rc.local ]; then echo '/usr/bin/linuxconf _startup_all' >> /usr/bin/linuxconf _startup_all else - lcf_err "neither systemd nor /etc/rc.local available." + lc_err "neither systemd nor /etc/rc.local available." fi || return $? #TODO: check if any user installed non-root startup hook else [ -f /etc/systemd/system/lc-hook.service ] && return 0 grep '/usr/bin/linuxconf _startup_all' /etc/rc.local >/dev/null 2>&1 && return 0 # TODO: install non-root startup hook - lcf_die "non-root startup hook not supported yet" + lc_die "non-root startup hook not supported yet" fi } function lci_install_login_hook () { @@ -245,17 +245,17 @@ function lci_install_login_hook () { fi } -lci_version=0.2.1 +lci_version=0.2.2 subcommand="$1" if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then - [[ ! -f /etc/linuxconf.conf ]] && lcf_die "Please run '$0 register <path/to/masterconf.sh>' at least once" + [[ ! -f /etc/linuxconf.conf ]] && lc_die "Please run '$0 register <path/to/masterconf.sh>' at least once" fi if [[ "$subcommand" = register ]]; then - lcf_echo2 "Confirm linuxconf init as $(whoami) at $(dirname "$(realpath "$2")") ? Make sure all (desired) user have read access!" + lc_echo2 "Confirm linuxconf init as $(whoami) at $(dirname "$(realpath "$2")") ? Make sure all (desired) user have read access!" read -p "Confirm ? " || exit 0 lci_register "$2" - lci_install_startup_hook || lcf_die "failed to install on_startup hook" - lci_install_login_hook || lcf_die "failed to install on_login hook" + lci_install_startup_hook || lc_die "failed to install on_startup hook" + lci_install_login_hook || lc_die "failed to install on_login hook" lci_init_if_needed lci_startup_if_needed elif [[ "$subcommand" = _cron ]]; then @@ -265,10 +265,10 @@ elif [[ "$subcommand" = _startup ]]; then lci_startup_if_needed elif [[ "$subcommand" = _startup_all ]]; then # systemd should call this service as root, and it will spawn subprocess for all users with sudo - [[ "$(whoami)" != root ]] && lcf_die "$0 _startup_all started as non-root. Exit because sudo might fail." - ar_uname=($(lci_state_file_list /etc/linuxconf.conf init_done)) || lcf_die "List all initialized users: lci_state_file_list failed" + [[ "$(whoami)" != root ]] && lc_die "$0 _startup_all started as non-root. Exit because sudo might fail." + ar_uname=($(lci_state_file_list /etc/linuxconf.conf init_done)) || lc_die "List all initialized users: lci_state_file_list failed" for uname in "${ar_uname[@]}"; do - lcf_echo2 "Spawn subprocess '$0 _startup' as user $uname..." + lc_echo2 "Spawn subprocess '$0 _startup' as user $uname..." sudo -u "$uname" "$0" _startup done elif [[ "$subcommand" = _xdg_login ]]; then -- GitLab