2019-12-11 17:30:05 -07:00
|
|
|
{ lib }:
|
|
|
|
|
2020-01-05 14:03:00 -07:00
|
|
|
rec {
|
|
|
|
/* Automatically convert an attribute set to command-line options.
|
2019-12-11 17:30:05 -07:00
|
|
|
|
|
|
|
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 09:21:41 -07: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;
|
|
|
|
};
|
2020-01-05 14:03:00 -07:00
|
|
|
=> "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'"
|
2019-12-11 17:30:05 -07:00
|
|
|
*/
|
2019-12-13 19:19:24 -07:00
|
|
|
encodeGNUCommandLine =
|
2020-01-05 14:03:00 -07:00
|
|
|
options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
|
|
|
|
|
|
|
|
toGNUCommandLine =
|
2019-12-13 19:25:52 -07:00
|
|
|
{ renderKey ?
|
|
|
|
key: if builtins.stringLength key == 1 then "-${key}" else "--${key}"
|
2019-12-11 17:30:05 -07:00
|
|
|
|
2019-12-13 19:25:52 -07:00
|
|
|
, renderOption ?
|
|
|
|
key: value:
|
|
|
|
if value == null
|
2020-01-05 14:03:00 -07:00
|
|
|
then []
|
|
|
|
else [ (renderKey key) (builtins.toString value) ]
|
2019-12-11 17:30:05 -07:00
|
|
|
|
2020-01-05 14:03:00 -07:00
|
|
|
, renderBool ? key: value: lib.optional value (renderKey key)
|
2019-12-11 17:30:05 -07:00
|
|
|
|
2020-01-05 14:03:00 -07:00
|
|
|
, renderList ? key: value: lib.concatMap (renderOption key) value
|
2019-12-13 19:25:52 -07:00
|
|
|
}:
|
|
|
|
options:
|
|
|
|
let
|
|
|
|
render = key: value:
|
2019-12-11 17:30:05 -07:00
|
|
|
if builtins.isBool value
|
2019-12-13 19:25:52 -07:00
|
|
|
then renderBool key value
|
2019-12-11 17:30:05 -07:00
|
|
|
else if builtins.isList value
|
2019-12-13 19:25:52 -07:00
|
|
|
then renderList key value
|
|
|
|
else renderOption key value;
|
2019-12-11 17:30:05 -07:00
|
|
|
|
|
|
|
in
|
2020-01-05 14:03:00 -07:00
|
|
|
builtins.concatLists (lib.mapAttrsToList render options);
|
2019-12-11 17:30:05 -07:00
|
|
|
}
|