made some little improvements

This commit is contained in:
deepend 2023-08-02 14:10:04 -06:00
parent fa8fa53e48
commit e5a6bf1d84
1 changed files with 105 additions and 98 deletions

203
tilde
View File

@ -74,34 +74,34 @@ prompt_confirm() {
}
help_message() {
printf "%s version %s\n" "$PROGNAME" "$VERSION"
printf "wrapper for user-submitted scripts\n"
printf "supports submission and admin approval\n"
usage
cat <<-EOF
Usage: $PROGNAME [options] [command]
Options:
-h, --help Show this help message and exit
-v, --version Show version information
Commands:
list Show a list of approved user scripts
submit Start the submission flow for your own script
about <script> Show description and details of a specific script
approve Enter the approval queue for pending scripts (requires root)
revoke <script> Revoke a previously approved script (requires root)
run <script> Run an approved script
Please add /tilde/bin to your PATH to use approved scripts without this wrapper.
EOF
}
usage() {
printf "\nusage: %s [help|list|submit|about|script_name]\n\n" "$PROGNAME"
printf " list - show a list of approved userscripts\n"
printf " submit - start the submission flow for your own script\n"
if isroot; then
printf " approve - enter the approval queue\n"
printf " revoke <script_name> - send a script back to the author and remove from /tilde/bin\n"
fi
printf " about <script_name> - get the description for script_name\n"
printf " <script_name> - run script_name with all remaining args are passed to the script\n"
case ":$PATH:" in
*:/tilde/bin:*)
;;
*)
printf "\nadd /tilde/bin to your PATH to use approved scripts without this wrapper\n"
printf "if you're using bash, run the following to add it quickly\n"
printf " echo 'export PATH=\$PATH:/tilde/bin' >> ~/.bashrc && source ~/.bashrc\n"
;;
esac
printf "\nusage: %s [help|list|submit|about|approve|revoke|run|script_name]\n\n" "$PROGNAME"
printf " help - Show usage information\n"
printf " list - Show a list of approved user scripts\n"
printf " submit - Start the submission flow for your own script\n"
printf " about <script> - Show description and details of a specific script\n"
printf " approve - Enter the approval queue for pending scripts (requires root)\n"
printf " revoke <script> - Revoke a previously approved script (requires root)\n"
printf " run <script> - Run an approved script\n"
}
verify_script_name() {
@ -129,20 +129,18 @@ verify_script_name() {
esac
}
submission_checklist() {
cat <<- _EOF_
requirements for submitting a user script or program:
Requirements for submitting a user script or program:
- placed in your ~/bin
- executable
- responds to help or --help
- no name collisions with existing scripts or $PROGNAME subcommands
- Placed in your ~/bin
- Executable
- Responds to help or --help
- No name collisions with existing scripts or $PROGNAME subcommands
_EOF_
}
mail_body() {
cat <<- _EOF_
Subject: tilde script submission from ${user}
@ -159,36 +157,32 @@ description:
$2
-----------------------------------------------------------------------
you'll find the script and description in: /tilde/pending-submissions/$user/$1
You'll find the script and description in: /tilde/pending-submissions/$user/$1
run this to see the approval queue:
Run this to see the approval queue:
sudo tilde approve
_EOF_
}
# Trap signals
trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT
# Parse command-line
case $1 in
-h | --help | help)
help_message; exit
-h | --help)
help_message
exit 0
;;
-v | --version)
printf "%s" "$VERSION"
printf "%s\n" "$VERSION"
exit 0
;;
-* | --*)
usage
error_exit "Unknown option $1"
;;
list | ls)
printf "available scripts:\n\n"
list)
# List approved scripts
printf "Available scripts:\n\n"
for scr in /tilde/bin/*; do
if [ -f "$scr" ]; then
script_name=$(basename "$scr")
@ -200,79 +194,80 @@ case $1 in
done
;;
about | apropos | description)
about | description)
# Show details about a specific script
if [ -z "$2" ]; then
printf "Please provide a script name.\n"
usage
exit 1
fi
if [ -f "/tilde/descriptions/$2" ]; then
cat "/tilde/descriptions/$2"
else
printf "%s not found. try %s list to see available user scripts.\n" "$2" "$PROGNAME"
printf "%s not found. Use '%s list' to see available user scripts.\n" "$2" "$PROGNAME"
exit 1
fi
;;
submit)
printf "hello, %s! so it's time to submit your script?\n" "$user"
# Submit a user script
printf "Hello, %s! Ready to submit your script?\n" "$user"
submission_checklist
prompt_confirm "are you ready to continue?" || exit
printf "enter the name of your script: "
read -r script_name
prompt_confirm "Are you ready to continue?" || exit
printf "Enter the name of your script: "
read -r script_name
verify_script_name "$script_name"
if [ -x "$HOME/bin/$script_name" ]; then
printf "cool, found your script\n"
if [ -x "/tilde/pending-submissions/$user/$script_name/$script_name" ]; then
error_exit "you've already submitted $script_name"
fi
else
if [ ! -x "$HOME/bin/$script_name" ]; then
error_exit "$script_name not found in ~/bin"
fi
printf "enter a description of your script: \n"
printf "Enter a description for your script: \n"
read -r description
printf "\nyour script, along with your description will be sent to the admins for approval\n"
prompt_confirm "ready to submit?" || exit
printf "\nYour script and description will be sent to the admins for approval.\n"
prompt_confirm "Ready to submit?" || exit
# submit now
# Submit now
mkdir -p "/tilde/pending-submissions/$user/$script_name"
ln -s "$HOME/bin/$script_name" "/tilde/pending-submissions/$user/$script_name/$script_name"
printf "%s\n" "$description" > "/tilde/pending-submissions/$user/$script_name/description.txt"
mail_body "$script_name" "$description" | sendmail root
printf "script submitted. thanks! :)\n"
mail_body "$script_name" "$description" | /usr/sbin/sendmail root
printf "Script submitted. Thank you! :)\n"
;;
approve)
if ! isroot; then
error_exit "re-run this as root to access the approval queue"
fi
# Approve pending scripts (requires root)
isroot || \
error_exit "Re-run this as root to access the approval queue."
printf "Welcome to the approval queue\n\n"
printf "welcome to the approval queue\n\n"
for user_submission in /tilde/pending-submissions/*; do
user=$(basename "$user_submission")
for script in "$user_submission"/*; do
script_name=$(basename "$script")
[ -f "$script/approved" ] && continue
for user in /tilde/pending-submissions/*; do
for scr in $user/*; do
user=$(basename "$user")
script_name=$(basename "$scr")
[ -f "$scr/approved" ] && continue
script="$scr/$script_name"
printf "Script name: %s\n" "$script_name"
cat "$script/description.txt"
prompt_confirm "Approve?" || continue
if [ -f "$script" ]; then
printf "%s by %s\n" "$script_name" "$user"
cat "$scr/description.txt"
prompt_confirm "approve?" || continue
ln -s "$(readlink -f "$script")" "/tilde/bin/$script_name"
cp "$scr/description.txt" "/tilde/descriptions/$script_name"
touch "$scr/approved"
chmod 664 /tilde/descriptions/*
printf "your submission of %s has been approved and is now available at /tilde/bin/%s" "$script_name" "$script_name" \
| sendmail "$user"
fi
ln -s "$(readlink -f "$script/$script_name")" "/tilde/bin/$script_name"
cp "$script/description.txt" "/tilde/descriptions/$script_name"
touch "$script/approved"
chmod 664 "/tilde/descriptions/$script_name"
printf "Your submission of %s has been approved and is now available at /tilde/bin/%s\n" "$script_name" "$script_name" \
| sendmail "$user"
done
done
printf "~~done for now~~\n"
printf "~~Done for now~~\n"
;;
revoke)
# Revoke an approved script (requires root)
isroot || \
error_exit "re-run this as sudo to access the revoke menu"
error_exit "Re-run this as root to access the revoke menu."
[ -f "/tilde/bin/$2" ] || \
error_exit "$2 isn't an approved script"
@ -292,20 +287,32 @@ case $1 in
printf "%s revoked and returned to author" "$2"
;;
*)
if [ -z "$1" ]; then
help_message
exit
elif [ -x "/tilde/bin/$1" ]; then
prog="/tilde/bin/$1"
run)
# Run an approved script
if [ -z "$2" ]; then
printf "Please provide a script name.\n"
usage
exit 1
fi
if [ -x "/tilde/bin/$2" ]; then
prog="/tilde/bin/$2"
shift
exec "$prog" "$@"
else
printf "%s not found. check %s list to see what's available\n\n" "$1" "$PROGNAME"
help_message
exit
printf "%s not found or not approved yet. Use '%s list' to see available user scripts.\n" "$2" "$PROGNAME"
exit 1
fi
;;
esac
"")
usage
exit 0
;;
*)
printf "Invalid command: %s\n" "$1"
usage
exit 1
;;
esac