diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2015-09-18 19:23:05 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2015-09-18 19:23:05 +0200 |
commit | 94bf820fc9c85effddb7214a9f0b0a154f23466f (patch) | |
tree | 3ecc6a295d8a7cfe74ac945210ebc3748bc2ba6a | |
parent | 0cfdf27ff951306458502e049dda1ad0748bb830 (diff) | |
parent | 17e59dbadbff1c8d68b794aa77fe3002e6362375 (diff) |
Merge pull request #71 from matthiasbeyer/add-repl
Add repl
-rwxr-xr-x | nix-script | 16 | ||||
-rwxr-xr-x | nix-script-repl.sh | 153 | ||||
-rw-r--r-- | nix-utils.sh | 26 |
3 files changed, 179 insertions, 16 deletions
@@ -25,15 +25,6 @@ VERBOSE=0 source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh -# -# Get the name of the script file for the command passed as argument -# -# Does not check whether the file exists. -# -script_for() { - echo "$(dirname ${BASH_SOURCE[0]})/nix-script-${1}.sh" -} - SHIFT_ARGS=0 # @@ -56,13 +47,6 @@ shift_n() { } # -# List all available commands -# -all_commands() { - find $(dirname ${BASH_SOURCE[0]}) -type f -name "nix-script-*.sh" -} - -# # Parse the arguments for this script # for cmd diff --git a/nix-script-repl.sh b/nix-script-repl.sh new file mode 100755 index 0000000..5e52851 --- /dev/null +++ b/nix-script-repl.sh @@ -0,0 +1,153 @@ +#!/usr/bin/env bash + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +usage() { + cat <<EOS >&2 + $(help_synopsis "${BASH_SOURCE[0]}" "[-h] [--shell]") + + --shell If a command is not available, try to execute it in this shell + -h Show this help and exit + + All nixos-script subcommands and their respective arguments are available. + + In addition to the normal commands, these commands are available: + + list | Lists all available commands + +$(help_end "repl") +EOS +} + +Color_Off='\e[0m' +Red='\e[0;31m' +Green='\e[32m' + +__SHELL=0 + +for arg +do + case $arg in + + --shell) + __SHELL=1 + dbg "__SHELL = $__SHELL" + ;; + + "-h") + usage + exit 0 + ;; + + *) + ;; + esac +done + +run() { + dbg $*; $* +} + +__exists() { + dbg "Exists '$1'? ..." + which $1 >/dev/null 2>/dev/null +} + +prompt() { + echo -en "${Green}nix-script repl >${Color_Off} " +} + +__run_if_exists() { + [[ $(__exists $1) ]] && run $* || false +} + +__list() { + dbg "Listing commands" + caller_util_list_subcommands_for "nix-script" +} + +__verbosity() { + case $1 in + on) + export VERBOSE=1 + dbg "VERBOSE = $VERBOSE" + stdout "Verbosity is now ON" + ;; + off) + export VERBOSE=0 + dbg "VERBOSE = $VERBOSE" + stdout "Verbosity is now ON" + ;; + *) + stderr "Unknown argument: $1" + stderr "Usage: verbosity [on|off]" + ;; + esac +} + +__debugging() { + case $1 in + on) + export DEBUG=1 + dbg "DEBUG = $DEBUG" + stdout "Debugging is now ON" + ;; + off) + export DEBUG=0 + dbg "DEBUG = $DEBUG" + stdout "Debugging is now ON" + ;; + *) + stderr "Unknown argument: $1" + stderr "Usage: debuggig [on|off]" + ;; + esac +} + +__exit() { + stdout "Ready. Bye-Bye!" + [[ -z "$1" ]] && exit 0 + exit $1 +} + +__builtin__() { + local str=$1; shift + local cmd=$1; shift + local args=$* + + [[ $COMMAND =~ $str ]] && $cmd $args && prompt +} + +prompt +while read COMMAND ARGS +do + __builtin__ "help" usage $ARGS && continue + __builtin__ "exit" __exit $ARGS && continue + __builtin__ "list" __list $ARGS && continue + __builtin__ "verbosity" __verbosity $ARGS && continue + __builtin__ "debugging" __debugging $ARGS && continue + + dbg "Got '$COMMAND' with args '$ARGS'" + stdout "Searching for script for '$COMMAND'" + SCRIPT=$(script_for $COMMAND) + + if [[ ! -f $SCRIPT || ! -x $SCRIPT ]] + then + dbg "Not available or executable: $COMMAND" + # + # Checks whether the args include bash-specific things like || or &&, as + # these cannot be executed by nix-script repl by now, and prints a + # warning and does not allow execution of these. + # + if [[ "$ARGS" =~ ^[a-zA-Z0-9_\ ]*$ && $__SHELL -eq 1 ]] + then + dbg "Executing: '$COMMAND $ARGS'" + $COMMAND $ARGS + fi + else + stdout "Calling: '$COMMAND $ARGS'" + bash $SCRIPT $ARGS + fi + prompt +done + diff --git a/nix-utils.sh b/nix-utils.sh index 9e2fd3c..a34a2ee 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -6,6 +6,13 @@ Yellow='\e[0;33m' Green='\e[0;32m' # +# Check whether a string (2nd arg) contains a substring (1st arg) +# +stringcontains() { + [ -z "${2##*$1*}" ] +} + +# # Print on stderr, in red # stderr() { @@ -28,6 +35,25 @@ stdout() { } # +# List all available commands as script path +# +all_commands() { + find $(dirname ${BASH_SOURCE[0]}) \ + -type f \ + -executable \ + -name "nix-script-*.sh" +} + +# +# Get the name of the script file for the command passed as argument +# +# Does not check whether the file exists. +# +script_for() { + echo "$(dirname ${BASH_SOURCE[0]})/nix-script-${1}.sh" +} + +# # Get the command name from a script path # scriptname_to_command() { |