diff --git a/lib/cli.nix b/lib/cli.nix index f47625d2f53..32d24a00ceb 100644 --- a/lib/cli.nix +++ b/lib/cli.nix @@ -6,8 +6,29 @@ rec { This helps protect against malformed command lines and also to reduce boilerplate related to command-line construction for simple use cases. + `toGNUCommandLine` returns a list of nix strings. + `toGNUCommandLineShell` returns an escaped shell string. + Example: - encodeGNUCommandLine + 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 { } { data = builtins.toJSON { id = 0; }; @@ -24,8 +45,9 @@ rec { verbose = true; }; => "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'" + */ - encodeGNUCommandLine = + toGNUCommandLineShell = options: attrs: lib.escapeShellArgs (toGNUCommandLine options attrs); toGNUCommandLine = diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index e47b48b5017..b320839b2ac 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -441,9 +441,34 @@ runTests { expected = "«foo»"; }; - testRenderOptions = { + +# CLI + + testToGNUCommandLine = { expr = - encodeGNUCommandLine + 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; + }; + + expected = [ "-X" "PUT" "--data" "{\"id\":0}" "--retry" "3" "--url" "https://example.com/foo" "--url" "https://example.com/bar" "--verbose" ]; + }; + + testToGNUCommandLineShell = { + expr = + cli.toGNUCommandLineShell { } { data = builtins.toJSON { id = 0; };