2019-12-12 01:30:05 +01:00
|
|
|
{ lib }:
|
|
|
|
|
2020-01-05 22:03:00 +01:00
|
|
|
rec {
|
|
|
|
/* Automatically convert an attribute set to command-line options.
|
2019-12-12 01:30:05 +01:00
|
|
|
|
|
|
|
This helps protect against malformed command lines and also to reduce
|
|
|
|
boilerplate related to command-line construction for simple use cases.
|
|
|
|
|
2020-01-22 23:24:06 +01:00
|
|
|
`toGNUCommandLine` returns a list of nix strings.
|
|
|
|
`toGNUCommandLineShell` returns an escaped shell string.
|
|
|
|
|
2019-12-12 01:30:05 +01:00
|
|
|
Example:
|
2020-01-22 23:37:10 +01:00
|
|
|
cli.toGNUCommandLine {} {
|
|
|
|
data = builtins.toJSON { id = 0; };
|
|
|
|
X = "PUT";
|
|
|
|
retry = 3;
|
|
|
|
retry-delay = null;
|
|
|
|
url = [ "https://example.com/foo" "https://example.com/bar" ];
|
|
|
|
silent = false;
|
|
|
|
verbose = true;
|
|
|
|
}
|
|
|
|
=> [
|
|
|
|
"-X" "PUT"
|
|
|
|
"--data" "{\"id\":0}"
|
|
|
|
"--retry" "3"
|
|
|
|
"--url" "https://example.com/foo"
|
|
|
|
"--url" "https://example.com/bar"
|
|
|
|
"--verbose"
|
|
|
|
]
|
|
|
|
|
|
|
|
cli.toGNUCommandLineShell {} {
|
|
|
|
data = builtins.toJSON { id = 0; };
|
|
|
|
X = "PUT";
|
|
|
|
retry = 3;
|
|
|
|
retry-delay = null;
|
|
|
|
url = [ "https://example.com/foo" "https://example.com/bar" ];
|
|
|
|
silent = false;
|
|
|
|
verbose = true;
|
|
|
|
}
|
|
|
|
=> "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
|
2019-12-12 01:30:05 +01:00
|
|
|
*/
|
2020-01-22 23:24:06 +01:00
|
|
|
toGNUCommandLineShell =
|
2020-01-05 22:03:00 +01:00
|
|
|
options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
|
|
|
|
|
|
|
|
toGNUCommandLine =
|
2019-12-14 03:25:52 +01:00
|
|
|
{ renderKey ?
|
2020-01-22 23:37:10 +01:00
|
|
|
key:
|
|
|
|
if builtins.stringLength key == 1
|
|
|
|
then "-${key}"
|
|
|
|
else "--${key}"
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2019-12-14 03:25:52 +01:00
|
|
|
, renderOption ?
|
|
|
|
key: value:
|
|
|
|
if value == null
|
2020-01-05 22:03:00 +01:00
|
|
|
then []
|
|
|
|
else [ (renderKey key) (builtins.toString value) ]
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2020-01-05 22:03:00 +01:00
|
|
|
, renderBool ? key: value: lib.optional value (renderKey key)
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2020-01-05 22:03:00 +01:00
|
|
|
, renderList ? key: value: lib.concatMap (renderOption key) value
|
2019-12-14 03:25:52 +01:00
|
|
|
}:
|
|
|
|
options:
|
|
|
|
let
|
2020-01-22 23:37:10 +01:00
|
|
|
render =
|
|
|
|
key: value:
|
|
|
|
if builtins.isBool value then renderBool key value
|
|
|
|
else if builtins.isList value then renderList key value
|
2019-12-14 03:25:52 +01:00
|
|
|
else renderOption key value;
|
2019-12-12 01:30:05 +01:00
|
|
|
|
|
|
|
in
|
2020-01-05 22:03:00 +01:00
|
|
|
builtins.concatLists (lib.mapAttrsToList render options);
|
2019-12-12 01:30:05 +01:00
|
|
|
}
|