diff --git a/etools-helper.sh b/etools-helper.sh new file mode 100755 index 0000000..70d15a7 --- /dev/null +++ b/etools-helper.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + # The script is being executed directly + for function in \ + _formatted_find \ + _filter; + do + unset $function + done +fi + +function _formatted_find() { + if [ -z "${ETOOLS_FIND_COMMAND}" ]; then + # Prevent wordsplitting + if [ "${ETOOLS_FIND_CMD}" = "fd" ]; then + # shellcheck disable=SC2086 + fd $ETOOLS_FIND_ARGS "${1}" "${2}" && [ "${ETOOLS_DEBUG}" ] && \ + einfo fd $ETOOLS_FIND_ARGS "${1}" "${2}" >&2 + else + # shellcheck disable=SC2086 + "$ETOOLS_FIND_CMD" "${2}" $ETOOLS_FIND_ARGS "${1}" && [ "${ETOOLS_DEBUG}" ] && \ + einfo "$ETOOLS_FIND_CMD" "${2}" $ETOOLS_FIND_ARGS "${1}" >&2 + fi + else + echo called fd 3 + eval '$(echo "${ETOOLS_FIND_COMMAND//\{repo\}/${2}}" | sed -e "s/{package}/${1}/g")' + fi +} + +function _filter() { + # TODO: continue here + echo "$@" +} diff --git a/etools.sh b/etools.sh new file mode 100644 index 0000000..7acb771 --- /dev/null +++ b/etools.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + + +# NOTE: parse config, gets run when sourced +function etools_configure() { + # Load config for shellcheck + # shellcheck source=/etc/etools/etools.conf + [ -f "/etc/etools/etools.conf" ] && . "/etc/etools/etools.conf" + # shellcheck source=~/.config/etools.conf + [ -f "$HOME/.config/etools.conf" ] && . "$HOME/.config/etools.conf" + # shellcheck source=~/.config/etools/etools.conf + [ -f "$HOME/.config/etools/etools.conf" ] && . "$HOME/.config/etools/etools.conf" + + + # The find command to run + [ -z "${ETOOLS_FIND_CMD}" ] && ETOOLS_FIND_CMD="fd" + # The Argyments passed to the find command + [ -z "${ETOOLS_FIND_ARGS}" ] && ETOOLS_FIND_ARGS=" + --exclude profile --exclude scripts --exclude eclass --exclude metadata \ + --exclude app-emacs --exclude dev-ruby --exclude acct-user --exclude acct-group \ + --type d" + + # [ -z "${ETOOLS_FIND_CMD}" ] && ETOOLS_FIND_CMD="find" + # [ -z "${ETOOLS_FIND_ARGS}" ] && \ + # ETOOLS_FIND_ARGS="\ + # ! -path './profile*' ! -path './scripts*' -path './eclass*' ! -path './metadata*' \ + # ! -path './app-emacs*' ! -path './dev-ruby*' ! -path './acct-user*' ! -path './acct-group*' \ + # -type d -name" + + # A full command that may be specified if FIND_CMD and FIND_ARGS cannot fulfill the needed job + # use {package} as a wildcart for the package that should be search for + # use {repo} as a wildcart for the repo to search + # use any other variable from ETOOLS inside it + # [ -z "${ETOOLS_FIND_COMMAND}" ] && ETOOLS_FIND_COMMAND="fd ${ETOOLS_FIND_ARGS} {package} {repo}" + + # The default path in which to look for the repos + [ -z "${ETOOLS_REPO_PATH}" ] && ETOOLS_REPO_PATH="/var/db/repos" + + + [ -z "${COLOR_INFO}" ] && COLOR_INFO='\033[1;34m' + [ -z "${COLOR_WARN}" ] && COLOR_WARN='\033[1;33m' + [ -z "${COLOR_ERROR}" ] && COLOR_ERROR='\033[1;31m' + + + [ -z "${ETOOLS_DEBUG}" ] && ETOOLS_DEBUG=: +} + + + +# NOTE: find a package just by it's name +# the function will go on to print out the most likely result with leading category +function etools_smart_find() { + [[ "${1}" = *"/"* ]] && echo "${1}" && \ + ewarn "do not use this function with specified category" >&2 && return 0; + [ "${1}" = "" ] && eeror "no package name provided" && return 2; + if [ ! "${2}" = "" ]; then + [ ! -d "${2}" ] && [ ! -d "${ETOOLS_REPO_PATH}/${2}" ] && \ + eerror "no valid repository name" && return 2; + . ./etools-helper.sh + if [[ "${2}" == /* ]]; then + _filer "$(_formatted_find "$@")" + else + _filter "$(_formatted_find "${1}" "${ETOOLS_REPO_PATH}/${2}")" + fi + + else + . ./etools-helper.sh + _filter "$(_formatted_find "${1}" "/var/db/repos/")" + fi + # unset helper functions + ./etools-helper.sh +} + + +# INFO: unset all variables +function etools_unset() { + for variable in \ + ETOOLS_FIND_CMD \ + ETOOLS_FIND_ARGS \ + ETOOLS_FIND_ARGS \ + ETOOLS_FIND_COMMAND \ + ETOOLS_REPO_PATH \ + etools_configure \ + etools_smart_find \ + etools_unset; + do + unset $variable + done +} + + +# INFO: einfo function +einfo() { + echo -e " ${COLOR_INFO}*\033[0m $*" +} + +# INFO: ewarn function +ewarn() { + echo -e " ${COLOR_WARN}* WARNING:\033[0m $*" +} + +# INFO: eerror function +eerror() { + echo -e " ${COLOR_ERROR}* ERROR:\033[0m $*" +} + + +# make sure we are being sourced and not executed +if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then + etools_configure +else + ewarn "this is a library do not execute it, source it instead" >&2 +fi