Skip to content

Instantly share code, notes, and snippets.

@mrjk
Last active March 28, 2024 00:16
Show Gist options
  • Save mrjk/91e8a42bfb2cf5a2ec9601be1a754088 to your computer and use it in GitHub Desktop.
Save mrjk/91e8a42bfb2cf5a2ec9601be1a754088 to your computer and use it in GitHub Desktop.
A simple template cli for bash
#!/bin/bash
# Utility to discover installed binaries
set -eu
path_list ()
{
tr ':' '\n' <<< "$PATH"
}
cli_help ()
{
cat <<EOF
Utility to discover installed binaries.
Usage: ${0##*/} list|paths|package|home [filter]
${0##*/} dup|help
Commands:
info Show paths informations
paths List bin from paths
packages List bin from packages
home List bin from home
dup List duplicatess
help Show this help
Infos:
Version: 0.1
License: GNU
Author: mrjk
EOF
}
cli_summary ()
{
local filter=${1:-".*"}
local grep_opt=${1:+"--color=always"}
for p in $(path_list); do
local c=$(ls -1 "$p" | wc -l)
echo "$p:$c"
done | grep $grep_opt "$filter" | column -t -s:
}
cli_pkg ()
{
local filter=${1:-".*"}
local grep_opt=${1:+"--color=always"}
pacman -Ql | grep /bin/ \
| awk '{print $1 ":" $2}' \
| column -t -s: \
| grep $grep_opt "$filter"
}
cli_path ()
{
local filter=${1:-".*"}
local grep_opt=${1:+"--color=always"}
while read -r path; do
local path=$path
find "$path" -maxdepth 1 \
-type f -executable \
-printf "$path:%f\n" \
| sort -t: -k2
done <<< "$(path_list)" \
| column -t -s: \
| grep $grep_opt "$filter"
}
cli_home ()
{
local filter=${1:-".*"}
local grep_opt=${1:+"--color=always"}
while read -r path; do
local path=$path
while read -r bin; do
local target=
[[ ! -L "$path/$bin" ]] ||
target=$(readlink -s -m "$path/$bin" | sed -e "s@$HOME/@@" -e 's/^/-> /')
echo "${path#$HOME/}:$bin:$target"
done <<< $(ls -1 "$path")
done <<< "$(path_list | grep $HOME )" \
| column -t -s: \
| grep $grep_opt "$filter"
}
cli_dup ()
{
local filter=${1:-".*"}
local grep_opt=${1:+"--color=always"}
while read -r path; do
local path=$path
find "$path" -maxdepth 1 \
-type f -executable \
-printf "%f\n"
done <<< "$(path_list)" \
| sort | uniq -D | sort | uniq \
| grep $grep_opt "$filter" | tee /dev/stderr | wc -l
}
main ()
{
local action=${1-paths}
shift 1 || true
case "$action" in
info|infos|view)
cli_summary $@ ;;
package|packages|pkg)
cli_pkg $@ ;;
path|paths|ls|list)
cli_path $@ ;;
home|h)
cli_home $@ ;;
duplicate|duplicates|dup)
cli_dup $@ ;;
*)
cli_help; return 1 ;;
esac
}
main "${@-}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment