mirror of
https://github.com/NiklasGollenstede/nixos-installer.git
synced 2024-11-25 01:14:13 +01:00
(re-)move generic-arg-*, more concise default.nix
This commit is contained in:
parent
9e02cd5952
commit
6f44532be5
BIN
flake.lock
BIN
flake.lock
Binary file not shown.
@ -1,4 +1,3 @@
|
|||||||
dirname: inputs@{ nixpkgs, functions, ...}: let
|
dirname: inputs: inputs.functions.lib.importLib inputs dirname { rename = {
|
||||||
categories = functions.lib.importAll inputs dirname;
|
functions = "fun";
|
||||||
self = (builtins.foldl' (a: b: a // (if builtins.isAttrs b then b else { })) { } (builtins.attrValues (builtins.removeAttrs categories [ "setup-scripts" ]))) // categories;
|
}; noSpread = [ "setup-scripts" ]; }
|
||||||
in self // { __internal__ = nixpkgs.lib // { self = self; fun = functions.lib; }; }
|
|
||||||
|
@ -235,7 +235,7 @@ in rec {
|
|||||||
# provide installer tools (not necessarily for system.pkgs.config.hostPlatform)
|
# provide installer tools (not necessarily for system.pkgs.config.hostPlatform)
|
||||||
hostPath=$PATH ; PATH=${lib.makeBinPath tools}
|
hostPath=$PATH ; PATH=${lib.makeBinPath tools}
|
||||||
|
|
||||||
${extractBashFunction (builtins.readFile setup-scripts.utils) "generic-arg-parse"}
|
source ${inputs.functions.lib.bash.generic-arg-parse}
|
||||||
set -o pipefail -o nounset # (do not rely on errexit)
|
set -o pipefail -o nounset # (do not rely on errexit)
|
||||||
generic-arg-parse "$@" || exit
|
generic-arg-parse "$@" || exit
|
||||||
|
|
||||||
@ -257,6 +257,9 @@ in rec {
|
|||||||
declare-flag '*' trace "" "Turn on bash's »errtrace« option before running »COMMAND«."
|
declare-flag '*' trace "" "Turn on bash's »errtrace« option before running »COMMAND«."
|
||||||
declare-flag '*' quiet "" "Try to suppress all non-error output. May also swallow some error related output."
|
declare-flag '*' quiet "" "Try to suppress all non-error output. May also swallow some error related output."
|
||||||
declare -g -A allowedCommands=( ) ; function declare-command { allowedCommands[$@]=$(< /dev/stdin) ; }
|
declare -g -A allowedCommands=( ) ; function declare-command { allowedCommands[$@]=$(< /dev/stdin) ; }
|
||||||
|
source ${inputs.functions.lib.bash.generic-arg-verify}
|
||||||
|
source ${inputs.functions.lib.bash.generic-arg-help}
|
||||||
|
source ${inputs.functions.lib.bash.prepend_trap}
|
||||||
${system.config.${installer}.build.scripts { native = pkgs; }}
|
${system.config.${installer}.build.scripts { native = pkgs; }}
|
||||||
if [[ ''${args[help]:-} ]] ; then (
|
if [[ ''${args[help]:-} ]] ; then (
|
||||||
functionDoc= ; while IFS= read -u3 -r name ; do
|
functionDoc= ; while IFS= read -u3 -r name ; do
|
||||||
|
@ -3,72 +3,6 @@
|
|||||||
# Utilities
|
# Utilities
|
||||||
##
|
##
|
||||||
|
|
||||||
## Performs a simple and generic parsing of CLI arguments. Creates a global associative array »args« and a global normal array »argv«.
|
|
||||||
# Named options may be passed as »--name[=value]«, where »value« defaults to »1«, and are assigned to »args«.
|
|
||||||
# Everything else, or everything following the »--« argument, ends up as positional arguments in »argv«.
|
|
||||||
# Checking the validity of the parsed arguments is up to the caller.
|
|
||||||
function generic-arg-parse { # ...
|
|
||||||
declare -g -A args=( ) ; declare -g -a argv=( ) # this ends up in the caller's scope
|
|
||||||
while (( "$#" )) ; do
|
|
||||||
if [[ $1 == -- ]] ; then shift ; argv+=( "$@" ) ; \return 0 ; fi
|
|
||||||
if [[ $1 == --* ]] ; then
|
|
||||||
if [[ $1 == *=* ]] ; then
|
|
||||||
local key=${1/=*/} ; args[${key/--/}]=${1/$key=/}
|
|
||||||
else args[${1/--/}]=1 ; fi
|
|
||||||
else argv+=( "$1" ) ; fi
|
|
||||||
shift ; done
|
|
||||||
}
|
|
||||||
|
|
||||||
## Shows the help text for a program and exits, if »--help« was passed as argument and parsed, or does nothing otherwise.
|
|
||||||
# Expects to be called between parsing and verifying the arguments.
|
|
||||||
# Uses »allowedArgs« for the list of the named arguments (the values are the descriptions).
|
|
||||||
# »name« should be the program name/path (usually »$0«), »args« the form/names of any positional arguments expected (e.g. »SOURCE... DEST«) and is included in the "Usage" description,
|
|
||||||
# »description« the introductory text shown before the "Usage", and »suffix« any text printed after the argument list.
|
|
||||||
function generic-arg-help { # 1: name, 2?: args, 3?: description, 4?: suffix, 5?: usageLine
|
|
||||||
if [[ ! ${args[help]:-} ]] ; then : ${allowedArgs[help]:=1} ; \return 0 ; fi
|
|
||||||
[[ ! ${3:-} ]] || echo "$3"
|
|
||||||
printf "${5:-'Usage:\n %s [FLAG[=value]]... [--] %s\n\nWhere »FLAG« may be any of:\n'}" "$1" "${2:-}"
|
|
||||||
local name ; while IFS= read -u3 -r name ; do
|
|
||||||
printf ' %s\n %s\n' "$name" "${allowedArgs[$name]//$'\n'/$'\n '}"
|
|
||||||
done 3< <( printf '%s\n' "${!allowedArgs[@]}" | LC_ALL=C sort )
|
|
||||||
printf ' %s\n %s\n' "--help" "Do nothing but print this message and exit with success."
|
|
||||||
[[ ! ${4:-} ]] || echo "$4"
|
|
||||||
\exit 0
|
|
||||||
}
|
|
||||||
|
|
||||||
## Performs a basic verification of the named arguments passed by the user and parsed by »generic-arg-parse« against the names in »allowedArgs«.
|
|
||||||
# Entries in »allowedArgs« should have the form »[--name]="description"« for boolean flags, and »[--name=VAL]="description"« for string arguments.
|
|
||||||
# »description« is used by »generic-arg-help«. Boolean flags may only have the values »1« (as set by »generic-ags-parse« for flags without value) or be empty.
|
|
||||||
# »VAL« is purely nominal. Any argument passed that is not in »allowedArgs« raises an error.
|
|
||||||
function generic-arg-verify { # 1?: exitCode
|
|
||||||
local exitCode=${exitCode:-${1:-1}}
|
|
||||||
local names=' '"${!allowedArgs[@]}"
|
|
||||||
for name in "${!args[@]}" ; do
|
|
||||||
if [[ ${allowedArgs[--$name]:-} ]] ; then
|
|
||||||
if [[ ${args[$name]} == '' || ${args[$name]} == 1 ]] ; then continue ; fi
|
|
||||||
echo "Argument »--$name« should be a boolean, but its value is: ${args[$name]}" 1>&2 ; \return $exitCode
|
|
||||||
fi
|
|
||||||
if [[ $names == *' --'"$name"'='* || $names == *' --'"$name"'[='* ]] ; then continue ; fi
|
|
||||||
if [[ ${undeclared:-} && $name =~ $undeclared ]] ; then continue ; fi
|
|
||||||
echo "Unexpected argument »--$name«.${allowedArgs[help]:+ Call with »--help« for a list of valid arguments.}" 1>&2 ; \return $exitCode
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
## Prepends a command to a trap. Especially useful fo define »finally« commands via »prepend_trap '<command>' EXIT«.
|
|
||||||
# NOTE: When calling this in a sub-shell whose parents already has traps installed, make sure to do »trap - trapName« first. On a new shell, this should be a no-op, but without it, the parent shell's traps will be added to the sub-shell as well (due to strange behavior of »trap -p« (in bash ~5.1.8)).
|
|
||||||
function prepend_trap { # 1: command, ...: trapNames
|
|
||||||
fatal() { printf "ERROR: $@\n" 1>&2 ; \return 1 ; }
|
|
||||||
local cmd=$1 ; shift 1 || fatal "${FUNCNAME} usage error"
|
|
||||||
local name ; for name in "$@" ; do
|
|
||||||
trap -- "$( set +x
|
|
||||||
printf '%s\n' "( ${cmd} ) || true ; "
|
|
||||||
p3() { printf '%s\n' "${3:-}" ; }
|
|
||||||
eval "p3 $(trap -p "${name}")"
|
|
||||||
)" "${name}" || fatal "unable to add to trap ${name}"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
declare -f -t prepend_trap # required to modify DEBUG or RETURN traps
|
|
||||||
|
|
||||||
## Given the name to an existing bash function, this creates a copy of that function with a new name (in the current scope).
|
## Given the name to an existing bash function, this creates a copy of that function with a new name (in the current scope).
|
||||||
function copy-function { # 1: existingName, 2: newName
|
function copy-function { # 1: existingName, 2: newName
|
||||||
local original=$(declare -f "${1?existingName not provided}") ; if [[ ! $original ]] ; then echo "Function $1 is not defined" 1>&2 ; \return 1 ; fi
|
local original=$(declare -f "${1?existingName not provided}") ; if [[ ! $original ]] ; then echo "Function $1 is not defined" 1>&2 ; \return 1 ; fi
|
||||||
|
@ -1 +1 @@
|
|||||||
dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { }
|
dirname: inputs: inputs.functions.lib.importModules inputs dirname { }
|
||||||
|
@ -1 +1 @@
|
|||||||
dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { }
|
dirname: inputs: inputs.functions.lib.importModules inputs dirname { }
|
||||||
|
@ -1 +1 @@
|
|||||||
dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { }
|
dirname: inputs: inputs.functions.lib.importModules inputs dirname { }
|
||||||
|
@ -1 +1 @@
|
|||||||
dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { }
|
dirname: inputs: inputs.functions.lib.importModules inputs dirname { }
|
||||||
|
@ -52,7 +52,7 @@ in let hostModule = {
|
|||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
ln -s ${cfg.system.build.toplevel} $out/system
|
ln -s ${cfg.system.build.toplevel} $out/system
|
||||||
ln -s ${pkgs.writeShellScript name ''
|
ln -s ${pkgs.writeShellScript name ''
|
||||||
${lib.fun.extractBashFunction (builtins.readFile lib.self.setup-scripts.utils) "generic-arg-parse"}
|
source ${lib.fun.bash.generic-arg-parse}
|
||||||
generic-arg-parse "$@" ; set -- ; set -o pipefail -u #; set -x
|
generic-arg-parse "$@" ; set -- ; set -o pipefail -u #; set -x
|
||||||
script=''${argv[0]:?'The first positional argument must be the script to execute in the VM'} ; argv=( "''${argv[@]:1}" )
|
script=''${argv[0]:?'The first positional argument must be the script to execute in the VM'} ; argv=( "''${argv[@]:1}" )
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importOverlays inputs dirname { }
|
dirname: inputs: inputs.functions.lib.importOverlays inputs dirname { }
|
||||||
|
@ -1 +1 @@
|
|||||||
dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importPatches inputs dirname { }
|
dirname: inputs: inputs.functions.lib.importPatches inputs dirname { }
|
||||||
|
Loading…
Reference in New Issue
Block a user