145 lines
4.0 KiB
Bash
Executable File
145 lines
4.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
# Settings variables go here
|
|
COMMAND="get"
|
|
DIRECTORY=""
|
|
CONTEXT=""
|
|
BYPASS_SANITY_CHECKS=""
|
|
|
|
show_help() {
|
|
cat <<EOF
|
|
Usage: slatectx-pwd [--context ctx] (set dir|get|clear|get-file)
|
|
|
|
slatectx-pwd can be used to set, get or clear the working directory for a given context.
|
|
|
|
COMMANDS:
|
|
Note, that only one of them can be used,
|
|
if multiple are specified the last one is used and others are ignored
|
|
|
|
set <directory> - sets the contexts pwd to the given directory.
|
|
slatectx-pwd will validate that the directory exists and use the
|
|
realpath(1) command to get the abolute path (symlink expansion is disabled).
|
|
Note: when used as the last option <directory> is optional,
|
|
it will fall back to the pwd if left out.
|
|
|
|
get - returns the content of the pwd-file after verifying that
|
|
it is an existing directory, if the check fails it pretends
|
|
that the directory is empty and return an exit code 3.
|
|
|
|
clear - deletes the pwd-file for the given context (if not write protected).
|
|
|
|
get-file - prints out the path of the pwd-file
|
|
|
|
OPTIONS:
|
|
|
|
--context <ctx> - manually set a context name
|
|
if unset the command slatectx-get-context-name will be used.
|
|
Note: The automatically derive context name will not take the
|
|
pwd into account as this would result in recursive lookups.
|
|
It uses the --not-by-path option for that.
|
|
|
|
--bypass-sanity-checks - basically what it says on the tin
|
|
you are only supposed to use this for testing.
|
|
|
|
EXIT CODES:
|
|
0 - success
|
|
1 - invalid command or unusable envoirnment
|
|
2 - Permission error
|
|
3 - sanity check kicked in (can be bypassed but shouldn't)
|
|
EOF
|
|
}
|
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case "$1" in
|
|
"set") COMMAND="set"
|
|
if [[ "$#" -gt 1 ]] ; then
|
|
DIRECTORY="$2" ; shift 2
|
|
else
|
|
shift 1
|
|
fi ;;
|
|
get) COMMAND="get"; shift 1;;
|
|
clear) COMMAND="clear"; shift 1;;
|
|
get-file) COMMAND="get-file"; shift 1;;
|
|
|
|
--context) CONTEXT="$2"; shift 2;;
|
|
--bypass-sanity-check) BYPASS_SANITY_CHECKS="y"; shift 1;;
|
|
|
|
--help) show_help; exit 0;;
|
|
*) printf "Unknown option: %s\n" "$1"; exit 1;;
|
|
esac
|
|
done
|
|
|
|
# Logic goes here
|
|
|
|
[ -z "$CONTEXT" ] && CONTEXT="$(slatectx-get-context-name --not-by-path)"
|
|
|
|
if [ -z "$CONTEXT" ] ; then
|
|
echo "Unable to get context name!" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -d "$XDG_RUNTIME_DIR" ] || [ ! -w "$XDG_RUNTIME_DIR" ] ; then
|
|
echo "XDG_RUNTIME_DIR is not set or doesn't point to a weritable directory!" >&2
|
|
exit 1
|
|
fi
|
|
|
|
slatectx_context_pwd="$XDG_RUNTIME_DIR/slatectx-pwd/$CONTEXT"
|
|
|
|
case "$COMMAND" in
|
|
set)
|
|
[ -z "$DIRECTORY" ] && DIRECTORY="$PWD"
|
|
if [ ! -d "$DIRECTORY" ] && [ -z "$BYPASS_SANITY_CHECKS" ] ; then
|
|
echo "Directory doen't exist, not setting new slatectx pwd" >&2
|
|
exit 3
|
|
fi
|
|
[ ! -d "$XDG_RUNTIME_DIR/slatectx-pwd" ] && mkdir -p "$XDG_RUNTIME_DIR/slatectx-pwd"
|
|
if [ ! -w "$slatectx_context_pwd" ] && [ -f "$slatectx_context_pwd" ] ; then
|
|
echo "pwd file for this context is write protected, not setting!" >&2
|
|
exit 2
|
|
fi
|
|
if [ -z "$BYPASS_SANITY_CHECKS" ] ; then
|
|
# the busybox version throws so,e errors here, we simply ignore them
|
|
realpath -s -- "$DIRECTORY" 2> /dev/null | tail -n 1 > "$slatectx_context_pwd"
|
|
else
|
|
printf "%s" "$DIRECTORY" > "$slatectx_context_pwd"
|
|
fi
|
|
;;
|
|
get)
|
|
if [ -r "$slatectx_context_pwd" ] ; then
|
|
if [ -n "$BYPASS_SANITY_CHECKS" ] ; then
|
|
cat "$slatectx_context_pwd"
|
|
exit 0
|
|
fi
|
|
if [[ "$(wc -c < "$slatectx_context_pwd")" -gt 10000 ]] ; then
|
|
echo "pwd-file is too large, pretending it to be empty!" >&2
|
|
exit 3
|
|
fi
|
|
DIRECTORY="$(cat "$slatectx_context_pwd")"
|
|
if [ ! -d "$DIRECTORY" ]; then
|
|
echo "pwd-file does not contain a directory, pretending it to be empty!" >&2
|
|
exit 3
|
|
fi
|
|
printf "%s\n" "$DIRECTORY"
|
|
fi
|
|
;;
|
|
get-file)
|
|
printf "%s\n" "$slatectx_context_pwd"
|
|
;;
|
|
clear)
|
|
if [ -w "$slatectx_context_pwd" ] ; then
|
|
rm "$slatectx_context_pwd"
|
|
elif [ ! -f "$slatectx_context_pwd" ] ; then
|
|
echo "pwd-file for this context is write protected, not clearing!" >&2
|
|
exit 2
|
|
fi
|
|
;;
|
|
*)
|
|
echo "Unknown command !" >&2
|
|
show_help >&2
|
|
echo 1
|
|
;;
|
|
esac
|
|
|