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 =
|
2020-01-22 23:55:42 +01:00
|
|
|
{ mkKey ?
|
|
|
|
k: if builtins.stringLength k == 1
|
|
|
|
then "-${k}"
|
|
|
|
else "--${k}"
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2020-01-22 23:55:42 +01:00
|
|
|
, mkOption ?
|
|
|
|
k: v: if v == null
|
|
|
|
then []
|
|
|
|
else [ (mkKey k) (builtins.toString v) ]
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2020-01-22 23:55:42 +01:00
|
|
|
, mkBool ? k: v: lib.optional v (mkKey k)
|
2019-12-12 01:30:05 +01:00
|
|
|
|
2020-01-22 23:55:42 +01:00
|
|
|
, mkList ? k: v: lib.concatMap (mkOption k) v
|
2019-12-14 03:25:52 +01:00
|
|
|
}:
|
|
|
|
options:
|
|
|
|
let
|
2020-01-22 23:55:42 +01:00
|
|
|
render = k: v:
|
|
|
|
if builtins.isBool v then mkBool k v
|
|
|
|
else if builtins.isList v then mkList k v
|
|
|
|
else mkOption k v;
|
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
|
|
|
}
|