Permalink
Browse files

support for --host, --global, --system on various commands

This affects edit, append, show, delete, filename, and template install.
When specified, operate on the indicated script
  • Loading branch information...
cxreg committed Aug 9, 2014
1 parent 3bd08c8 commit 4fef3d3610873d9e8044cb5be1922fb056f4228d
Showing with 168 additions and 87 deletions.
  1. +1 −1 README.md
  2. +2 −1 bin/smartcd
  3. +43 −20 lib/core/smartcd
  4. +93 −54 lib/core/smartcd_edit
  5. +27 −10 lib/core/smartcd_template
  6. +2 −1 lib/core/varstash
View
@@ -53,7 +53,7 @@ These are just a few examples of problems people are solving with smartcd.
Once it is installed, you're ready to go. All actions are available through
the `smartcd` command.
Usage: smartcd ( edit | append | show | delete | filename | helper | template | setup | config ) [args]
Usage: smartcd (edit|append|show|delete|filename|helper|template|setup|config) [args]
If you run `smartcd edit enter`, it will launch your editor to allow you to
begin creating a script for the current directory which will run when you
View
@@ -36,7 +36,8 @@ case "$1" in
echo " source $smartcd_lib_path"
;;
edit|append) command="$1"
edit|append|\
delete) command="$1"
shift
source $smartcd_lib_path
smartcd $command "$@"
View
@@ -180,7 +180,7 @@ function smartcd() {
command="reenter"
fi
local usage="Usage: smartcd ( edit | append | show | delete | filename | helper | template | setup | config | export | import ) [args]"
local usage="Usage: smartcd (edit|append|show|delete|filename|helper|template|setup|config|export|import) [args]"
case $command in
-h|help|'') echo $usage;;
cd|pushd|popd) shift; _smartcd $command "$@"; return $?;;
@@ -190,14 +190,36 @@ function smartcd() {
;;
edit|show|cat|\
rm|delete|\
append|filename) shift; local file=$1; shift
local type
case $file in
enter|leave) type="$file";;
bash_enter|bash_leave) type="${file#bash_}";;
*) echo "Usage: smartcd $command ( enter | leave )";;
append|filename) shift
local whence="global"
if [[ -n $SMARTCD_DEFAULT_LOCATION ]]; then
whence=$SMARTCD_DEFAULT_LOCATION
fi
local system=
while [[ $1 =~ ^-- ]]; do
opt="$1"
case $opt in
--host) whence="host";;
--global) whence="global";;
--system) system="true";;
--all) whence="all"; system="both";;
*) echo "$opt unknown"
return
;;
esac
shift
done
local type=$1
shift
case $type in
enter|leave) ;;
bash_enter|bash_leave) type="${type#bash_}";;
*) echo "Usage: smartcd $command [--host|--global|--all] [--system] (enter|leave)"
return
;;
esac
[[ -n $type ]] && _smartcd_file_check $type $command "$@"
[[ -n $type ]] && _smartcd_file_check "$type" "$whence" "$system" "$command" "$@"
;;
template) shift; smartcd_template "$@";;
setup) shift; local setup=$1; shift;
@@ -350,13 +372,13 @@ function _smartcd_check_inode() {
}
function _smartcd_sysdir() {
if [[ -n $SMARTCD_SYSTEM_SCRIPTS ]]; then
local sysdir=$SMARTCD_SYSTEM_SCRIPTS
if [[ -n $SMARTCD_SYSTEM_ETC ]]; then
local sysdir=$SMARTCD_SYSTEM_ETC
else
local sysdir=$(_smartcd_file)
case $sysdir in
~*|$HOME/*) sysdir="/etc/smartcd/scripts";;
*) sysdir=${sysdir/%lib\/core\/smartcd/etc\/scripts};;
~*|$HOME/*) sysdir="/etc/smartcd";;
*) sysdir=${sysdir/%lib\/core\/smartcd/etc};;
esac
fi
@@ -505,11 +527,11 @@ function _smartcd() {
# 1) Run any system-wide scripts
local sysdir=$(_smartcd_sysdir)
if [[ -f "$sysdir$smartcd_current_dir/bash_leave" ]]; then
if [[ -f "$sysdir/scripts$smartcd_current_dir/bash_leave" ]]; then
if (( ${SMARTCD_QUIET:-0} == 0 )); then
echo "smartcd: running $sysdir$smartcd_current_dir/bash_leave"
echo "smartcd: running $sysdir/scripts$smartcd_current_dir/bash_leave"
fi
_smartcd_exec_file "$sysdir$smartcd_current_dir/bash_leave" "$smartcd_current_dir"
_smartcd_exec_file "$sysdir/scripts$smartcd_current_dir/bash_leave" "$smartcd_current_dir"
fi
# 2) Run ~/.smartcd scripts
@@ -617,11 +639,11 @@ function _smartcd() {
# 1) Run any system-wide scripts
local sysdir=$(_smartcd_sysdir)
if [[ -f "$sysdir$smartcd_current_dir/bash_enter" ]]; then
if [[ -f "$sysdir/scripts$smartcd_current_dir/bash_enter" ]]; then
if (( ${SMARTCD_QUIET:-0} == 0 )); then
echo "smartcd: running $sysdir$smartcd_current_dir/bash_enter"
echo "smartcd: running $sysdir/scripts$smartcd_current_dir/bash_enter"
fi
_smartcd_exec_file "$sysdir$smartcd_current_dir/bash_enter" "$smartcd_current_dir"
_smartcd_exec_file "$sysdir/scripts$smartcd_current_dir/bash_enter" "$smartcd_current_dir"
fi
# 2) Run ~/.smartcd scripts
@@ -777,12 +799,13 @@ function _smartcd_exit_trap() {
fi
}
# very old usage, nobody should be doing this now
function bash_enter() {
_smartcd_file_check enter "$@"
_smartcd_file_check enter "global" "" "$@"
}
function bash_leave() {
_smartcd_file_check leave "$@"
_smartcd_file_check leave "global" "" "$@"
}
function fn_exists() {
View
@@ -1,9 +1,7 @@
# wrapper function to preserve legacy usage
function smartcd_edit() {
# Invoke the users editor (or vi) on the relevant file
local type="$1"
local dir="${2:-$(pwd)}"
local action=edit
[[ -n $append ]] && action=append
case $type in
bash_enter|bash_leave) local file=$type; type="${type#bash_}";;
@@ -13,13 +11,34 @@ function smartcd_edit() {
;;
esac
# XXX - global only
local base=$(_smartcd_base)
if [[ -f "$base$dir/$file" ]]; then
smartcd_upgrade_database
fi
local smartcd_dir="$base/scripts$dir"
_smartcd_edit "$type" "global" "$dir" "$file" "$smartcd_dir"
}
function _smartcd_edit() {
# Invoke the users editor (or vi) on the relevant file
local type="$1"
local whence="$2"
local dir="$3"
local file="$4"
local smartcd_dir="$5"
local action=edit
local smartcd_file="$smartcd_dir/$file"
if [[ -f $smartcd_file && ! -w "$smartcd_file" ]]; then
echo "$smartcd_file is not writable, do you need elevated privileges?"
return
fi
[[ -n $append ]] && action=append
if [[ ! -d "$smartcd_dir" ]]; then
command mkdir -p "$smartcd_dir"
fi
@@ -32,7 +51,7 @@ function smartcd_edit() {
local tmpfile=$(mktemp /tmp/smartcd-edit.XXXXXX) line=
local _old_ifs="$IFS"
IFS=$'\n'
if [[ -f "$smartcd_dir/$file" ]]; then
if [[ -f "$smartcd_file" ]]; then
while builtin read -r line; do
case $line in
# extremely hinky replacement here, very whitespace sensitive
@@ -43,9 +62,9 @@ function smartcd_edit() {
;;
*) echo "$line" >> "$tmpfile";;
esac
done < "$smartcd_dir/$file"
done < "$smartcd_file"
else
if [[ $type == "enter" ]]; then
if [[ $type = "enter" ]]; then
local enter_usage="$(cat <<EOF
Some examples are editing your \$PATH or creating
# a temporary alias:
@@ -84,67 +103,63 @@ EOF
IFS=$'\n'
eval $mon_on
if [[ $? == 0 ]]; then
echo -n > "$smartcd_dir/$file"
if [[ $? = 0 ]]; then
echo -n > "$smartcd_file"
if [[ -s "$tmpfile" ]]; then
local writing=1
while builtin read -r line; do
case $line in
"# begin smartcd template"*) writing=;;
"# end smartcd template"*) writing=1;;
*) if [[ -n $writing ]]; then echo "$line" >> "$smartcd_dir/$file"; fi;;
*) if [[ -n $writing ]]; then echo "$line" >> "$smartcd_file"; fi;;
esac
done < "$tmpfile"
fi
fi
IFS="$_old_ifs"
command rm "$tmpfile"
elif [[ -n $append ]]; then
command cat >> "$smartcd_dir/$file"
command cat >> "$smartcd_file"
else
command cat > "$smartcd_dir/$file"
command cat > "$smartcd_file"
fi
if [[ -f "$smartcd_dir/$file" && ! -s "$smartcd_dir/$file" ]]; then
if [[ -f "$smartcd_file" && ! -s "$smartcd_file" ]]; then
# Delete any file that is left empty
command rm "$smartcd_dir/$file"
command rm "$smartcd_file"
fi
}
function smartcd_append() {
function _smartcd_append() {
local append=1
smartcd_edit "$@"
_smartcd_edit "$@"
}
function smartcd_delete() {
# Invoke the users editor (or vi) on the relevant file
function _smartcd_delete() {
# Delete the file if it exists
local type="$1"
local dir="${2:-$(pwd)}"
case $type in
enter|leave) local file="bash_$type";;
*) echo "Usage: smartcd_delete ( enter | leave )"
return
;;
esac
local base=$(_smartcd_base)
local smartcd_dir="$base/scripts$dir"
local whence="$2"
local system="$3"
local dir="$4"
local file="$5"
local smartcd_dir="$6"
if [[ -f "$smartcd_dir/$file" ]]; then
echo "Removing $smartcd_dir/$file"
rm "$smartcd_dir/$file"
else
echo "No $type script for $dir"
[[ -n $system ]] && local issystem="system "
echo "No $issystem$whence $type script for $dir"
fi
}
# _smartcd_file_check <type> <action> [<directory>]
function _smartcd_file_check() {
local type="$1"
local action="$2"
local dir="$3"
local whence="$2"
local system="$3"
local action="$4"
local dir="$5"
if [[ -n $dir ]]; then
# canonicalize
@@ -157,32 +172,56 @@ function _smartcd_file_check() {
dir="$(pwd)"
fi
local base=$(_smartcd_base)
local confdir="$base/scripts$dir"
if [[ -n $system ]]; then
local base=$(_smartcd_sysdir)
else
local base=$(_smartcd_base)
fi
if [[ $whence = "host" ]]; then
local host=${SMARTCD_HOSTNAME:-$(hostname)}
base="$base/hosts/$host"
fi
local file="bash_$type"
if [[ -f "$base$dir/$file" ]]; then
smartcd_upgrade_database
fi
case $action in
-h|help) echo "Usage: $type [ edit | append | delete | show | filename ]";;
-f|filename) echo "$confdir/$file";;
-e|edit) smartcd_edit $type "$dir";;
-a|append) smartcd_append $type "$dir";;
-d|rm|delete) smartcd_delete $type "$dir";;
-l|show|cat|'') if [[ -f "$confdir/$file" ]]; then
case $action in
-l|show|cat) echo "# ---8<--- begin $confdir/$file"
command cat "$confdir/$file"
echo "# ---8<--- end $confdir/$file"
;;
esac
else
echo "No $type script for $dir"
fi
;;
esac
if [[ $whence = "all" ]]; then
case $action in
-f|filename|\
-d|rm|delete|\
-l|show|cat|'') _smartcd_file_check "$type" "global" "true" "$action" "$dir"
_smartcd_file_check "$type" "host" "true" "$action" "$dir"
_smartcd_file_check "$type" "global" "" "$action"
_smartcd_file_check "$type" "host" "" "$action"
;;
*) echo "--all not supported for smartcd $action"
return
;;
esac
else
local smartcd_dir="$base/scripts$dir"
case $action in
-h|help) echo "Usage: $type [edit|append|delete|show|filename]";;
-f|filename) echo "$smartcd_dir/$file";;
-e|edit) _smartcd_edit "$type" "$whence" "$dir" "$file" "$smartcd_dir";;
-a|append) _smartcd_append "$type" "$whence" "$dir" "$file" "$smartcd_dir";;
-d|rm|delete) _smartcd_delete "$type" "$whence" "$system" "$dir" "$file" "$smartcd_dir";;
-l|show|cat|'') if [[ -f "$smartcd_dir/$file" ]]; then
case $action in
-l|show|cat) echo "# ---8<--- begin $smartcd_dir/$file"
command cat "$smartcd_dir/$file"
echo "# ---8<--- end $smartcd_dir/$file"
;;
esac
else
[[ -n $system ]] && local issystem="system "
echo "No $issystem$whence $type script for $dir"
fi
;;
esac
fi
}
# vim: filetype=sh autoindent expandtab shiftwidth=4 softtabstop=4
Oops, something went wrong.

0 comments on commit 4fef3d3

Please sign in to comment.