From 254fa074c14766be46187ff663a536fe63adca7f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 6 Jun 2015 11:29:53 +0200 Subject: Add util for listing all commands for a caller --- nix-utils.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nix-utils.sh b/nix-utils.sh index 69dbf12..c5dd151 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -131,3 +131,9 @@ __git_current_branch() { branch_name=${branch_name##refs/heads/} ([[ -z "$branch_name" ]] && git rev-parse HEAD) || echo $branch_name } + +# Argument 1: Caller script name, format: "nix-script" +caller_util_all_commands() { + find $(dirname ${BASH_SOURCE[0]}) -type f -name "${1}-*.sh" +} + -- cgit v1.2.3 From 6b5bc28848d56b96d228fb58bd515003c8c61c15 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 6 Jun 2015 11:30:18 +0200 Subject: Add util for listing all subcommands for a caller --- nix-utils.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nix-utils.sh b/nix-utils.sh index c5dd151..ba5720d 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -137,3 +137,11 @@ caller_util_all_commands() { find $(dirname ${BASH_SOURCE[0]}) -type f -name "${1}-*.sh" } +# Argument 1: Caller script name, format: "nix-script" +caller_util_list_subcommands_for() { + for cmd in $(caller_util_all_commands $1) + do + scriptname_to_command "$cmd" "$1" + done +} + -- cgit v1.2.3 From ba0adf8f188941ca2a291e961cb59069358005f8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 6 Jun 2015 11:31:09 +0200 Subject: Add util for generating a script name for a caller and a command --- nix-utils.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nix-utils.sh b/nix-utils.sh index ba5720d..9d4a3d6 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -145,3 +145,9 @@ caller_util_list_subcommands_for() { done } +# Argument 1: Caller script name +# Argzment 2: Command name +caller_util_script_for() { + echo "$(dirname ${BASH_SOURCE[0]})/${1}-${2}.sh" +} + -- cgit v1.2.3 From ce2aaf51b3fa94cd270cf779d76428307e16e649 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 6 Jun 2015 11:31:59 +0200 Subject: Add util for getting the complete script name for a caller and a command, including sanity checks --- nix-utils.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nix-utils.sh b/nix-utils.sh index 9d4a3d6..9e0a070 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -151,3 +151,13 @@ caller_util_script_for() { echo "$(dirname ${BASH_SOURCE[0]})/${1}-${2}.sh" } +# Argument 1: Caller script name +# Argzment 2: Command name +caller_util_get_script() { + local SCRIPT=$(caller_util_script_for $1 $2) + + [[ ! -f $SCRIPT ]] && stderr "Not available: $COMMAND -> $SCRIPT" && exit 1 + [[ ! -x $SCRIPT ]] && stderr "Not executeable: $SCRIPT" && exit 1 + + echo "$SCRIPT" +} -- cgit v1.2.3 From f68ad54e89fb5f210982f87f958a6b10195f07da Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 6 Jun 2015 11:33:08 +0200 Subject: Add nix-script channel command, which is a caller itself --- nix-script-channel.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 nix-script-channel.sh diff --git a/nix-script-channel.sh b/nix-script-channel.sh new file mode 100755 index 0000000..6a24eca --- /dev/null +++ b/nix-script-channel.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# Level-2 wrapper to be able to +# +# nix-script channel + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +export VERBOSE + +usage() { + cat <&2 + $(help_synopsis "${BASH_SOURCE[0]}" "[-h] [-l] ") + + -l List all available commands + -h Show this help and exit + +$(help_end "channel") +EOS +} + +while getopts "hl" OPTION +do + case $OPTION in + h) + usage + exit 0 + ;; + + l) + caller_util_list_subcommands_for "nix-script-channel" + exit 0 + ;; + *) + ;; + esac +done + +if [[ -z "$1" ]] +then + # no command available + stderr "No command given" + usage + exit 1 +fi + +SCRIPT=$(caller_util_get_script "nix-script-channel" "$1") +[[ -z "$SCRIPT" ]] && exit 1 +stdout "SCRIPT = $SCRIPT" + +stdout "Parsing args for '$1'" +SCRIPT_ARGS=$(echo $* | sed -r "s/(.*)$1(.*)/\2/") + +stdout "Calling: '$SCRIPT $SCRIPT_ARGS'" +RC_CONFIG=$RC_CONFIG RC_NIXPKGS=$RC_NIXPKGS exec bash $SCRIPT $SCRIPT_ARGS -- cgit v1.2.3 From a82ae9096fefc8bed71c002b8e99a930e2bc7fb4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 28 May 2015 21:16:55 +0200 Subject: Add tool to list channel generations --- nix-script-channel-list-generations.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 nix-script-channel-list-generations.sh diff --git a/nix-script-channel-list-generations.sh b/nix-script-channel-list-generations.sh new file mode 100755 index 0000000..6ab5408 --- /dev/null +++ b/nix-script-channel-list-generations.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +Color_Off='\e[0m' +Red='\e[0;31m' + +usage() { + cat <&2 + $(help_synopsis "${BASH_SOURCE[0]}" "[-h]") + + -h | Show this help and exit + +$(help_end) +EOS +} + +while getopts "h" OPTION +do + case $OPTION in + h) + usage + exit 0 + ;; + *) + ;; + esac +done + +stdout "Done with argument parsing" + +explain sudo nix-env -p /nix/var/nix/profiles/per-user/root/channels --list-generations -- cgit v1.2.3 From 9140a5cfe32a2fc57649f9436a6c5c0ee4b176ee Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 28 May 2015 21:21:09 +0200 Subject: Add script for checking out a generation --- nix-script-channel-checkout-generation.sh | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 nix-script-channel-checkout-generation.sh diff --git a/nix-script-channel-checkout-generation.sh b/nix-script-channel-checkout-generation.sh new file mode 100755 index 0000000..044e2d0 --- /dev/null +++ b/nix-script-channel-checkout-generation.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +usage() { + cat <]") + + -g Generation to checkout + -h Show this help and exit + +$(help_end) +EOS +} + +# no generation by now +GEN= + +while getopts "hg:" OPTION +do + case $OPTION in + g) + GEN=$OPTARG + stdout "GEN = $GEN" + ;; + h) + usage + exit 0 + ;; + + *) + ;; + esac +done + +[[ -z "$GEN" ]] && stderr "No generation number passed" && exit 1 + +CHANNELS=/nix/var/nix/profiles/per-user/root/channels + +stdout "Executing checkout. Password cache will be reset afterwards" +explain sudo nix-env -p $CHANNELS --switch-generation $GEN + +stdout "Resetting sudo password" +sudo -k + -- cgit v1.2.3 From bd75f32309dbaed6f4266056f186c34d3d19d8f9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 28 May 2015 21:52:43 +0200 Subject: Add tool for diffing channel generations --- nix-script-channel-diff-generations.sh | 102 +++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 nix-script-channel-diff-generations.sh diff --git a/nix-script-channel-diff-generations.sh b/nix-script-channel-diff-generations.sh new file mode 100755 index 0000000..5e65397 --- /dev/null +++ b/nix-script-channel-diff-generations.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +usage() { + cat <&2 + $(help_synopsis "${BASH_SOURCE[0]}" "[-g ] [-w ] [-n ] [-h]") + + -g | Use this command instead of 'diff --name-only' (currently no support for multi-word args) + -w | Path to working-copy of nixpkgs git repo, default: $RC_NIXPKGS + -n | Generations to show diff in form a..b + -h | Show this help and exit + +$(help_end) +EOS +} + +GEN_A= +GEN_B= +WC=$RC_NIXPKGS +GIT="diff --name-only" + +explain() { + stdout $* + $* +} + +while getopts "g:w:n:h" OPTION +do + case $OPTION in + g) + GIT=$OPTARG + dbg "GIT = $GIT" + ;; + + w) + WC=$OPTARG + dbg "WC = $WC" + ;; + + n) + GEN_A=$(echo $OPTARG | cut -d "." -f 1) + GEN_B=$(echo $OPTARG | cut -d "." -f 3) + + [[ -z "$GEN_A" || -z "$GEN_B" ]] && \ + stderr "Parsing error for '$OPTARG'" && usage && exit 1 + + dbg "GEN_A = $GEN_A" + dbg "GEN_B = $GEN_B" + ;; + + h) + usage + exit 1 + ;; + esac +done + +[[ -z "$GEN_A" || -z "$GEN_B" ]] && \ + stderr "No generation information" && usage && exit 1 + +pathof() { + echo "/nix/var/nix/profiles/per-user/root/channels-$1-link/nixos/nixpkgs/.version-suffix" +} + +version_string() { + local path=$(pathof $1) + local version=$(cat $path) + + [[ -z "$version" ]] && \ + stderr "No commit version information for generation $1" && exit 1 + + echo $version + +} + +commit_for() { + echo $(version_string $1) | cut -d . -f 2 +} + +__git() { + explain git --git-dir="$WC/.git" --work-tree="$WC" $* +} + +A=$(commit_for $GEN_A) +B=$(commit_for $GEN_B) + +stdout "A = $A" +stdout "B = $B" + +[[ "$A" -eq "$B" ]] && \ + echo "Same hash for both generations. There won't be a diff" && exit 1 + +if [[ -z "$WC" ]] +then + echo "$A..$B" +else + __git $GIT "$A..$B" +fi + +stdout "Ready" + -- cgit v1.2.3 From 80bc48e7440c046f11acba98401f73616172bd1a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jun 2015 15:22:00 +0200 Subject: Rewrite scriptname_to_command helper function --- nix-script | 2 +- nix-utils.sh | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/nix-script b/nix-script index 0234ded..1e1b6db 100755 --- a/nix-script +++ b/nix-script @@ -141,7 +141,7 @@ then dbg "Listing commands" for cmd in $(all_commands) do - echo $(scriptname_to_command $cmd) + echo $(scriptname_to_command $cmd "nix-script") done exit 0 fi diff --git a/nix-utils.sh b/nix-utils.sh index 9e0a070..1aa35c5 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -31,8 +31,7 @@ stdout() { # Get the command name from a script path # scriptname_to_command() { - echo "$1" | sed 's,^\.\/nix-script-,,' | sed 's,\.sh$,,' | \ - sed -r "s,$(dirname ${BASH_SOURCE[0]})/nix-script-,," + echo "$1" | sed -r "s,$(dirname ${BASH_SOURCE[0]})/$2-(.*)\.sh$,\1," } # -- cgit v1.2.3 From c825fa1af583ee90f3b94b99d744ab0e248905e4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jun 2015 15:26:19 +0200 Subject: Fix: Sort command list --- nix-utils.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix-utils.sh b/nix-utils.sh index 1aa35c5..b21c4ab 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -141,7 +141,7 @@ caller_util_list_subcommands_for() { for cmd in $(caller_util_all_commands $1) do scriptname_to_command "$cmd" "$1" - done + done | sort } # Argument 1: Caller script name -- cgit v1.2.3 From 2b7289deab6a079db13540b8cb7899b230777e51 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jun 2015 15:26:31 +0200 Subject: Fix: Use caller util in main --- nix-script | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nix-script b/nix-script index 1e1b6db..3860f20 100755 --- a/nix-script +++ b/nix-script @@ -139,10 +139,7 @@ dbg "RC_NIXPKGS = '$RC_NIXPKGS'" if [ $LIST_COMMANDS -eq 1 ] then dbg "Listing commands" - for cmd in $(all_commands) - do - echo $(scriptname_to_command $cmd "nix-script") - done + caller_util_list_subcommands_for "nix-script" exit 0 fi -- cgit v1.2.3 From cccf8001bd93a4b459609c3792dc679e695ed86b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jun 2015 15:41:16 +0200 Subject: Fix: scriptname_to_command should replace callee string --- nix-utils.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nix-utils.sh b/nix-utils.sh index b21c4ab..382763f 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -31,7 +31,8 @@ stdout() { # Get the command name from a script path # scriptname_to_command() { - echo "$1" | sed -r "s,$(dirname ${BASH_SOURCE[0]})/$2-(.*)\.sh$,\1," + callee=$([ -z "$2" ] && echo "nix-script" || echo "$2") + echo "$1" | sed -r "s,$(dirname ${BASH_SOURCE[0]})/$callee-(.*)\.sh$,\1," } # -- cgit v1.2.3 From d0fb9648ce12a47b8490626df449b2bf6aa4ab16 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jun 2015 15:44:05 +0200 Subject: Ugly fix: Hard-code (more or less) help synopsis string --- nix-script-channel-checkout-generation.sh | 2 +- nix-script-channel-diff-generations.sh | 2 +- nix-script-channel-list-generations.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nix-script-channel-checkout-generation.sh b/nix-script-channel-checkout-generation.sh index 044e2d0..ba0db5e 100755 --- a/nix-script-channel-checkout-generation.sh +++ b/nix-script-channel-checkout-generation.sh @@ -4,7 +4,7 @@ source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh usage() { cat <]") + $(help_synopsis "channel" "checkout-generation [-h] [-g ]") -g Generation to checkout -h Show this help and exit diff --git a/nix-script-channel-diff-generations.sh b/nix-script-channel-diff-generations.sh index 5e65397..2e5972a 100755 --- a/nix-script-channel-diff-generations.sh +++ b/nix-script-channel-diff-generations.sh @@ -4,7 +4,7 @@ source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh usage() { cat <&2 - $(help_synopsis "${BASH_SOURCE[0]}" "[-g ] [-w ] [-n ] [-h]") + $(help_synopsis "channel" "diff-generations [-g ] [-w ] [-n ] [-h]") -g | Use this command instead of 'diff --name-only' (currently no support for multi-word args) -w | Path to working-copy of nixpkgs git repo, default: $RC_NIXPKGS diff --git a/nix-script-channel-list-generations.sh b/nix-script-channel-list-generations.sh index 6ab5408..3b4cc87 100755 --- a/nix-script-channel-list-generations.sh +++ b/nix-script-channel-list-generations.sh @@ -7,7 +7,7 @@ Red='\e[0;31m' usage() { cat <&2 - $(help_synopsis "${BASH_SOURCE[0]}" "[-h]") + $(help_synopsis "channel" "list-generations [-h]") -h | Show this help and exit -- cgit v1.2.3