forked from cosmic/cosmic
prepping log to get much, much cooler
This commit is contained in:
parent
888a89fc51
commit
d91ac9f95c
178
bin/cosmic-log
178
bin/cosmic-log
|
@ -3,23 +3,118 @@
|
|||
# TODO: create new messages for ship using templates
|
||||
# TODO: better isolate listings.gophermap from accidental clobbering
|
||||
|
||||
version="0.1.0"
|
||||
user=$(whoami)
|
||||
ships=$(find /var/gopher -user "$user" -type d -exec basename {} \;)
|
||||
numships=$(echo "${ships}" | wc -l)
|
||||
tmp="$HOME/.cosmiclog"
|
||||
tmp=$(mktemp -t "$(basename "$0").tmp.XXXXXXX") || die "Failed to create temporary file" 1
|
||||
|
||||
flag_help=0
|
||||
flag_version=0
|
||||
flag_debug=0
|
||||
flag_shortlist=0
|
||||
|
||||
arg_options="hvds:z"
|
||||
arg_log=1
|
||||
arg_ship=""
|
||||
|
||||
die () {
|
||||
msg="$1"
|
||||
code="$2"
|
||||
# exit code defaults to 1
|
||||
if printf "%s" "$code" | grep -q '^[0-9]+$'; then
|
||||
code=1
|
||||
fi
|
||||
# output message to stdout or stderr based on code
|
||||
if [ -n "$msg" ]; then
|
||||
if [ "$code" -eq 0 ]; then
|
||||
printf "%s\\n" "$msg"
|
||||
else
|
||||
printf "%s\\n" "$msg" >&2
|
||||
fi
|
||||
fi
|
||||
exit "$code"
|
||||
}
|
||||
|
||||
finish () {
|
||||
rm -f "$tmp"
|
||||
}
|
||||
trap finish EXIT
|
||||
|
||||
check_yn () {
|
||||
read -r answer
|
||||
if [ "$answer" != "${answer#[Yy]}" ] ; then
|
||||
printf 1 # true
|
||||
else
|
||||
printf 0 # false
|
||||
parse_input () {
|
||||
if ! parsed=$(getopt $arg_options "$@"); then
|
||||
die "Invalid input" 2
|
||||
fi
|
||||
|
||||
eval set -- "$parsed"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h)
|
||||
flag_help=1
|
||||
arg_log=0
|
||||
shift
|
||||
;;
|
||||
-v)
|
||||
flag_version=1
|
||||
arg_log=0
|
||||
shift
|
||||
;;
|
||||
-d)
|
||||
flag_debug=1
|
||||
arg_log=0
|
||||
shift
|
||||
;;
|
||||
-s)
|
||||
shift
|
||||
arg_ship="$1"
|
||||
shift
|
||||
;;
|
||||
-z)
|
||||
flag_shortlist=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
die "Internal error: $1" 3
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
show_help () {
|
||||
printf "%s [-hvd] [-s shipname]\\n\\n" "$(basename "$0")"
|
||||
printf " -h Show this help\\n"
|
||||
printf " -v Show current version info\\n"
|
||||
printf " -d Debug mode\\n"
|
||||
printf " -s [shipname] Only log messages for ship named\\n"
|
||||
printf "\\n"
|
||||
printf "Passing no options will enter interactive mode.\\n\\n"
|
||||
printf "\\n"
|
||||
}
|
||||
|
||||
read_key () {
|
||||
_key=
|
||||
if [ -t 0 ]; then
|
||||
if [ -z "$_stty" ]; then
|
||||
_stty=$(stty -g)
|
||||
fi
|
||||
stty -echo -icanon min 1
|
||||
_key=$(dd bs=1 count=1 2>/dev/null)
|
||||
stty "$_stty"
|
||||
fi
|
||||
}
|
||||
|
||||
yesno () {
|
||||
read_key
|
||||
case $_key in
|
||||
y ) result=0 ;;
|
||||
* ) result=1 ;;
|
||||
esac
|
||||
return $result
|
||||
}
|
||||
|
||||
check_log () {
|
||||
|
@ -37,12 +132,11 @@ check_log () {
|
|||
else
|
||||
# check each unpublished message for sending
|
||||
IFS='
|
||||
'
|
||||
'
|
||||
for u in $uniq
|
||||
do
|
||||
printf "\\n Send message %s? " "$(basename "$u" | sed 's/\.[^.]*$//')"
|
||||
res=$(check_yn)
|
||||
if [ "$res" -eq 1 ]; then
|
||||
if yesno; then
|
||||
# prompt for title and prepare output
|
||||
printf "Title for message %s? " "$(basename "$u" | sed 's/\.[^.]*$//')"
|
||||
read -r title
|
||||
|
@ -56,18 +150,52 @@ check_log () {
|
|||
fi
|
||||
}
|
||||
|
||||
printf "INITIALIZING QEC...\\n"
|
||||
printf "Ready to transmit for [%s]\\n" "${user}"
|
||||
if [ "$numships" -eq 0 ]; then
|
||||
printf "No registered ships found in system.\\n"
|
||||
elif [ "$numships" -eq 1 ]; then
|
||||
check_log "$ships"
|
||||
else
|
||||
IFS='
|
||||
'
|
||||
for f in $ships
|
||||
do
|
||||
check_log "$(basename "$f")"
|
||||
done
|
||||
unset IFS
|
||||
fi
|
||||
main() {
|
||||
parse_input "$@"
|
||||
|
||||
# debug
|
||||
if [ $flag_debug -gt 0 ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
# shortlist for bash completion
|
||||
if [ $flag_shortlist -gt 0 ]; then
|
||||
out="$ships"
|
||||
die "${out}" 0
|
||||
fi
|
||||
|
||||
# version
|
||||
if [ $flag_version -gt 0 ]; then
|
||||
printf "%s\\n" "$version"
|
||||
fi
|
||||
|
||||
# print help
|
||||
if [ $flag_help -gt 0 ]; then
|
||||
show_help
|
||||
fi
|
||||
|
||||
# standard log if no params
|
||||
if [ $arg_log -gt 0 ]; then
|
||||
printf "INITIALIZING QEC...\\n"
|
||||
printf "Ready to transmit for [%s]\\n" "${user}"
|
||||
if [ "$numships" -eq 0 ]; then
|
||||
printf "No registered ships found in system.\\n"
|
||||
elif [ "$numships" -eq 1 ]; then
|
||||
if [ -z "$arg_ship" ] || [ "$ships" = "$arg_ship" ]; then
|
||||
check_log "$ships"
|
||||
fi
|
||||
else
|
||||
IFS='
|
||||
'
|
||||
for f in $ships
|
||||
do
|
||||
if [ -z "$arg_ship" ] || [ "$f" = "$arg_ship" ]; then
|
||||
check_log "$(basename "$f")"
|
||||
fi
|
||||
done
|
||||
unset IFS
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
_log() {
|
||||
local cur
|
||||
cur=${COMP_WORDS[COMP_CWORD]}
|
||||
local helplist
|
||||
helplist=$(log -z)
|
||||
COMPREPLY=( $( compgen -W "$helplist" -- "$cur" ) )
|
||||
}
|
||||
|
||||
# Detect if current shell is ZSH, and if so, load this file in bash
|
||||
# compatibility mode.
|
||||
if [ -n "$ZSH_VERSION" ]; then
|
||||
autoload bashcompinit
|
||||
bashcompinit
|
||||
fi
|
||||
|
||||
complete -o default -o nospace -F _log log
|
Loading…
Reference in New Issue