Skip to content

Instantly share code, notes, and snippets.

@realchrisolin
Created February 13, 2020 20:47
Show Gist options
  • Save realchrisolin/b56e6503d7c00261a595dae1cc210140 to your computer and use it in GitHub Desktop.
Save realchrisolin/b56e6503d7c00261a595dae1cc210140 to your computer and use it in GitHub Desktop.
utility script to automatically create new directories associated with support cases (to manage log files from multiple customers/cases)
########################################################
# vim:ft=sh:smd:ar:si:et:bg=dark:ts=4:sw=4
# file: newcase
# author: Chris Olin - http://chrisolin.com
# purpose: automatically create new case directories
# created date: 12-12-2012
# license:
########################################################
#post-run stuff
##########################
unset CASEID
unset CASESUB
unset CASECLIENT
#you want this if your ZSH prompt is getting the WD with "%~". see https://github.com/nojhan/liquidprompt/issues/124 for details
if [ "$SHELL" = '/bin/zsh' ]; then
unsetopt AUTO_NAME_DIRS
setopt nullglob
fi
if [ "$SHELL" = '/bin/bash' ]; then
shopt -s nullglob
fi
#Where is our case directory located?
CASEDIR=$HOME/Cases
#Let's set some useful aliases
#echo -e 'Creating sed aliases\n.'
echo -e 'Creating clipboard aliases.\n'
alias getcid="pwd | perl -pe 's/^.+\/([0-9]+)\s-\s(.+)\s-\s(.+)$/\$1/e'"
alias getcclient="pwd | perl -pe 's/^.+\/([0-9]+)\s-\s(.+)\s-\s(.+)$/\$2/e'"
alias getcsub="pwd | perl -pe 's/^.+\/([0-9]+)\s-\s(.+)\s-\s(.+)$/\$3/e'"
alias setcid='export CASEID="$(getcid)"'
alias setcclient='export CASECLIENT="$(getcclient)"'
alias setcsub='export CASESUB="$(getcsub)"'
alias setcasevars='setcid && setcclient && setcsub'
if [ "$OS" = 'CYGWIN_NT-5.1' ] || [ "$OS" = 'Windows_NT' ]; then #updated to work in any (*coughbashandzshsneeze*) posix-compliant shell -- http://tiny.cc/0bq8ww
alias cid='export CASEID="$(getcid)" && echo "Echoing \"$CASEID\" to clipboard." && echo "$CASEID" >/dev/clipboard '
alias cacc='export CASECLIENT="$(getcclient)" && echo "Echoing \"$CASECLIENT\" to clipboard." && echo "$CASECLIENT" >/dev/clipboard'
alias csub='export CASESUB="$(getcsub)" && echo "Echoing \"$CASESUB\" to clipboard." && echo "$CASESUB" >/dev/clipboard'
alias call='export CASEID="$(getcid)" && CASECLIENT="$(getcclient)" && CASESUB="$(getcsub)" && echo "Echoing all case info to clipboard." && echo "$CASEID - $CASECLIENT - $CASESUB" >/dev/clipboard'
fi
##########################
#Sometimes we might want to pass the case ID as an argument
CASEID=$1
#Standard quetions
if [ -z "$CASEID" ]; then
echo -e 'Please enter the Case ID.'
read CASEID
echo -e '\n'
fi
#Let's make sure the directory doesn't already exist (and switch to it if it does).
EXISTDIR=$(ls "$CASEDIR/" | grep $CASEID)
if [ -n "$EXISTDIR" ]; then
if [ -d "$CASEDIR/$EXISTDIR" ]; then
echo "Directory already exists. Changing this console's CWD to it."
cd "$CASEDIR/$CASEID"*
if [ -n "$STY" ]; then
setcasevars
screen -X title "$CASEID - $CASECLIENT"
fi
return 0
fi
fi
echo -e 'Who is the client? (Please do not use hyphen-minus (-) character. Use -- or minus [−] character [unicode U+2212] instead.)'
read CASECLIENT
echo -e '\n'
echo -e 'Thank you. Please enter the case subject. (Same rules using hypen-minus character applies here too).'
read CASESUB
echo -e '\n'
#Store directory format in variable
CDIR="$(echo $CASEDIR/$CASEID - $CASECLIENT - $CASESUB)"
#This is where the magic is
echo -e "Creating directory and changing this console's CWD to it."
echo -e '\n'
mkdir "$CASEDIR/$CASEID - $CASECLIENT - $CASESUB"
if [ -d "$CDIR" ]; then
cd "$CASEDIR/$CASEID - $CASECLIENT - $CASESUB"
if [ -n "$STY" ]; then
screen -X title "'$CASEID' - '$CASECLIENT'"
fi
echo -e 'Success! Have a nice day!'
else
echo -e 'Failure! Directory does not appear to have been created.'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment