From 22b3c0cc36cc4eeccb1c2422efb7a7b17f6b2c21 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Thu, 3 Dec 2020 20:48:04 -0800 Subject: [PATCH] added debug module with debug function - a first cut load.sh now loads debug module or a noop file library, file.lib - refactored _find function, added alias superceed * added h option to include .hidden files * - added build_file function to append file to another refactored comment and uncomment functions added find_mountpoint function to filesystem module added ssh_config builder function and ssh override to use it. added github module with binary release fetch function (needs work) --- alias/superceed | 4 ++ env/02-defaults.env | 1 - function/comment | 16 +++---- function/ssh | 78 ++++++++++++++++++++++++++++++++++ load.sh | 15 +++++-- module.lib | 2 +- modules/github-bin-fetch.sh | 30 +++++++++++++ modules/utility/debug.lib | 8 ++++ modules/utility/file.lib | 52 +++++++++++++++-------- modules/utility/filesystem.mod | 11 +++++ setup/etc/bash.bashrc | 3 ++ setup/user/.bashrc | 2 +- shell.env | 1 + ssh/config/readme.md | 1 + ssh/{ => session}/readme.md | 2 +- 15 files changed, 192 insertions(+), 34 deletions(-) create mode 100644 alias/superceed create mode 100644 function/ssh create mode 100644 modules/github-bin-fetch.sh create mode 100644 modules/utility/debug.lib create mode 100644 ssh/config/readme.md rename ssh/{ => session}/readme.md (81%) diff --git a/alias/superceed b/alias/superceed new file mode 100644 index 0000000..7b667ab --- /dev/null +++ b/alias/superceed @@ -0,0 +1,4 @@ +# any binary superceed by a function with +# initial binary available as s as in super +alias find="_find" +alias sfind="command find" diff --git a/env/02-defaults.env b/env/02-defaults.env index ce2c079..8058f21 100644 --- a/env/02-defaults.env +++ b/env/02-defaults.env @@ -2,5 +2,4 @@ # export EDITOR=atom # export ELECTRON_TRASH=gio export EDITOR=nano - export BROWSER=google-chrome diff --git a/function/comment b/function/comment index 15fda3f..abe085e 100644 --- a/function/comment +++ b/function/comment @@ -1,14 +1,10 @@ #!/bin/bash -function comment_line() { - local regex="${1:?}" - local file="${2:?}" - local comment_mark="${3:-#}" - sed -ri "s:^([ ]*)($regex):\\1$comment_mark\\2:" "$file" +function comment() { + [[ $# -lt 2 ]] && echo must supply pattern and file && return 1 + sed -i '/'$1'/s/^#*/#/g' "$2" } -function uncomment_line() { - local regex="${1:?}" - local file="${2:?}" - local comment_mark="${3:-#}" - sed -ri "s:^([ ]*)[$comment_mark]+[ ]?([ ]*$regex):\\1\\2:" "$file" +function uncomment() { + [[ $# -lt 2 ]] && echo must supply pattern and file && return 1 + sed -i '/'$1'/s/^#*//g' "$2" } diff --git a/function/ssh b/function/ssh new file mode 100644 index 0000000..6c87616 --- /dev/null +++ b/function/ssh @@ -0,0 +1,78 @@ +#!/bin/bash +# this will superceed the ssh binary in order to source all the config files +module_load file # loads find and build_file + +function ssh_config () { +local CDIRS +local CDIR +local DIRS +local DIR +local PDIRS + +declare OPTION +declare OPTARG +declare OPTIND +while getopts 'd:' OPTION; do + # echo $OPTION $OPTARG +case "$OPTION" in + d) + PDIRS=$OPTARG + # echo option d: $DIRS + ;; + *) + echo unknown option $OPTION + ;; +esac +done + +shift $(( OPTIND - 1 )) + +local OUTPUT=${*:-"$BASH_SHELL_BASE/ssh/config/_config"} +[[ $PDIRS ]] && DIRS=($PDIRS) || DIRS=(${BASH_SHELL_DIRS} "$HOME/$BASH_SHELL_USER") +# echo DIRS "${DIRS[@]}" +# echo $OUTPUT +CDIRS=() +j=0 +cnt=${#DIRS[@]} +for ((i=0;i $OUTPUT +build_file "/etc/ssh/ssh_config" $OUTPUT +# echo existing dirs ${CDIRS[@]} +for CDIR in "${CDIRS[@]}" +do + # FILES=$(find -n '*.cfg' -d 0 $CDIR) + for f in $(find -n '*.cfg' -p 'archive off' -d 0 $CDIR) ; + do + # echo "Processing $f"; + [[ $f ]] && build_file "$f" $OUTPUT + done +done +build_file "$HOME/.ssh/config" $OUTPUT +} + +ssh () { + if [[ $1 = "-F" ]]; then + CONFIG=${2:-"$BASH_SHELL_BASE/ssh/config/_config"} + shift;shift + fi + CONFIG=${CONFIG:-"$BASH_SHELL_BASE/ssh/config/_config"} + [[ -f "$CONFIG" ]] || ssh_config "$CONFIG" + command ssh -F $CONFIG "$@" +} diff --git a/load.sh b/load.sh index 1620afd..6003c08 100755 --- a/load.sh +++ b/load.sh @@ -1,7 +1,7 @@ #!/bin/bash # [[ ! $- == *i* ]] && exec >> ~/logs/load.log && exec 2>&1 - +# export BASH_DEBUG=true function nilog () { [[ ! $- == *i* ]] && echo -e "-----\n $USER $*" &>> ~/logs/load.log } @@ -28,6 +28,15 @@ DIR=${1:-$(dirname ${BASH_SOURCE[0]})} module_load file [[ $? -ne 0 ]] && echo unable to access the file module, aboarting load && return 1 +module_load debug +if [[ $? -ne 0 ]]; then +echo "unable to load a 'debug' module using a noop for debug function" +# noop +function debug () { +: +} +fi + ([[ $BASH_ENV ]] && [[ ! $- == *i* ]] && [[ $BASH_USE_ALIAS ]]) && shopt -s expand_aliases function shell_process_directory () { @@ -92,12 +101,12 @@ unset BASH_SHELL_BASE_LOADED unset BASH_SHELL_LOADED # echo bash shell dirs: $BASH_SHELL_DIRS -DIRS=${1:-$BASH_SHELL_DIRS} +dirs=${1:-$BASH_SHELL_DIRS} BASH_SHELL_BASE_SUBDIRS=$(cat "$BASH_SHELL_BASE/.bash-shell-include") # echo subdir includes: $BASH_SHELL_BASE_SUBDIRS -for dir in $DIRS; do +for dir in $dirs; do # echo $dir shell_process_directory $dir [[ $dir == "$BASH_SHELL_BASE" ]] && BASH_SHELL_BASE_LOADED=true diff --git a/module.lib b/module.lib index 98cbed1..6892a51 100644 --- a/module.lib +++ b/module.lib @@ -7,7 +7,7 @@ local NAME DIR=$1 NAME=$2 # echo finding $NAME in $DIR -FILE="$(find ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh)" +FILE="$(command find ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh)" # echo files found $FILE COUNT=0 if [ "$FILE" ]; then diff --git a/modules/github-bin-fetch.sh b/modules/github-bin-fetch.sh new file mode 100644 index 0000000..52f4cf9 --- /dev/null +++ b/modules/github-bin-fetch.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# if you have run into github api anonymous access limits which happens during debugging/dev then add user and token here or sourced from a separate file +# set to location for tokens in file +source ~/githubapitoken + +if [ "$GITHUB_TOKEN" != "" ]; then +echo using access token with script +echo $GITHUB_USER $GITHUB_TOKEN +fi + +DIR=$(dirname "$(readlink -f "$0")") || exit + +ORG=$1 +REPO=$2 +TYPE=$3 +NAME=$4 +EXCLUDE=$5 + +URL=$(curl -s https://api.github.com/repos/$ORG/$REPO/releases/latest \ +# | grep 'browser_download_url.*'$TYPE'' +# | sed -n -e 's/^.*: //p' \ +# | sed 's/"//g' + ) + + + echo "Pulling $URL" +TODO put back tokens here. +# wget $URL -O $NAME.$TYPE +# chmod +x $DIR/$NAME.$TYPE +# ln -s $DIR/$NAME.$TYPE /opt/bin/$NAME diff --git a/modules/utility/debug.lib b/modules/utility/debug.lib new file mode 100644 index 0000000..9d9afb4 --- /dev/null +++ b/modules/utility/debug.lib @@ -0,0 +1,8 @@ +#!/bin/bash +# TODO allow debug to have managment flags and levels +function debug () { +[[ $BASH_DEBUG ]] && echo -e "#### DEBUG ####\n $@ \n#####" >&2 +} + +# alias debug_on="sudo -i uncomment BASH_DEBUG /etc/bash.bashrc" +# alias debug_off="sudo -i comment BASH_DEBUG /etc/bash.bashrc" diff --git a/modules/utility/file.lib b/modules/utility/file.lib index a90cf81..7a40625 100644 --- a/modules/utility/file.lib +++ b/modules/utility/file.lib @@ -1,5 +1,16 @@ #!/bin/bash +# export BASH_DEBUG=true + +build_file () { + [[ -f "$2" ]] || (echo "output file $2 does not exist";return 1) + if [[ -f "$1" ]]; then + echo -e "\n####### ADDED $1 ########" >> $2 + cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2 + else debug "no such file $1 to append" + fi +} + function lines_2_str () { [[ ! -f "$1" ]] && return 1 local str='' @@ -19,9 +30,9 @@ done < $1 return 0 } -function super_find () { - # echo super_find $@ >&2 -# USAGE +# find, superceeds find use `command find` to get the super +function _find () { +# USAGE # all option arguments that contain globs/wildcards must be quoted to avoid expansion # f sets path and file excludes from a supplied file path # all lines ending in / will be treated as directory names to ignore, otherwise files @@ -32,17 +43,17 @@ function super_find () { # if no directory is given it will attempt to source the present working directory # example: # source_dir -p "archive" -x '"*.off" "*.md"' -d 0 # $DIR/$SUBDIR -declare OPTION -declare OPTARG -declare OPTIND local EXCLUDE_FILE local PATHS local NAMES local ENAMES local DEPTH=1 -local VERBOSE=0 +local HIDDEN -while getopts 'p:d:e:n:f:' OPTION; do +declare OPTION +declare OPTARG +declare OPTIND +while getopts 'p:d:e:n:f:h' OPTION; do case "$OPTION" in f) EXCLUDE_FILE=$OPTARG @@ -66,6 +77,10 @@ case "$OPTION" in DEPTH=$OPTARG # echo "SOURCING TO DEPTH (0=any)" "$DEPTH" ;; + h) + HIDDEN=true + # echo "SOURCING TO DEPTH (0=any)" "$DEPTH" + ;; *) echo unknown option $OPTION ;; @@ -78,8 +93,8 @@ local DIR DIR="$*" if [ ! "$DIR" ]; then if [ -v PS1 ]; then - echo no directory to source provided - echo sourcing present working directory $(pwd) + echo no directory provided to search + echo searching present working directory $(pwd) read -p "Do you want to continue? " -n 1 -r [[ $REPLY =~ ^[Yy]$ ]] && DIR=$(pwd) || return 1 else @@ -92,10 +107,11 @@ fi # echo dir $DIR local FIND -FIND="find $DIR" +FIND="command find $DIR" FIND+=$([ ! $DEPTH == 0 ] && echo " -maxdepth $DEPTH ") -# ignore all .name and .path by default -FIND+=" -type f ! -path \"*/.*/*\" ! -name \".*\" " +FIND+=" -type f " +# include HIDDEN files and directories IS FALSE BY DEFULT +[[ ! $HIDDEN ]] && FIND+="! -path \"*/.*/*\" ! -name \".*\" " local name local path @@ -135,21 +151,23 @@ fi # echo # echo find dir: $DIR >&2 -# echo find command: $FIND >&2 +debug "find command: $FIND" local FILES FILES=$(eval $FIND | sort) -echo $FILES +[[ $FILES ]] && echo $FILES return 0 } source_dir () { # echo passed: $* + debug function: source_dir local FILES - FILES=$(super_find "$@") + FILES=$(_find "$@") # find function + # echo $FILES >&2 [[ $? -ne 0 ]] && return 1 for f in $FILES; do - # echo sourcing: $f + # echo sourcing: $f >&2 source "$f" done diff --git a/modules/utility/filesystem.mod b/modules/utility/filesystem.mod index 6b8bb3f..3b16651 100644 --- a/modules/utility/filesystem.mod +++ b/modules/utility/filesystem.mod @@ -4,3 +4,14 @@ mounted () { [[ ! $1 ]] && echo no mount point to test && return 2 mountpoint "$1" &> /dev/null && echo yes || return 1 } + +# peals back sub-directories until if finds a mountpoing +find_mountpoint () { + local dir=$1 + if [[ ! $dir = "/" ]]; then + # echo trying $dir for mountpoint + [[ $(mounted $dir) ]] && echo $dir || find_mountpoint "$(dirname $1)" + else + return 1 + fi +} diff --git a/setup/etc/bash.bashrc b/setup/etc/bash.bashrc index d174f41..afeb750 100644 --- a/setup/etc/bash.bashrc +++ b/setup/etc/bash.bashrc @@ -11,4 +11,7 @@ if ([ -n "$SSH_CONNECTION" ] || [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]); then export SSH_SESSION=true source /etc/profile fi +# global debug on or off via debug_on debug_off aliases +# can add this export to any file +#export BASH_DEBUG=true source $BASH_SHELL_LOAD diff --git a/setup/user/.bashrc b/setup/user/.bashrc index 046bab8..490d279 100644 --- a/setup/user/.bashrc +++ b/setup/user/.bashrc @@ -2,6 +2,6 @@ # echo "$USER .bashrc" # processing user's shell repo if base was loaded [[ $BASH_SHELL_BASE_LOADED = true ]] && \ -BASH_SHELL_USER=${BASH_SHELL_USER:-"bash/shell"} && \ +export BASH_SHELL_USER=${BASH_SHELL_USER:-"bash/shell"} && \ [[ -d $HOME/$BASH_SHELL_USER ]] && shell_process_directory "$HOME/$BASH_SHELL_USER" ||\ echo no user shell directory $HOME/$BASH_SHELL_USER to process, create one or clone a template diff --git a/shell.env b/shell.env index f4960a4..ee5d20f 100644 --- a/shell.env +++ b/shell.env @@ -55,6 +55,7 @@ export BASH_USE_ALIAS=true # will source aliases for non-interactive # identify a network name that this host resides on # make a directory of the same name # if unset then only /all will be sourced +# TODO just read all directories in BASH_SHELL_NETWORK. export NETWORK_DOMAINS=(238.kebler.net 645.kebler.net 3115.kebler.net) # network domain folder entry will be made for each domain set, first is home domain, others via vpn if [[ $NETWORK_DOMAINS ]]; then diff --git a/ssh/config/readme.md b/ssh/config/readme.md new file mode 100644 index 0000000..d2a51fe --- /dev/null +++ b/ssh/config/readme.md @@ -0,0 +1 @@ +*anything in /config will be used as configuration file with ssh function that calls ssh binary* diff --git a/ssh/readme.md b/ssh/session/readme.md similarity index 81% rename from ssh/readme.md rename to ssh/session/readme.md index 168ba5e..223d2e1 100644 --- a/ssh/readme.md +++ b/ssh/session/readme.md @@ -1 +1 @@ -*anything in /session will be sourced if this is a remote ssh login session* +*anything in /session will be sourced if this is a remote ssh login session*