cue-lang / docs-and-content

A place to discuss, plan, and track documentation on cuelang.org
5 stars 1 forks source link

docs/howto/import-multiple-yaml-files-as-cue: Importing multiple YAML files as CUE #111

Open jpluscplusm opened 3 months ago

jpluscplusm commented 3 months ago

Slack thread: https://cuelang.slack.com/archives/C012UU8B72M/p1709051362031909

Question:

I have a dir with subdirs that each contain a metadata.yaml. How can I use cue import to merge all of them into a single cue file? cue import **/metadata.yaml -p sc -l 'strings.ToLower(type)' produces one cue file per yaml file.

Solution:

exec cue export -l 'strings.ToLower(type)' -o res.cue a/metadata.yml b/metadata.yml c/metadata.yml
cmp res.cue res.cue.golden
-- a/metadata.yml --
type: a
value: 5
-- b/metadata.yml --
type: b
value: 4
-- c/metadata.yml --
type: c
value: 10
-- res.cue.golden --
a: {
    type:  "a"
    value: 5
}
b: {
    type:  "b"
    value: 4
}
c: {
    type:  "c"
    value: 10
}

If we expose this as a code block, then we'd need the cue export to produce CUE on stdout, which negates /some/ of the point of the solution. We probably need to use a script block which performs the export then a cat, to properly demonstrate what's going on.

Take advantage of https://github.com/cue-lang/cue/commit/93acaa356a707751467b9422c8288c9759a25560 to place the output CUE in a package.