]> git.ipfire.org Git - thirdparty/curl.git/commit
tool: add "variable" support
authorDaniel Stenberg <daniel@haxx.se>
Mon, 31 Jul 2023 09:50:28 +0000 (11:50 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 31 Jul 2023 09:51:34 +0000 (11:51 +0200)
commit2e160c9c652504e147f474ed920ae891481e299c
tree6b7e1441a27e7b1120bbbc9aac5c543e6df10f9d
parent47a3e6e577b019b8dfce8d3f8df764a8dd427fd2
tool: add "variable" support

Add support for command line variables. Set variables with --variable
name=content or --variable name@file (where "file" can be stdin if set
to a single dash (-)).

Variable content is expanded in option parameters using "{{name}}"
(without the quotes) if the option name is prefixed with
"--expand-". This gets the contents of the variable "name" inserted, or
a blank if the name does not exist as a variable. Insert "{{" verbatim
in the string by prefixing it with a backslash, like "\\{{".

Import an environment variable with --variable %name. It makes curl exit
with an error if the environment variable is not set. It can also rather
get a default value if the variable does not exist, using =content or
@file like shown above.

Example: get the USER environment variable into the URL:

 --variable %USER
 --expand-url = "https://example.com/api/{{USER}}/method"

When expanding variables, curl supports a set of functions that can make
the variable contents more convenient to use. It can trim leading and
trailing white space with "trim", output the contents as a JSON quoted
string with "json", URL encode it with "url" and base 64 encode it with
"b64". To apply functions to a variable expansion, add them colon
separated to the right side of the variable. They are then performed in
a left to right order.

Example: get the contents of a file called $HOME/.secret into a variable
called "fix". Make sure that the content is trimmed and percent-encoded
sent as POST data:

  --variable %HOME=/home/default
  --expand-variable fix@{{HOME}}/.secret
  --expand-data "{{fix:trim:url}}"
  https://example.com/

Documented. Many new test cases.

Co-brainstormed-by: Emanuele Torre
Assisted-by: Jat Satiro
Closes #11346
32 files changed:
docs/cmdline-opts/Makefile.inc
docs/cmdline-opts/config.d
docs/cmdline-opts/page-header
docs/cmdline-opts/variable.d [new file with mode: 0644]
docs/options-in-versions
lib/base64.c
lib/curl_base64.h
projects/generate.bat
src/CMakeLists.txt
src/Makefile.inc
src/tool_cfgable.h
src/tool_getparam.c
src/tool_getparam.h
src/tool_helpers.c
src/tool_listhelp.c
src/tool_operate.c
src/tool_writeout_json.c
src/tool_writeout_json.h
src/var.c [new file with mode: 0644]
src/var.h [new file with mode: 0644]
tests/data/Makefile.inc
tests/data/test428 [new file with mode: 0644]
tests/data/test429 [new file with mode: 0644]
tests/data/test448 [new file with mode: 0644]
tests/data/test449 [new file with mode: 0644]
tests/data/test450 [new file with mode: 0644]
tests/data/test451 [new file with mode: 0644]
tests/data/test452 [new file with mode: 0644]
tests/data/test453 [new file with mode: 0644]
tests/data/test454 [new file with mode: 0644]
tests/data/test455 [new file with mode: 0644]
winbuild/MakefileBuild.vc