2019-12-12 00:30:05 +00:00
|
|
|
{ lib }:
|
|
|
|
|
|
|
|
{ /* Automatically convert an attribute set to command-line options.
|
|
|
|
|
|
|
|
This helps protect against malformed command lines and also to reduce
|
|
|
|
boilerplate related to command-line construction for simple use cases.
|
|
|
|
|
|
|
|
Example:
|
2019-12-15 16:21:41 +00:00
|
|
|
encodeGNUCommandLine
|
|
|
|
{ }
|
|
|
|
{ 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 00:30:05 +00:00
|
|
|
*/
|
2019-12-14 02:19:24 +00:00
|
|
|
encodeGNUCommandLine =
|
2019-12-14 02:25:52 +00:00
|
|
|
{ renderKey ?
|
|
|
|
key: if builtins.stringLength key == 1 then "-${key}" else "--${key}"
|
2019-12-12 00:30:05 +00:00
|
|
|
|
2019-12-14 02:25:52 +00:00
|
|
|
, renderOption ?
|
|
|
|
key: value:
|
|
|
|
if value == null
|
|
|
|
then ""
|
|
|
|
else " ${renderKey key} ${lib.escapeShellArg value}"
|
2019-12-12 00:30:05 +00:00
|
|
|
|
2019-12-14 02:25:52 +00:00
|
|
|
, renderBool ? key: value: if value then " ${renderKey key}" else ""
|
2019-12-12 00:30:05 +00:00
|
|
|
|
2019-12-15 16:21:41 +00:00
|
|
|
, renderList ? key: value: lib.concatMapStrings (renderOption key) value
|
2019-12-14 02:25:52 +00:00
|
|
|
}:
|
|
|
|
options:
|
|
|
|
let
|
|
|
|
render = key: value:
|
2019-12-12 00:30:05 +00:00
|
|
|
if builtins.isBool value
|
2019-12-14 02:25:52 +00:00
|
|
|
then renderBool key value
|
2019-12-12 00:30:05 +00:00
|
|
|
else if builtins.isList value
|
2019-12-14 02:25:52 +00:00
|
|
|
then renderList key value
|
|
|
|
else renderOption key value;
|
2019-12-12 00:30:05 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
lib.concatStrings (lib.mapAttrsToList render options);
|
|
|
|
}
|