diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2015-09-07 16:12:34 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2015-09-07 16:12:34 +0200 |
commit | e143ae471f32275a0ebbce996b1e481163f5fe33 (patch) | |
tree | 64b635e426abf0487eea754b72d3b6f6c55a3b17 | |
parent | e09781c6b551689dd569f8bd473210e32cdbe6e9 (diff) | |
parent | d0fb9648ce12a47b8490626df449b2bf6aa4ab16 (diff) |
Merge pull request #17 from matthiasbeyer/add-channel-tools
Add channel tools
-rwxr-xr-x | nix-script | 5 | ||||
-rwxr-xr-x | nix-script-channel-checkout-generation.sh | 45 | ||||
-rwxr-xr-x | nix-script-channel-diff-generations.sh | 102 | ||||
-rwxr-xr-x | nix-script-channel-list-generations.sh | 32 | ||||
-rwxr-xr-x | nix-script-channel.sh | 55 | ||||
-rw-r--r-- | nix-utils.sh | 34 |
6 files changed, 267 insertions, 6 deletions
@@ -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) - done + caller_util_list_subcommands_for "nix-script" exit 0 fi diff --git a/nix-script-channel-checkout-generation.sh b/nix-script-channel-checkout-generation.sh new file mode 100755 index 0000000..ba0db5e --- /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 <<EOS + $(help_synopsis "channel" "checkout-generation [-h] [-g <n>]") + + -g <n> 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 + diff --git a/nix-script-channel-diff-generations.sh b/nix-script-channel-diff-generations.sh new file mode 100755 index 0000000..2e5972a --- /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 <<EOS >&2 + $(help_synopsis "channel" "diff-generations [-g <command>] [-w <path>] [-n <generations>] [-h]") + + -g <command> | Use this command instead of 'diff --name-only' (currently no support for multi-word args) + -w <path> | Path to working-copy of nixpkgs git repo, default: $RC_NIXPKGS + -n <generations> | 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" + diff --git a/nix-script-channel-list-generations.sh b/nix-script-channel-list-generations.sh new file mode 100755 index 0000000..3b4cc87 --- /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 <<EOS >&2 + $(help_synopsis "channel" "list-generations [-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 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 <command> + +source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh + +export VERBOSE + +usage() { + cat <<EOS >&2 + $(help_synopsis "${BASH_SOURCE[0]}" "[-h] [-l] <command>") + + -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 diff --git a/nix-utils.sh b/nix-utils.sh index 69dbf12..382763f 100644 --- a/nix-utils.sh +++ b/nix-utils.sh @@ -31,8 +31,8 @@ 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-,," + callee=$([ -z "$2" ] && echo "nix-script" || echo "$2") + echo "$1" | sed -r "s,$(dirname ${BASH_SOURCE[0]})/$callee-(.*)\.sh$,\1," } # @@ -131,3 +131,33 @@ __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" +} + +# 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 | sort +} + +# Argument 1: Caller script name +# Argzment 2: Command name +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" +} |