diff --git a/lib/dir.sh b/lib/dir.sh index 4cc575c..8d816e4 100644 --- a/lib/dir.sh +++ b/lib/dir.sh @@ -1,5 +1,41 @@ #!/usr/bin/env bash +# @descroption +# Returns the first folder above the given that contains +# a file. +# @arg1 file without the path to search for, eg ".evnrc" +# @arg2 Starting file path to seartch +# @output File path that's a sub-phat of the @arg2 contaning the file. +# if no file is found, 1 or 2 is returned." +dir.with-file() { + local file="$1" + local dir="${2:-$(pwd -P)}" + + if [[ ${dir:0:1} != "/" ]]; then + dir="$(pwd -P)/${dir}" + fi + + local _d="${dir}" + + while true; do + local try="${_d}/${file}" + [[ -f "${try}" ]] && { + echo "${_d}" + return 0 + } + _d="$(dirname "${_d}")" + if [[ "${_d}" == "/" || "${_d}" == "" ]] ; then + [[ -f "${_d}/${file}" ]] || { + echo "No file ${file} was found in the path.">&2 + return 1 + } + echo "${_d}" + exit 0 + fi + done + return 2 +} + dir.count-slashes() { local dir="${1}" echo "${dir}" | diff --git a/lib/gpg.sh b/lib/gpg.sh index a858e2b..39ff6e0 100644 --- a/lib/gpg.sh +++ b/lib/gpg.sh @@ -9,37 +9,110 @@ # @description GPG related utilities #——————————————————————————————————————————————————————————————————————————————— -# gpgit +# gpg #——————————————————————————————————————————————————————————————————————————————— -function gpgit.install-deps() { - util.os +function gpg.install-deps() { + [[ -z ${AppCurrentOS} ]] && util.os case "${AppCurrentOS}" in darwin) - brew.install.packages "coreutils gawk gnu-sed git tar xz-utils bzip lzip file jq curl gzip" + brew.install.packages "coreutils gawk gnu-sed git curl gzip" ;; linux) # Install dependencies and optional dependencies - run "sudo apt-get install bash gnupg2 git tar xz-utils coreutils gawk grep sed" - run "sudo apt-get install gzip bzip lzip file jq curl" + run "sudo apt-get install -yqq bash gnupg2 git tar xz-utils coreutils gawk grep sed" + run "sudo apt-get install -yqq gzip bzip lzip file jq curl" ;; esac } -function gpgit.install() { - gpgit.install-deps - - # Download and verify source - VERSION=1.4.1 - run "wget \"https://github.com/NicoHood/gpgit/releases/download/${VERSION}/gpgit-${VERSION}.tar.xz\"" - run "wget \"https://github.com/NicoHood/gpgit/releases/download/${VERSION}/gpgit-${VERSION}.tar.xz.asc\"" - run "gpg2 --keyserver hkps://keyserver.ubuntu.com --recv-keys 97312D5EB9D7AE7D0BD4307351DAE9B7C1AE9161" +function gpg.install() { + [[ -z ${AppCurrentOS} ]] && util.os + gpg.install-deps + case "${AppCurrentOS}" in + darwin) + brew.install.packages "gnupg" + ;; + linux) + # Install dependencies and optional dependencies + run "sudo apt-get install gnupg -yyq" + ;; + esac +} - gpg2 --verify "gpgit-${VERSION}.tar.xz.asc" "gpgit-${VERSION}.tar.xz" +function gpg.key-for-github() { + [[ -z ${AppCurrentOS} ]] && util.os + if ! command -v gpg >/dev/null ; then + gpg.install + fi + # shellcheck disable=SC2034 + local -a info=($(gpg.name-and-email)) + local name="${info[0]}" + local email="${info[1]}" + local -a keys=( $(gpg.my-keys) ) + if [[ ${#keys[@]} -gt 0 ]] ; then + gpg.my-keys + return 0 + fi + local key_spec="$(mktemp)" + echo "\ +%echo Generating a basic OpenPGP key +Key-Type: 1 +Key-Length: 4096 +Name-Real: ${name} +Name-Email: ${email} +Expire-Date: 0 +%no-protection +%commit +%echo done +" >"${key_spec}" + cat "${key_spec}" + gpg --batch --gen-key "${key_spec}" >/dev/null +} - # Extract, install and run GPGit - tar -xf "gpgit-${VERSION}.tar.xz" - sudo make -C "gpgit-${VERSION}" PREFIX=/usr/local install - gpgit --helpp +function gpg.name-and-email() { + local name="$(git config --global --get user.name)" + local email="$(git config --global --get user.email)" + + [[ -z ${name} ]] && run.ui.ask-user-value name "Your full name:" + [[ -z ${email} ]] && run.ui.ask-user-value email "Your full Email:" + echo "${name}" "${email}" } +function gpg.my-keys() { + local -a info=($(gpg.name-and-email)) + local name="${info[0]}" + local email="${info[1]}" + + # shellcheck disable=SC2034 + declare -a keys=( $(gpg --list-secret-keys --keyid-format=long | grep -B 3 -E "^uid *\[ultimate\] ${name}.*$" | grep -E '^sec' | cut -d '/' -f 2 | sed 's/ .*$//g') ) + if [[ ${#keys[@]} -gt 0 ]]; then + printf "\n${bldylw}Your GPG keys are:${clr}\n" >&2 + echo "${keys[*]}" | tr ' ' "\n" + local len=${#keys[@]} + local index + while true; do + if [[ -n ${index} && ${index} -ge 0 && ${index} -lt ${#keys[@]} ]]; then + local key_id="${keys[${index}]}" + printf -- "Key ID is ${bldylw}${key_id}\n\n" + run "git config --global user.signingkey ${key_id}" + gpg --armor --export "${key_id}" |pbcopy + gpg --armor --export "${key_id}" + printf -- "${clr}NOTE: ${bldylw}the key is now in your clipboard${clr}.\n\n" + printf -- "${clr}NOTE: gpg key for your ~/.gitconfig is ${bldgrn}${key_id}\n\n" + hr + return $? + else + [[ -n ${index} ]] && printf "${bldred}Invalid answer, expecting a number between 1 and ${len}.${clr}\n" + run.ui.ask-user-value index "Which key would you like to print [1-${len}]? ${clr}" + index=$(( index - 1 )) + fi + done + + else + echo "No gpg keys found matching name ${name}." >&2 + return 1 + fi + return 0 +} + diff --git a/lib/output-utils.sh b/lib/output-utils.sh index e6eba3e..983089b 100644 --- a/lib/output-utils.sh +++ b/lib/output-utils.sh @@ -38,7 +38,7 @@ function err() { function ask() { printf -- "%s${txtylw}$*${clr}\n" "${LibOutput__LeftPrefix}" - printf -- "%s${bakcyn}${bldwht}❯ ${bldylw}" "${LibOutput__LeftPrefix}" + printf -- "%s${bldgrn}❯ ${bldylw}" "${LibOutput__LeftPrefix}" } function inf() { diff --git a/test/dir_test.bats b/test/dir_test.bats index fd77dc0..0d501ef 100644 --- a/test/dir_test.bats +++ b/test/dir_test.bats @@ -1,6 +1,7 @@ #!/usr/bin/env bats load test_helper +source lib/time.sh source lib/dir.sh source lib/file.sh @@ -12,6 +13,26 @@ setup() { export TEMP_DIR=$(file.temp -d) } +@test "dir.with-file()" { + local TEMP_DIR="$(mktemp -d)" + local path="${TEMP_DIR}/a/b/c/d" + mkdir -p "${path}" + + local a=".a-file" + local a_path="${TEMP_DIR}/a/b/${a}" + touch ${a_path} + + local b=".b-file" + local b_path="${TEMP_DIR}/a/b/c/d/${b}" + touch ${b_path} + + local a_dir=$(dir.with-file "${a}" "${path}") + local b_dir=$(dir.with-file "${b}" "${path}") + + [[ ${a_dir} == "${TEMP_DIR}/a/b" && "${b_dir}" == "${TEMP_DIR}/a/b/c/d" ]] + +} + @test "dir.short-home ${HOME}/workspace/project" { export HOME export