Skip to content

Commit

Permalink
adding dir.with-file + gpg key generation (#101)
Browse files Browse the repository at this point in the history
* adding dir.with-files + gpg key generation

* Updating documentation post update; version 3.0.2
  • Loading branch information
kigster authored Jun 14, 2022
1 parent 2532e22 commit 9677f1f
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.1
3.0.2
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[separator=—]
= Bashmatic® — BASH-based DSL helpers for humans, sysadmins, and fun.
// vim: ft=asciidoc
:author: Version v3.0.1
:author: Version v3.0.2
:doctype: book
:source-highlighter: rouge
:rouge-style: base16.monokai
Expand Down
Binary file modified README.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v3.0.2](https://github.com/kigster/bashmatic/tree/v3.0.2) (2022-06-14)

[Full Changelog](https://github.com/kigster/bashmatic/compare/v3.0.1...v3.0.2)

## [v3.0.1](https://github.com/kigster/bashmatic/tree/v3.0.1) (2022-05-22)

[Full Changelog](https://github.com/kigster/bashmatic/compare/v3.0.0...v3.0.1)
Expand Down
167 changes: 146 additions & 21 deletions doc/FUNCTIONS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3775,6 +3775,39 @@ dir.short-home ()
----

==== `dir.with-file`

[source,bash]
----
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." 1>&2
return 1
}
echo "${_d}"
exit 0
fi
done
return 2
}
----


=== Module `docker`

Expand Down Expand Up @@ -4702,8 +4735,8 @@ g-u ()
gem.cache-installed ()
{
gem.configure-cache
if [[ ! -s "${LibGem__GemListCache}" || -z $(find "${LibGem__GemListCache}" -mmin -30 2>/dev/null) ]]; then
run "gem list > ${LibGem__GemListCache}" > /dev/null
if [[ ! -s "${LibGem__GemListCache}" || -z "$(find "${LibGem__GemListCache}" -mmin -30 2>/dev/null)" ]]; then
gem list > "${LibGem__GemListCache}" > /dev/null
fi
}
Expand Down Expand Up @@ -4782,7 +4815,7 @@ gem.configure-cache ()
local ruby_version=$(ruby.numeric-version)
export LibGem__GemListCache="${LibGem__GemListCacheBase}.${ruby_version}"
local dir=$(dirname "${LibGem__GemListCache}")
[[ -d ${dir} ]] || run "mkdir -p ${dir}" > /dev/null
[[ -d ${dir} ]] || mkdir -p "${dir}" > /dev/null
}
----
Expand Down Expand Up @@ -5611,45 +5644,137 @@ github.validate ()

=== Module `gpg`

==== `gpgit.install`
==== `gpg.install`

[source,bash]
----
gpgit.install ()
gpg.install ()
{
gpgit.install-deps
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"
gpg2 --verify "gpgit-${VERSION}.tar.xz.asc" "gpgit-${VERSION}.tar.xz"
tar -xf "gpgit-${VERSION}.tar.xz"
sudo make -C "gpgit-${VERSION}" PREFIX=/usr/local install
gpgit --helpp
[[ -z ${AppCurrentOS} ]] && util.os
gpg.install-deps
case "${AppCurrentOS}" in
darwin)
brew.install.packages "gnupg"
;
linux)
run "sudo apt-get install gnupg -yyq"
;
esac
}
----

==== `gpgit.install-deps`
==== `gpg.install-deps`

[source,bash]
----
gpgit.install-deps ()
gpg.install-deps ()
{
util.os
[[ -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)
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
}
----

==== `gpg.key-for-github`

[source,bash]
----
gpg.key-for-github ()
{
[[ -z ${AppCurrentOS} ]] && util.os
if ! command -v gpg > /dev/null; then
gpg.install
fi
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
}
----

==== `gpg.my-keys`

[source,bash]
----
gpg.my-keys ()
{
local -a info=($(gpg.name-and-email))
local name="${info[0]}"
local email="${info[1]}"
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" 1>&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}." 1>&2
return 1
fi
return 0
}
----

==== `gpg.name-and-email`

[source,bash]
----
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}"
}
----


=== Module `is`

Expand Down Expand Up @@ -8632,7 +8757,7 @@ abort ()
ask ()
{
printf -- "%s${txtylw}$*${clr}\n" "${LibOutput__LeftPrefix}"
printf -- "%s${bakcyn}${bldwht}❯ ${bldylw}" "${LibOutput__LeftPrefix}"
printf -- "%s${bldgrn}❯ ${bldylw}" "${LibOutput__LeftPrefix}"
}
----
Expand Down
8 changes: 8 additions & 0 deletions doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1381,8 +1381,16 @@ Grab the version from `postgres` binary in the PATH and remove fractional sub-ve
* [dir.with-file()](#dirwith-file)
* [dir.short-home()](#dirshort-home)
### `dir.with-file()`
#### Arguments
* @arg1 file without the path to search for, eg ".evnrc"
* @arg2 Starting file path to seartch
### `dir.short-home()`
Replaces the first part of the directory that matches ${HOME} with '~/'
Expand Down
Binary file modified doc/USAGE.pdf
Binary file not shown.
36 changes: 36 additions & 0 deletions lib/dir.sh
Original file line number Diff line number Diff line change
@@ -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}" |
Expand Down
Loading

0 comments on commit 9677f1f

Please sign in to comment.