Skip to content
Permalink
Browse files

Added basic jobot files

  • Loading branch information
BenjaminEHowe committed Jun 9, 2019
0 parents commit 81fc6ce74df024be95cff495b18bc2c10ed74be3
Showing with 859 additions and 0 deletions.
  1. +674 −0 LICENSE
  2. +2 −0 README.md
  3. +9 −0 debugMessage
  4. +88 −0 jobot
  5. +8 −0 jobot-runner
  6. +12 −0 modules/echo/default
  7. +66 −0 modules/go/default
674 LICENSE

Large diffs are not rendered by default.

@@ -0,0 +1,2 @@
# jobot
A virtual assistant
@@ -0,0 +1,9 @@
debugMessage() {
if test "$1"; then
if test $debug; then
echo "$1"
fi
else
echo 'ERROR (internal): debugMessage called but no argument supplied!'
fi
}
88 jobot
@@ -0,0 +1,88 @@
#!/bin/sh

print() {
printf %s\\n "$1"
}

debugMessage() {
if test "$1"; then
if test $debug; then
print "$1"
fi
else
print 'ERROR (internal): debugMessage called but no argument supplied!'
fi
}

# check if we've been passed the --debug argument
if test $1 && test $1 = '--debug'; then
debug=1
print 'Debugging mode...'
shift
fi

# if we have no arguments, print help
if test $# = 0; then
print "Usage: $0 MODULE [ARGUMENT...]"
exit 0
fi

# the argument is the module name
moduleName=$1
shift
debugMessage "Required module: $moduleName"

# we must have an argument... so we need to discover modules!
modules=''
if test -e 'modules'; then
debugMessage 'A modules directory exists'
modules=$modules$(find modules/* -maxdepth 0 -type d)
else
debugMessage 'No modules directory exists'
fi
if test -e "$HOME/.jobot/modules"; then
debugMessage 'A modules directory (within ~/.jobot) exists'
modules=$modules$(find $HOME/.jobot/modules/* -maxdepth 0 -type d)
else
debugMessage 'No modules directory (within ~/.jobot) exists'
fi
if test -n "$JOBOT_MODULES"; then
debugMessage 'An environment variable exists'
print 'TODO: parse $JOBOT_MODULES environment variable'
# the below parses the path environment variable - will be useful later
#for directory in $(echo $PATH | sed -e 's/:/ /g'); do
# print $directory
#done
else
debugMessage 'No environment variable exists'
fi

# if we're in debug mode, print a list of the discovered modules
if test $debug; then
print 'Modules available:'
for directory in $modules; do
print " $directory"
done
fi

# find the requested module
for module in $modules; do
thisModuleName=$(echo $module | grep -e '[^\/]*$' -o)
if test $thisModuleName = $moduleName; then
# validate that a default file actually exists...
if test -e "$module/default"; then
# execute!
debugMessage "Executing $module/default"
cd $module
. "./default"
exit 0
else
print "ERROR: $module/default does not exist!"
exit 2
fi
fi
done

# if we get this far then we never found a matching module...
print "ERROR: module $moduleName not found!"
exit 1
@@ -0,0 +1,8 @@
#/bin/sh

# set the directory below to the jobot installation directory
# note: use $HOME instead of ~
JOBOT_DIR="/path/to/jobot/directory"

cd "$JOBOT_DIR"
. "$JOBOT_DIR/jobot"
@@ -0,0 +1,12 @@
#!/bin/sh

. ../../debugMessage

debugMessage "I see $# argument(s)"

count=0
until test $# = 0; do
i=$((i+1))
echo "Argument $i: $1"
shift
done
@@ -0,0 +1,66 @@
#!/bin/sh

. ../../debugMessage

findUrl() {
if test $# -lt 1; then
echo 'ERROR (internal): findUrl requires term argument'
return 1
fi
if test $debug; then
eval termForDebug=$1
debugMessage "Term: $termForDebug:"
fi
for file in $files; do
debugMessage " Searching $file"
url=$(cat "$file" | sed -rn "s/^$1\t+([^\t]+)$/\1/p")
if test -n "$url"; then
debugMessage ' Found!'
break
fi
done
}

# set the term and shift so it's no longer an arg
term=$1
shift

# find the files where we might find a term
# TODO: what if $HOME/.jobot/go doesn't exist?
# TODO: create an installer module?
files=$(find "$HOME/.jobot/go" -type f)

if test $# -eq 0; then
findUrl "$term"
else
findUrl "${term}\($#\)"
if test -z "$url"; then
findUrl "${term}\(n\)"
fi
if test -z "$url"; then
findUrl "$term"
fi
# evaluate the url to fill the args
eval url="$url"
fi

# if a URL was found...
if test -n "$url"; then
# open it using an appropriate command
if command -v xdg-open > /dev/null; then
debugMessage "Opening $url using xdg-open"
xdg-open \'"$url"\'
elif command -v powershell.exe > /dev/null; then
debugMessage "Opening $url using powershell.exe"
powershell.exe Start \'"$url"\'
elif command -v open > /dev/null; then
# n.b. open exists on linux, but doesn't do what we want it to...
# on mac it works as we'd like
debugMessage "Opening $url using open"
open \'"$url"\'
else
echo 'ERROR: no supported browser handler found!'
fi
else
echo 'ERROR: no URL found!'
fi

0 comments on commit 81fc6ce

Please sign in to comment.
You can’t perform that action at this time.