From 4fa7e95e7f56f0c27b944e8f3b649b98dfaf87eb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 29 May 2015 22:26:27 +0200 Subject: Add: nix-script repl This is a combination of 23 commits: * Add first draft for nix-shell repl * Remove functions which are in nix-utils.sh * Minify if-then-exit * Remove sudo run... why do I actually have this? * Include helpers * Add help text * Remove old arguments in parsing code * Dont print on stderr, do debugging output here * Remove explain checker, use dbg() as output function * Fix checker * Use bash to print * Use "exit" or "quit" to exit nix-repl * Remove doubled caller code * Move: script_for() function to helpers * Remove old variables * Fix bash call * move all_commands() function to nix-utils.sh * Formatted and extended all_commands() function * Lists executables only. * Shorten error message * Add helper to check whether a string contains a string * Add possibility to execute simple bash commands in repl * The more debug output, the better * More things, we should really squash this stuff --- nix-script | 16 --------- nix-script-repl.sh | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ nix-utils.sh | 26 ++++++++++++++ 3 files changed, 127 insertions(+), 16 deletions(-) create mode 100755 nix-script-repl.sh diff --git a/nix-script b/nix-script index 3860f20..5bdfe15 100755 --- a/nix-script +++ b/nix-script @@ -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 # @@ -55,13 +46,6 @@ shift_n() { echo $* } -# -# List all available commands -# -all_commands() { - find $(dirname ${BASH_SOURCE[0]}) -type f -name "nix-script-*.sh" -} - # # Parse the arguments for this script # diff --git a/nix-script-repl.sh b/nix-script-repl.sh new file mode 100755 index 0000000..21252d5 --- /dev/null +++ b/nix-script-repl.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +usage() { + cat <&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" +} + +prompt +while read COMMAND ARGS +do + [[ $COMMAND =~ "quit" || $COMMAND =~ "exit" ]] && break + [[ $COMMAND =~ "help" ]] && usage && prompt && continue + [[ $COMMAND =~ "list" ]] && __list && prompt && 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 + +stdout "Ready. Bye-Bye!" + diff --git a/nix-utils.sh b/nix-utils.sh index 9e2fd3c..a34a2ee 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -5,6 +5,13 @@ Red='\e[0;31m' 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 # @@ -27,6 +34,25 @@ stdout() { [[ $VERBOSE -eq 1 ]] && echo -e "${Green}[$(basename $0)]:${Color_Off} $*" } +# +# 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 # -- cgit v1.2.3