diff options
author | Anastasios Grammenos <anastasios.grammenos@noris.gr> | 2021-03-17 13:46:41 +0200 |
---|---|---|
committer | Anastasios Grammenos <anastasios.grammenos@noris.gr> | 2021-03-17 13:46:41 +0200 |
commit | 7288da0ba23bd7e9ca15d39446c4bf6c095b1e54 (patch) | |
tree | 32415e58e9149ac7ae26a761bd6dc4b2e5ac93c9 | |
parent | 0ccaa814bda420a39f4a184d655fa5cb670bf964 (diff) | |
download | conf-7288da0ba23bd7e9ca15d39446c4bf6c095b1e54.tar.gz conf-7288da0ba23bd7e9ca15d39446c4bf6c095b1e54.tar.bz2 conf-7288da0ba23bd7e9ca15d39446c4bf6c095b1e54.zip |
Update and add completion
-rwxr-xr-x | conf | 80 | ||||
-rw-r--r-- | conf_completion | 40 |
2 files changed, 111 insertions, 9 deletions
@@ -29,6 +29,9 @@ CONF_HOME="${CONF_HOME:-$HOME/.conf}" DEF_GROUP="default" HOME_SUB="/home" +## Indentation for tree listing +CONF_TREE_INDENT="|- " + ######## # VARS # ######## @@ -62,7 +65,7 @@ function add { while [ $# -gt 0 ]; do case ${1} in - -g) #a add next file to group + -g) #a add next file to specified group GROUP="${2}" shift shift @@ -97,11 +100,20 @@ function list { [[ -e "$group" ]] || break _group="$(basename "$group")" echo "${_group}:" - find "${group}" -type f | sed -e "s#^${group}${HOME_SUB}#|- ${HOME}#" + find "${group}" -type f -exec basename {} \; | sed -e "s#^#${CONF_TREE_INDENT}#" done && return 0 - while getopts ":lg:Gh" OPT; do + while getopts ":lag:Gh" OPT; do case ${OPT} in + a) #l list tree with full paths + for group in "${CONF_HOME}"/*; do + [[ -e "$group" ]] || break + _group="$(basename "$group")" + echo "${_group}:" + find "${group}" -type f | sed -e "s#^${group}${HOME_SUB}#|- ${HOME}#" + done + return 0 + ;; l) #l list paths for group in "${CONF_HOME}"/*; do [[ -e "$group" ]] || break @@ -139,20 +151,20 @@ function search { } function edit { - [ $# -ne 1 ] && err "Specify entry to edit:\n <basename>\n <group>\n <group>:<basename>" + [ $# -ne 1 ] && err "Specify entry to edit:\n <basename>\n <group>\n <group>/<basename>" declare -A configs for group in "${CONF_HOME}"/*; do [[ -e "$group" ]] || break _group="$(basename "$group")" while IFS= read -r -d '' file; do - configs["${_group}:$(basename "$file")"]="$file" + configs["${_group}/$(basename "$file")"]="$file" done < <(find "${CONF_HOME}/${_group}" -type f -print0) done matched=() for c in "${!configs[@]}"; do - if [ "${c/:*/}" == "$1" ]; then + if [ "${c/\/*/}" == "$1" ]; then matched+=( "${configs[$c]}" ) fi done @@ -162,7 +174,7 @@ function edit { matched=() for c in "${!configs[@]}"; do - if [ "${c/*:/}" == "$1" ]; then + if [ "${c/*\//}" == "$1" ]; then matched+=( "${configs[$c]}" ) fi done @@ -183,7 +195,53 @@ function edit { } function relink { - echo Not implemented + CONF_GROUPS=($(list -G)) + SELECTED_GROUPS=() + + for arg in "$@"; do + if [ "$arg" == "-t" ]; then + MODE="test" + elif [ "$arg" == "-h" ]; then + echo relink help + return 0 + else + if [[ " ${CONF_GROUPS[@]} " =~ " ${arg} " ]]; then + SELECTED_GROUPS+=("$arg") + else + err "${arg}: group not found" + fi + fi + done + + echo -e "Selected groups to relink: $([ ${#SELECTED_GROUPS[@]} -eq 0 ] && echo *all* || echo ${SELECTED_GROUPS[@]})\n---" + + # if [ "$MODE" == "test" ]; then + # echo "*** DRY RUN ***" + # for group in "${CONF_HOME}"/*; do + # [[ -e "$group" ]] || break + # ([[ " ${SELECTED_GROUPS[@]} " =~ " $(basename "$group") " ]] || [ ${#SELECTED_GROUPS[@]} -eq 0 ]) || continue + # echo "$(basename "$group"):" + # find "${group}" -type f -exec echo "{} --> {}" \; | sed -e "s# ${group}${HOME_SUB}#${HOME}#" + # done + # return 0 + # fi + + if [ ${#SELECTED_GROUPS[@]} -eq 0 ]; then + SELECTED_GROUPS=("${CONF_GROUPS[@]}") + fi + + echo_if_test "*** DRY RUN ***" + for g in "${SELECTED_GROUPS[@]}"; do + echo "$g:" + for f in $(list -g "${g}"); do + local SRC + SRC="${f/${HOME}/${CONF_HOME}\/${g}${HOME_SUB}}" + echo "$SRC -> $f" + [[ "$MODE" == "test" ]] && continue + set +e + ln -s "$SRC" "$f" + done + done } function printhelp { @@ -193,7 +251,8 @@ function printhelp { if [ $# -lt 1 ] then - printhelp + list + exit 0 fi case ${1} in @@ -217,6 +276,9 @@ case ${1} in shift relink "$@" ;; + help) ## print this help + printhelp + ;; version | --version | -v | -V) echo "version $CONF_VERSION" ;; diff --git a/conf_completion b/conf_completion new file mode 100644 index 0000000..ebeb482 --- /dev/null +++ b/conf_completion @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +_conf() +{ + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + local commands="add ls list search edit relink grep help" + if [[ $COMP_CWORD -gt 1 ]]; then + local lastarg="${COMP_WORDS[$COMP_CWORD-1]}" + case "${COMP_WORDS[1]}" in + l|ls|list) + if [[ $lastarg == "-g" ]]; then + COMPREPLY=($(compgen -W "$(conf ls -G)" -- ${cur})) + else + COMPREPLY=($(compgen -W "$(conf ls -h 2>&1 | awk -F' ' '{print $1}')" -- ${cur})) + fi + ;; + add|a) + if [[ $lastarg == "-g" ]]; then + COMPREPLY=($(compgen -W "$(conf ls -G)" -- ${cur})) + else + COMPREPLY=($(compgen -f -- ${cur})) + fi + ;; + edit|e) + COMPREPLY=($(compgen -W "$(conf ls -G) \ + $(conf ls -l | xargs -If basename f) \ + $(for g in $(conf ls -G); do \ + conf ls -g $g | xargs -If basename f | sed -e "s#^#${g}/#"; \ + done)" -- ${cur})) + ;; + esac + else + COMPREPLY=($(compgen -W "${commands}" -- ${cur})) + fi +} + +complete -F _conf conf + + |