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