2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-11-27 17:43:27 +00:00
bazel-lib/docs/yq.md
Alex Eagle 0f5e1dcafd
chore(deps): upgrade stardoc (#894)
* chore(deps): upgrade stardoc

This uses the Bazel 7 'starlark_doc_extract' rule which our docsite expects for slurping data.

* chore: stardoc setup in WORKSPACE too

* chore: skip stardoc on bazel 6 in cases where the legacy extractor produces different docstrings
2024-08-08 12:56:11 -07:00

3.7 KiB
Generated

Wrapper rule around the yq tool

From the documentation at https://mikefarah.gitbook.io/yq:

yq is a a lightweight and portable command-line YAML processor. yq uses jq like syntax but works with yaml files as well as json.

Usage examples

load("@aspect_bazel_lib//lib:yq.bzl", "yq")
# Remove fields
yq(
    name = "safe-config",
    srcs = ["config.yaml"],
    expression = "del(.credentials)",
)
# Merge two yaml documents
yq(
    name = "ab",
    srcs = [
        "a.yaml",
        "b.yaml",
    ],
    expression = ". as $item ireduce ({}; . * $item )",
)
# Split a yaml file into several files
yq(
    name = "split",
    srcs = ["multidoc.yaml"],
    outs = [
        "first.yml",
        "second.yml",
    ],
    args = [
        "-s '.a'",  # Split expression
        "--no-doc", # Exclude document separator --
    ],
)
# Convert a yaml file to json
yq(
    name = "convert-to-json",
    srcs = ["foo.yaml"],
    args = ["-o=json"],
    outs = ["foo.json"],
)
# Convert a json file to yaml
yq(
    name = "convert-to-yaml",
    srcs = ["bar.json"],
    args = ["-P"],
    outs = ["bar.yaml"],
)
# Call yq in a genrule
genrule(
    name = "generate",
    srcs = ["farm.yaml"],
    outs = ["genrule_output.yaml"],
    cmd = "$(YQ_BIN) '.moo = "cow"' $(location farm.yaml) > $@",
    toolchains = ["@yq_toolchains//:resolved_toolchain"],
)
# With --stamp, causes properties to be replaced by version control info.
yq(
    name = "stamped",
    srcs = ["package.yaml"],
    expression = "|".join([
        "load(strenv(STAMP)) as $stamp",
        # Provide a default using the "alternative operator" in case $stamp is empty dict.
        ".version = ($stamp.BUILD_EMBED_LABEL // "<unstamped>")",
    ]),
)

yq

yq(name, srcs, expression, args, outs, kwargs)

Invoke yq with an expression on a set of input files.

yq is capable of parsing and outputting to other formats. See their docs for more examples.

PARAMETERS

Name Description Default Value
name Name of the rule none
srcs List of input file labels none
expression yq expression (https://mikefarah.gitbook.io/yq/commands/evaluate).

Defaults to the identity expression ".". Subject to stamp variable replacements, see Stamping. When stamping is enabled, an environment variable named "STAMP" will be available in the expression.

Be careful to write the filter so that it handles unstamped builds, as in the example above.
"."
args Additional args to pass to yq.

Note that you do not need to pass eval or eval-all as this is handled automatically based on the number srcs. Passing the output format or the parse format is optional as these can be guessed based on the file extensions in srcs and outs.
[]
outs Name of the output files.

Defaults to a single output with the name plus a ".yaml" extension, or the extension corresponding to a passed output argument (e.g., "-o=json"). For split operations you must declare all outputs as the name of the output files depends on the expression.
None
kwargs Other common named parameters such as tags or visibility none