List Commands with Descriptions (#161)

* Updated `utils.sh` with `bpkg_exec_exist` and `bpkg_exec_or_exit`.
* Updated scripts with `bpkg_exec_or_exit`.
* Updated `install.sh` to utilize utility functions.
* Updated `runner` in `run.sh`.
* Added `--list` option to `run.sh`.
* Added `commands-description` to `bpkg.json`.
* Updated README TOC and missing `dependencies-dev` section.
* Added `commands-description` section to README.
* Corrected failing shellcheck.
pull/162/head
Sam Likins 1 year ago committed by GitHub
parent e2e373acf6
commit 79481450fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,33 +8,34 @@ You can install shell scripts globally (on `${PREFIX:-/usr/local/bin}`) or use t
<!-- BEGIN-MARKDOWN-TOC --> <!-- BEGIN-MARKDOWN-TOC -->
* [Install](#install) * [Install](#install)
* [0. Dependencies](#0-dependencies) * [0. Dependencies](#0-dependencies)
* [1. Install script](#1-install-script) * [1. Install script](#1-install-script)
* [2. clib](#2-clib) * [2. clib](#2-clib)
* [3. Source Code](#3-source-code) * [3. Source Code](#3-source-code)
* [Usage](#usage) * [Usage](#usage)
* [Installing packages](#installing-packages) * [Installing packages](#installing-packages)
* [Packages With Dependencies](#packages-with-dependencies) * [Packages With Dependencies](#packages-with-dependencies)
* [Running packages with `bpkg`](#running-packages-with-bpkg) * [Running packages with `bpkg`](#running-packages-with-bpkg)
* [Retrieving package info](#retrieving-package-info) * [Retrieving package info](#retrieving-package-info)
* [Package details](#package-details) * [Package details](#package-details)
* [bpkg.json](#bpkgjson) * [bpkg.json](#bpkgjson)
* [name](#name) * [name](#name)
* [version (optional)](#version-optional) * [version (optional)](#version-optional)
* [description](#description) * [description](#description)
* [global](#global) * [global](#global)
* [install](#install-1) * [install](#install-1)
* [scripts](#scripts) * [scripts](#scripts)
* [files (optional)](#files-optional) * [files (optional)](#files-optional)
* [dependencies (optional)](#dependencies-optional) * [dependencies (optional)](#dependencies-optional)
* [commands (optional)](#commands-optional) * [dependencies-dev (optional)](#dependencies-dev-optional)
* [commands (optional)](#commands-optional)
* [commands-description (optional)](#commands-description-optional)
* [Packaging best practices](#packaging-best-practices) * [Packaging best practices](#packaging-best-practices)
* [Package exports](#package-exports) * [Package exports](#package-exports)
* [Sponsors](#sponsors) * [Sponsors](#sponsors)
* [Contributors](#contributors) * [Contributors](#contributors)
* [Backers](#backers) * [Backers](#backers)
* [License](#license) * [License](#license)
<!-- END-MARKDOWN-TOC --> <!-- END-MARKDOWN-TOC -->
## Install ## Install
@ -262,6 +263,16 @@ This is a hash of dependencies. The keys are the package names, and the values a
} }
``` ```
### dependencies-dev (optional)
This is a hash of dependencies only needed during development. Like the `dependencies` array, the keys are the package names, and the values are the version specifiers; `'master'` or a tagged release can be used as the identifier. These development dependencies are installed by adding the `-d` or `--dev` flags to the `bpkg install` command.
```json
"dependencies-dev": {
"term": "0.0.1"
}
```
### commands (optional) ### commands (optional)
This is a hash of commands. The keys are the names of the commands and the values are the commands to execute in a shell. The commands can be called from the command line with `bpkg run` followed by the command name. This is a hash of commands. The keys are the names of the commands and the values are the commands to execute in a shell. The commands can be called from the command line with `bpkg run` followed by the command name.
@ -279,6 +290,16 @@ $ bpkg run say-hello "Bash Package Manager"
Hello Bash Package Manager Hello Bash Package Manager
``` ```
### commands-description (optional)
This is a hash of descriptions for configured commands. The keys are the names of the commands and the values are the descriptions for the specified commands. The command descriptions can be listed on the command line by providing the `-l` or `--list` flags after the `bpkg run` command.
```json
"commands-description": {
"say-hello": "Output hello to provided name (ex: bpkg run say-hello John)"
}
```
## Packaging best practices ## Packaging best practices
These are guidelines that we strongly encourage developers to follow. These are guidelines that we strongly encourage developers to follow.

@ -8,5 +8,9 @@
"commands": { "commands": {
"lint": "command shellcheck **/*.sh", "lint": "command shellcheck **/*.sh",
"list-sources": "echo ${BPKG_PACKAGE_SOURCES[@]} | sed 's/ /\n/g'" "list-sources": "echo ${BPKG_PACKAGE_SOURCES[@]} | sed 's/ /\n/g'"
},
"commands-description": {
"lint": "Execute shellcheck against all .sh files in project",
"list-sources": "Output package source files"
} }
} }

@ -1,31 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if ! type -f bpkg-realpath &>/dev/null; then
echo "error: bpkg-realpath not found, aborting"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/realpath/realpath.sh
source "$(which bpkg-realpath)"
fi
if ! type -f bpkg-utils &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
fi fi
if ! type -f bpkg-getdeps &>/dev/null; then # shellcheck source=lib/utils/utils.sh
echo "error: bpkg-getdeps not found, aborting" source "$(which bpkg-utils)"
exit 1
else # shellcheck source=lib/realpath/realpath.sh
# shellcheck disable=SC2230 bpkg_exec_or_exit bpkg-realpath &&
# shellcheck source=lib/getdeps/getdeps.sh source "$(which bpkg-realpath)"
# shellcheck source=lib/getdeps/getdeps.sh
bpkg_exec_or_exit bpkg-getdeps &&
source "$(which bpkg-getdeps)" source "$(which bpkg-getdeps)"
fi
bpkg_initrc bpkg_initrc
@ -34,15 +23,6 @@ let install_dev=0
let force_actions=${BPKG_FORCE_ACTIONS:-0} let force_actions=${BPKG_FORCE_ACTIONS:-0}
let needs_global=0 let needs_global=0
## check parameter consistency
validate_parameters () {
if [[ ${#BPKG_GIT_REMOTES[@]} -ne ${#BPKG_REMOTES[@]} ]]; then
error "$(printf 'BPKG_GIT_REMOTES[%d] differs in size from BPKG_REMOTES[%d] array' "${#BPKG_GIT_REMOTES[@]}" "${#BPKG_REMOTES[@]}")"
return 1
fi
return 0
}
## output usage ## output usage
usage () { usage () {
echo 'usage: bpkg-install [directory]' echo 'usage: bpkg-install [directory]'
@ -52,62 +32,6 @@ usage () {
echo ' or: bpkg-install [-g|--global] [-f|--force] ...<user>/<package>' echo ' or: bpkg-install [-g|--global] [-f|--force] ...<user>/<package>'
} }
## format and output message
message () {
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term color "$1"
fi
shift
echo -n " $1"
shift
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
fi
printf ': '
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
bpkg-term bright
fi
printf "%s\n" "$@"
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
fi
}
## output error
error () {
message 'red' 'error' "$@" >&2
return 0
}
## output warning
warn () {
message 'yellow' 'warn' "$@" >&2
return 0
}
## output info
info () {
local title='info'
if (( "$#" > 1 )); then
title="$1"
shift
fi
message 'cyan' "$title" "$@"
return 0
}
save_remote_file () { save_remote_file () {
local auth_param dirname path url local auth_param dirname path url
@ -209,7 +133,7 @@ bpkg_install () {
pkgs+=("$opt") pkgs+=("$opt")
shift shift
else else
error "Unknown option \`$opt'" bpkg_error "Unknown option \`$opt'"
return 1 return 1
fi fi
;; ;;
@ -249,7 +173,7 @@ bpkg_install () {
did_fail=0 did_fail=0
break break
elif [[ "$?" == '2' ]]; then elif [[ "$?" == '2' ]]; then
error 'fatal error occurred during install' bpkg_error 'fatal error occurred during install'
return 1 return 1
fi fi
i=$((i+1)) i=$((i+1))
@ -257,7 +181,7 @@ bpkg_install () {
done done
if (( did_fail == 1 )); then if (( did_fail == 1 )); then
error 'package not found on any remote' bpkg_error 'package not found on any remote'
return 1 return 1
fi fi
@ -309,7 +233,7 @@ bpkg_install_from_remote () {
name="${pkg_parts[0]}" name="${pkg_parts[0]}"
version="${pkg_parts[1]}" version="${pkg_parts[1]}"
else else
error 'Error parsing package version' bpkg_error 'Error parsing package version'
return 1 return 1
fi fi
@ -329,7 +253,7 @@ bpkg_install_from_remote () {
user="${pkg_parts[0]}" user="${pkg_parts[0]}"
name="${pkg_parts[1]}" name="${pkg_parts[1]}"
else else
error 'Unable to determine package name' bpkg_error 'Unable to determine package name'
return 1 return 1
fi fi
@ -340,7 +264,7 @@ bpkg_install_from_remote () {
## check to see if remote is raw with oauth (GHE) ## check to see if remote is raw with oauth (GHE)
if [[ "${remote:0:10}" == "raw-oauth|" ]]; then if [[ "${remote:0:10}" == "raw-oauth|" ]]; then
info 'Using OAUTH basic with content requests' bpkg_info 'Using OAUTH basic with content requests'
OLDIFS="$IFS" OLDIFS="$IFS"
IFS="'|'" IFS="'|'"
local remote_parts=("$remote") local remote_parts=("$remote")
@ -360,7 +284,7 @@ bpkg_install_from_remote () {
## clean up extra slashes in uri ## clean up extra slashes in uri
uri=${uri/\/\///} uri=${uri/\/\///}
info "Install $uri from remote $remote [$git_remote]" bpkg_info "Install $uri from remote $remote [$git_remote]"
## Ensure remote is reachable ## Ensure remote is reachable
## If a remote is totally down, this will be considered a fatal ## If a remote is totally down, this will be considered a fatal
@ -368,7 +292,7 @@ bpkg_install_from_remote () {
## from the broken remote. ## from the broken remote.
{ {
if ! url_exists "$remote" "$auth_param"; then if ! url_exists "$remote" "$auth_param"; then
error "Remote unreachable: $remote" bpkg_error "Remote unreachable: $remote"
return 2 return 2
fi fi
} }
@ -392,7 +316,7 @@ bpkg_install_from_remote () {
if (( 0 == has_pkg_json )); then if (( 0 == has_pkg_json )); then
## check to see if there's a Makefile. If not, this is not a valid package ## check to see if there's a Makefile. If not, this is not a valid package
if ! url_exists "$url/Makefile?$nonce" "$auth_param"; then if ! url_exists "$url/Makefile?$nonce" "$auth_param"; then
warn "Makefile not found, skipping remote: $url" bpkg_warn "Makefile not found, skipping remote: $url"
return 1 return 1
fi fi
fi fi
@ -462,8 +386,8 @@ bpkg_install_from_remote () {
fi fi
if [[ -z "$build" ]]; then if [[ -z "$build" ]]; then
warn 'Missing build script' bpkg_warn 'Missing build script'
warn 'Trying "make install"...' bpkg_warn 'Trying "make install"...'
build='make install' build='make install'
fi fi
@ -483,7 +407,7 @@ bpkg_install_from_remote () {
( (( 0 == prevent_prune )) && rm -rf "$name-$version") ( (( 0 == prevent_prune )) && rm -rf "$name-$version")
## shallow clone ## shallow clone
info "Cloning $repo_url to $(pwd)/$name-$version" bpkg_info "Cloning $repo_url to $(pwd)/$name-$version"
(test -d "$name-$version" || git clone "$repo_url" "$name-$version" 2>/dev/null) && ( (test -d "$name-$version" || git clone "$repo_url" "$name-$version" 2>/dev/null) && (
## move into directory ## move into directory
cd "$name-$version" && ( cd "$name-$version" && (
@ -495,7 +419,7 @@ bpkg_install_from_remote () {
) )
## build ## build
info "Performing install: \`$build'" bpkg_info "Performing install: \`$build'"
mkdir -p "$PREFIX"/{bin,lib} mkdir -p "$PREFIX"/{bin,lib}
build_output=$(eval "$build") build_output=$(eval "$build")
echo "$build_output" echo "$build_output"
@ -518,7 +442,7 @@ bpkg_install_from_remote () {
mkdir -p "$BPKG_PACKAGE_DEPS/bin" mkdir -p "$BPKG_PACKAGE_DEPS/bin"
# install package dependencies # install package dependencies
info "Install dependencies for $name" bpkg_info "Install dependencies for $name"
BPKG_DEPS_EXEC="bpkg_getdeps" BPKG_DEPS_EXEC="bpkg_getdeps"
if (( 1 == install_dev )); then if (( 1 == install_dev )); then
@ -533,19 +457,19 @@ bpkg_install_from_remote () {
if [[ "$script" ]];then if [[ "$script" ]];then
local scriptname="$(echo "$script" | xargs basename )" local scriptname="$(echo "$script" | xargs basename )"
info "fetch" "$url/$script" bpkg_info "fetch" "$url/$script"
warn "BPKG_PACKAGE_DEPS is '$BPKG_PACKAGE_DEPS'" bpkg_warn "BPKG_PACKAGE_DEPS is '$BPKG_PACKAGE_DEPS'"
info "write" "$BPKG_PACKAGE_DEPS/$name/$script" bpkg_info "write" "$BPKG_PACKAGE_DEPS/$name/$script"
save_remote_file "$url/$script" "$BPKG_PACKAGE_DEPS/$name/$script" "$auth_param" save_remote_file "$url/$script" "$BPKG_PACKAGE_DEPS/$name/$script" "$auth_param"
scriptname="${scriptname%.*}" scriptname="${scriptname%.*}"
info "$scriptname to PATH" "$BPKG_PACKAGE_DEPS/bin/$scriptname" bpkg_info "$scriptname to PATH" "$BPKG_PACKAGE_DEPS/bin/$scriptname"
if (( force_actions == 1 )); then if (( force_actions == 1 )); then
ln -sf "../$name/$script" "$BPKG_PACKAGE_DEPS/bin/$scriptname" ln -sf "../$name/$script" "$BPKG_PACKAGE_DEPS/bin/$scriptname"
else else
if test -f "$BPKG_PACKAGE_DEPS/bin/$scriptname"; then if test -f "$BPKG_PACKAGE_DEPS/bin/$scriptname"; then
warn "'$BPKG_PACKAGE_DEPS/bin/$scriptname' already exists. Overwrite? (yN)" bpkg_warn "'$BPKG_PACKAGE_DEPS/bin/$scriptname' already exists. Overwrite? (yN)"
read -r yn read -r yn
case $yn in case $yn in
Yy) rm -f "$BPKG_PACKAGE_DEPS/bin/$scriptname" ;; Yy) rm -f "$BPKG_PACKAGE_DEPS/bin/$scriptname" ;;
@ -565,9 +489,9 @@ bpkg_install_from_remote () {
for file in "${files[@]}"; do for file in "${files[@]}"; do
( (
if [[ "$file" ]];then if [[ "$file" ]];then
info "fetch" "$url/$file" bpkg_info "fetch" "$url/$file"
warn "BPKG_PACKAGE_DEPS is '$BPKG_PACKAGE_DEPS'" bpkg_warn "BPKG_PACKAGE_DEPS is '$BPKG_PACKAGE_DEPS'"
info "write" "$BPKG_PACKAGE_DEPS/$name/$file" bpkg_info "write" "$BPKG_PACKAGE_DEPS/$name/$file"
save_remote_file "$url/$file" "$BPKG_PACKAGE_DEPS/$name/$file" "$auth_param" save_remote_file "$url/$file" "$BPKG_PACKAGE_DEPS/$name/$file" "$auth_param"
fi fi
) )
@ -580,7 +504,7 @@ bpkg_install_from_remote () {
## Use as lib or perform install ## Use as lib or perform install
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_install export -f bpkg_install
elif validate_parameters; then elif bpkg_validate; then
bpkg_install "$@" bpkg_install "$@"
exit $? exit $?
else else

@ -3,11 +3,11 @@
if ! type -f bpkg-utils &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
fi fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
bpkg_initrc bpkg_initrc
usage () { usage () {

@ -1,14 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if ! type -f bpkg-realpath &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-realpath not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/realpath/realpath.sh
source "$(which bpkg-realpath)"
fi fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
# shellcheck source=lib/realpath/realpath.sh
bpkg_exec_or_exit bpkg-realpath &&
source "$(which bpkg-realpath)"
BPKG_JSON="$(which bpkg-json)" BPKG_JSON="$(which bpkg-json)"
if [ -z "$BPKG_JSON" ]; then if [ -z "$BPKG_JSON" ]; then

@ -1,62 +1,81 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if ! type -f bpkg-realpath &>/dev/null; then
echo "error: bpkg-realpath not found, aborting"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/realpath/realpath.sh
source "$(which bpkg-realpath)"
fi
if ! type -f bpkg-utils &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
fi fi
if ! type -f bpkg-env &>/dev/null; then # shellcheck source=lib/utils/utils.sh
echo "error: bpkg-env not found, aborting" source "$(which bpkg-utils)"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh
source "$(which bpkg-env)"
fi
if ! type -f bpkg-install &>/dev/null; then # shellcheck source=lib/realpath/realpath.sh
echo "error: bpkg-install not found, aborting" bpkg_exec_or_exit bpkg-realpath &&
exit 1 source "$(which bpkg-realpath)"
else
# shellcheck source=lib/install/install.sh # shellcheck source=lib/install/install.sh
bpkg_exec_or_exit bpkg-install &&
source "$(which bpkg-install)" source "$(which bpkg-install)"
fi
if ! type -f bpkg-package &>/dev/null; then # shellcheck source=lib/package/package.sh
echo "error: bpkg-package not found, aborting" bpkg_exec_or_exit bpkg-package &&
exit 1
else
# shellcheck source=lib/package/package.sh
source "$(which bpkg-package)" source "$(which bpkg-package)"
fi
bpkg_initrc bpkg_initrc
## output usage ## output usage
usage () { usage () {
echo 'usage: bpkg-run [-h|--help]' echo 'usage: bpkg-run [-h|--help]'
echo ' or: bpkg-run [-h|--help] [command]' echo ' or: bpkg-run [-l|--list]'
echo ' or: bpkg-run [-s|--source] <package> [command]' echo ' or: bpkg-run [-s|--source] <package> [command]'
echo ' or: bpkg-run [-s|--source] <user>/<package> [command]' echo ' or: bpkg-run [-s|--source] <user>/<package> [command]'
} }
runner () { bpkg_list_commands () {
local commands
local col_len
local description
commands="$(bpkg_package 2>/dev/null | grep '\["commands"' | sed 's/\["commands","\([^"]*\).*/\1/')"
col_len="$(wc -L <<< "${commands}")"
if [ "${col_len}" -eq 0 ]; then
bpkg_error "No commands provided in BPKG package file."
return 1
fi
for command in ${commands}; do
description="$(bpkg_package commands-description "${command}")"
if [ -z "${description}" ]; then
description="Runs the ${command} command as defined in BPKG configuration"
fi
printf " "
bpkg_exec_exist bpkg-term &&
bpkg-term color cyan
printf "%-${col_len}s " "${command}"
bpkg_exec_exist bpkg-term && {
bpkg-term reset
bpkg-term bright
}
printf "%s\n" "${description}"
bpkg_exec_exist bpkg-term &&
bpkg-term reset
done
return 0
}
bpkg_runner () {
local cmd="$1" local cmd="$1"
shift
# shellcheck disable=SC2068
eval "$cmd" eval "$cmd"
return $? return $?
} }
@ -75,6 +94,11 @@ bpkg_run () {
return 0 return 0
;; ;;
-l|--list)
bpkg_list_commands
return $?
;;
-s|--source) -s|--source)
should_source=1 should_source=1
shift shift
@ -137,8 +161,7 @@ bpkg_run () {
done done
shift shift
# shellcheck disable=SC2068 bpkg_runner "$prefix ${args[*]}"
runner "$prefix ${args[*]}" $@
return $? return $?
fi fi
fi fi
@ -172,9 +195,9 @@ bpkg_run () {
return 1 return 1
fi fi
local pkgname="$(bpkg_package name 2>/dev/null)" local pkg_name="$(bpkg_package name 2>/dev/null)"
if [ -n "$pkgname" ]; then if [ -n "$pkg_name" ]; then
name="$pkgname" name="$pkg_name"
fi fi
if (( 1 == should_emit_source )); then if (( 1 == should_emit_source )); then
@ -218,8 +241,7 @@ bpkg_run () {
done done
shift shift
# shellcheck disable=SC2068 bpkg_runner "$prefix ${args[*]}"
runner "$prefix ${args[*]}" $@
fi fi
# shellcheck disable=SC2068 # shellcheck disable=SC2068

@ -3,19 +3,10 @@
if ! type -f bpkg-utils &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
fi fi
if ! type -f bpkg-env &>/dev/null; then # shellcheck source=lib/utils/utils.sh
echo "error: bpkg-env not found, aborting" source "$(which bpkg-utils)"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh
source "$(which bpkg-env)"
fi
bpkg_initrc bpkg_initrc

@ -1,13 +1,18 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if ! type -f bpkg-run &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-run not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck source=lib/run/run.sh
source "$(which bpkg-run)"
fi fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
# shellcheck source=lib/run/run.sh
bpkg_exec_or_exit bpkg-run &&
source "$(which bpkg-run)"
bpkg_source () { bpkg_source () {
# shellcheck disable=SC2068 # shellcheck disable=SC2068
bpkg_run --emit-source --target "$1" ${@:2} bpkg_run --emit-source --target "$1" ${@:2}

@ -3,12 +3,11 @@
if ! type -f bpkg-utils &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
fi fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
## output usage ## output usage
usage () { usage () {
echo "usage: bpkg-suggest [-h|--help] <query>" echo "usage: bpkg-suggest [-h|--help] <query>"

@ -3,11 +3,11 @@
if ! type -f bpkg-utils &>/dev/null; then if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting" echo "error: bpkg-utils not found, aborting"
exit 1 exit 1
else
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
fi fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
bpkg_initrc bpkg_initrc
usage () { usage () {

@ -1,161 +1,185 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if ! type -f bpkg-env &>/dev/null; then if [ -z "${BPKG_UTILS}" ]; then
echo "error: bpkg-env not found, aborting" BPKG_UTILS=1
exit 1
else ## Collection of shared bpkg functions
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh ## Init local config and set environmental defaults
source "$(which bpkg-env)" bpkg_initrc () {
fi local global_config=${BPKG_GLOBAL_CONFIG:-"/etc/bpkgrc"}
# shellcheck disable=SC1090
[ -f "$global_config" ] && source "$global_config"
local config=${BPKG_CONFIG:-"$HOME/.bpkgrc"}
# shellcheck disable=SC1090
[ -f "$config" ] && source "$config"
## set defaults
if [ ${#BPKG_REMOTES[@]} -eq 0 ]; then
BPKG_REMOTES[0]=${BPKG_REMOTE-https://raw.githubusercontent.com}
BPKG_GIT_REMOTES[0]=${BPKG_GIT_REMOTE-https://github.com}
export BPKG_REMOTES
export BPKG_GIT_REMOTE
export BPKG_GIT_REMOTES
fi
## Collection of shared bpkg functions export BPKG_PACKAGE_USER="${BPKG_PACKAGE_USER:-"bpkg"}"
export BPKG_INDEX=${BPKG_INDEX:-"$HOME/.bpkg/index"}
## Init local config and set environmental defaults
bpkg_initrc() { bpkg_validate
local global_config=${BPKG_GLOBAL_CONFIG:-"/etc/bpkgrc"}
# shellcheck disable=SC1090 return $?
[ -f "$global_config" ] && source "$global_config" }
local config=${BPKG_CONFIG:-"$HOME/.bpkgrc"}
# shellcheck disable=SC1090 ## check parameter consistency
[ -f "$config" ] && source "$config" bpkg_validate () {
## set defaults if [ ${#BPKG_GIT_REMOTES[@]} -ne ${#BPKG_REMOTES[@]} ]; then
if [ ${#BPKG_REMOTES[@]} -eq 0 ]; then bpkg_error "$(printf 'BPKG_GIT_REMOTES[%d] differs in size from BPKG_REMOTES[%d] array' "${#BPKG_GIT_REMOTES[@]}" "${#BPKG_REMOTES[@]}")"
BPKG_REMOTES[0]=${BPKG_REMOTE-https://raw.githubusercontent.com} return 1
BPKG_GIT_REMOTES[0]=${BPKG_GIT_REMOTE-https://github.com} fi
return 0
export BPKG_REMOTES }
export BPKG_GIT_REMOTE
export BPKG_GIT_REMOTES
fi ## format and output message
bpkg_message () {
export BPKG_PACKAGE_USER="${BPKG_PACKAGE_USER:-"bpkg"}" bpkg_exec_exist bpkg-term &&
export BPKG_INDEX=${BPKG_INDEX:-"$HOME/.bpkg/index"} bpkg-term color "${1}"
shift
bpkg_validate
echo -n " ${1}"
return $?
}
## check parameter consistency
bpkg_validate () {
if [ ${#BPKG_GIT_REMOTES[@]} -ne ${#BPKG_REMOTES[@]} ]; then
bpkg_error "$(printf 'BPKG_GIT_REMOTES[%d] differs in size from BPKG_REMOTES[%d] array' "${#BPKG_GIT_REMOTES[@]}" "${#BPKG_REMOTES[@]}")"
return 1
fi
return 0
}
## format and output message
bpkg_message () {
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term color "${1}"
fi
shift
echo -n " ${1}"
shift
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
fi
printf ": "
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
bpkg-term bright
fi
printf "%s\n" "${@}"
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
fi
}
## output error
bpkg_error () {
{
bpkg_message "red" "error" "${@}"
} >&2
}
## output warning
bpkg_warn () {
{
bpkg_message "yellow" "warn" "${@}"
} >&2
}
## output info
bpkg_info () {
local title="info"
if (( "${#}" > 1 )); then
title="${1}"
shift shift
fi
bpkg_message "cyan" "${title}" "${@}" bpkg_exec_exist bpkg-term &&
} bpkg-term reset
## takes a remote and git-remote and sets the globals: printf ": "
## BPKG_REMOTE: raw remote URI
## BPKG_GIT_REMOTE: git remote for cloning bpkg_exec_exist bpkg-term && {
## BPKG_AUTH_GIT_REMOTE: git remote with oauth info embedded, bpkg-term reset
## BPKG_OAUTH_TOKEN: token for x-oauth-basic bpkg-term bright
## BPKG_CURL_AUTH_PARAM: auth arguments for raw curl requests }
## BPKG_REMOTE_INDEX: location of local index for remote
bpkg_select_remote () { printf "%s\n" "${@}"
local remote=$1
local git_remote=$2 bpkg_exec_exist bpkg-term &&
export BPKG_REMOTE_HOST=$(echo "$git_remote" | sed 's/.*:\/\///' | sed 's/\/$//' | tr '/' '_') bpkg-term reset
export BPKG_REMOTE_INDEX="$BPKG_INDEX/$BPKG_REMOTE_HOST" }
# shellcheck disable=SC2034
export BPKG_REMOTE_INDEX_FILE="$BPKG_REMOTE_INDEX/index.txt" ## output error
export BPKG_OAUTH_TOKEN="" bpkg_error () {
export BPKG_CURL_AUTH_PARAM="" {
export BPKG_GIT_REMOTE=$git_remote bpkg_message "red" "error" "${@}"
export BPKG_AUTH_GIT_REMOTE=$git_remote } >&2
if [ "${remote:0:10}" == "raw-oauth|" ]; then }
OLDIFS="${IFS}"
IFS="|" ## output warning
# shellcheck disable=SC2206 bpkg_warn () {
local remote_parts=($remote) {
IFS="${OLDIFS}" bpkg_message "yellow" "warn" "${@}"
export BPKG_OAUTH_TOKEN=${remote_parts[1]} } >&2
}
## output info
bpkg_info () {
local title="info"
if (( "${#}" > 1 )); then
title="${1}"
shift
fi
bpkg_message "cyan" "${title}" "${@}"
}
## check if executable exists in path
bpkg_exec_exist () {
local exec_name="${1}"
type -f "${exec_name}" > /dev/null 2>&1
}
## executable exists in path or exit with message
bpkg_exec_or_exit () {
local exec_name="${1}"
local exit_error="${2:-1}"
if ! bpkg_exec_exist "${exec_name}"; then
bpkg_error "${exec_name} not found, aborting"
exit "${exit_error}"
fi
}
## takes a remote and git-remote and sets the globals:
## BPKG_REMOTE: raw remote URI
## BPKG_GIT_REMOTE: git remote for cloning
## BPKG_AUTH_GIT_REMOTE: git remote with oauth info embedded,
## BPKG_OAUTH_TOKEN: token for x-oauth-basic
## BPKG_CURL_AUTH_PARAM: auth arguments for raw curl requests
## BPKG_REMOTE_INDEX: location of local index for remote
bpkg_select_remote () {
local remote=$1
local git_remote=$2
export BPKG_REMOTE_HOST=$(echo "$git_remote" | sed 's/.*:\/\///' | sed 's/\/$//' | tr '/' '_')
export BPKG_REMOTE_INDEX="$BPKG_INDEX/$BPKG_REMOTE_HOST"
# shellcheck disable=SC2034 # shellcheck disable=SC2034
export BPKG_CURL_AUTH_PARAM="-u $BPKG_OAUTH_TOKEN:x-oauth-basic" export BPKG_REMOTE_INDEX_FILE="$BPKG_REMOTE_INDEX/index.txt"
export BPKG_REMOTE=${remote_parts[2]} export BPKG_OAUTH_TOKEN=""
if [[ "$git_remote" == https://* ]] && [[ "$git_remote" != *x-oauth-basic* ]] && [[ "$git_remote" != *${BPKG_OAUTH_TOKEN}* ]]; then export BPKG_CURL_AUTH_PARAM=""
export BPKG_GIT_REMOTE=$git_remote
export BPKG_AUTH_GIT_REMOTE=$git_remote
if [ "${remote:0:10}" == "raw-oauth|" ]; then
OLDIFS="${IFS}"
IFS="|"
# shellcheck disable=SC2206
local remote_parts=($remote)
IFS="${OLDIFS}"
export BPKG_OAUTH_TOKEN=${remote_parts[1]}
# shellcheck disable=SC2034 # shellcheck disable=SC2034
export BPKG_AUTH_GIT_REMOTE=${git_remote/https:\/\//https:\/\/$BPKG_OAUTH_TOKEN:x-oauth-basic@} export BPKG_CURL_AUTH_PARAM="-u $BPKG_OAUTH_TOKEN:x-oauth-basic"
export BPKG_REMOTE=${remote_parts[2]}
if [[ "$git_remote" == https://* ]] && [[ "$git_remote" != *x-oauth-basic* ]] && [[ "$git_remote" != *${BPKG_OAUTH_TOKEN}* ]]; then
# shellcheck disable=SC2034
export BPKG_AUTH_GIT_REMOTE=${git_remote/https:\/\//https:\/\/$BPKG_OAUTH_TOKEN:x-oauth-basic@}
fi
else
export BPKG_REMOTE="$remote"
fi fi
else }
export BPKG_REMOTE="$remote"
fi ## given a user and name, sets BPKG_REMOTE_RAW_PATH using the available
} ## BPKG_REMOTE and BPKG_OAUTH_TOKEN details
bpkg_select_raw_path () {
## given a user and name, sets BPKG_REMOTE_RAW_PATH using the available local user=$1
## BPKG_REMOTE and BPKG_OAUTH_TOKEN details local name=$2
bpkg_select_raw_path() { if [ "$BPKG_OAUTH_TOKEN" == "" ]; then
local user=$1 export BPKG_REMOTE_RAW_PATH="$BPKG_REMOTE/$user/$name"
local name=$2 else
if [ "$BPKG_OAUTH_TOKEN" == "" ]; then # shellcheck disable=SC2034
export BPKG_REMOTE_RAW_PATH="$BPKG_REMOTE/$user/$name" Export BPKG_REMOTE_RAW_PATH="$BPKG_REMOTE/$user/$name/raw"
else fi
# shellcheck disable=SC2034 return 0
Export BPKG_REMOTE_RAW_PATH="$BPKG_REMOTE/$user/$name/raw" }
fi
return 0
}
export -f bpkg_initrc bpkg_exec_or_exit bpkg-env
export -f bpkg_validate
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh
source "$(which bpkg-env)"
export -f bpkg_message
export -f bpkg_warn
export -f bpkg_error
export -f bpkg_info
export -f bpkg_select_remote export -f bpkg_initrc
export -f bpkg_select_raw_path export -f bpkg_validate
export -f bpkg_message
export -f bpkg_warn
export -f bpkg_error
export -f bpkg_info
export -f bpkg_exec_exist
export -f bpkg_exec_or_exit
export -f bpkg_select_remote
export -f bpkg_select_raw_path
fi

Loading…
Cancel
Save