lib/cli,lib/tests/misc: somewhat more standard formatting

This commit is contained in:
Profpatsch 2020-01-22 23:37:10 +01:00
parent 6841f408cc
commit b2654c226a
2 changed files with 61 additions and 76 deletions

View file

@ -10,49 +10,44 @@ rec {
`toGNUCommandLineShell` returns an escaped shell string. `toGNUCommandLineShell` returns an escaped shell string.
Example: Example:
toGNUCommandLine cli.toGNUCommandLine {} {
{ } data = builtins.toJSON { id = 0; };
{ data = builtins.toJSON { id = 0; }; X = "PUT";
retry = 3;
X = "PUT"; retry-delay = null;
url = [ "https://example.com/foo" "https://example.com/bar" ];
retry = 3; silent = false;
verbose = true;
retry-delay = null; }
=> [
url = [ "https://example.com/foo" "https://example.com/bar" ]; "-X" "PUT"
"--data" "{\"id\":0}"
silent = false; "--retry" "3"
"--url" "https://example.com/foo"
verbose = true; "--url" "https://example.com/bar"
}; "--verbose"
=> [ "-X" "PUT" "--data" "{\"id\":0}" "--retry" "3" "--url" "https://example.com/foo" "--url" "https://example.com/bar" "--verbose" ] ]
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'"
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'";
*/ */
toGNUCommandLineShell = toGNUCommandLineShell =
options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs); options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs);
toGNUCommandLine = toGNUCommandLine =
{ renderKey ? { renderKey ?
key: if builtins.stringLength key == 1 then "-${key}" else "--${key}" key:
if builtins.stringLength key == 1
then "-${key}"
else "--${key}"
, renderOption ? , renderOption ?
key: value: key: value:
@ -66,11 +61,10 @@ rec {
}: }:
options: options:
let let
render = key: value: render =
if builtins.isBool value key: value:
then renderBool key value if builtins.isBool value then renderBool key value
else if builtins.isList value else if builtins.isList value then renderList key value
then renderList key value
else renderOption key value; else renderOption key value;
in in

View file

@ -445,45 +445,36 @@ runTests {
# CLI # CLI
testToGNUCommandLine = { testToGNUCommandLine = {
expr = expr = cli.toGNUCommandLine {} {
cli.toGNUCommandLine data = builtins.toJSON { id = 0; };
{ } X = "PUT";
{ data = builtins.toJSON { id = 0; }; retry = 3;
retry-delay = null;
url = [ "https://example.com/foo" "https://example.com/bar" ];
silent = false;
verbose = true;
};
X = "PUT"; expected = [
"-X" "PUT"
retry = 3; "--data" "{\"id\":0}"
"--retry" "3"
retry-delay = null; "--url" "https://example.com/foo"
"--url" "https://example.com/bar"
url = [ "https://example.com/foo" "https://example.com/bar" ]; "--verbose"
];
silent = false;
verbose = true;
};
expected = [ "-X" "PUT" "--data" "{\"id\":0}" "--retry" "3" "--url" "https://example.com/foo" "--url" "https://example.com/bar" "--verbose" ];
}; };
testToGNUCommandLineShell = { testToGNUCommandLineShell = {
expr = expr = cli.toGNUCommandLineShell {} {
cli.toGNUCommandLineShell data = builtins.toJSON { id = 0; };
{ } X = "PUT";
{ data = builtins.toJSON { id = 0; }; retry = 3;
retry-delay = null;
X = "PUT"; url = [ "https://example.com/foo" "https://example.com/bar" ];
silent = false;
retry = 3; verbose = true;
};
retry-delay = null;
url = [ "https://example.com/foo" "https://example.com/bar" ];
silent = false;
verbose = true;
};
expected = "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'"; expected = "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
}; };