cue-lang / docs-and-content

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

docs/{howto,concept}: Progressively completing a configuration with dependent CUE workflow tasks #123

Open jpluscplusm opened 2 months ago

jpluscplusm commented 2 months ago

In https://cuelang.slack.com/archives/C012UU8B72M/p1710782813626889 a user asked:

I'm trying to write a cue cmd that uses the output of a previous task as input to a cue evaluation

//-- main.cue
package tenants

#Inputs: {
  gitSha: string
}

inputs: #Inputs

outputObject: {
  something: "hello"
  another:   "world"
  sha:       inputs.gitSha
}

and

//-- dump_tool.cue
package tenants

import (
  "encoding/yaml"
  "tool/cli"
  "tool/exec"
)

inputs: gitSha: command.dump.getSHA.stdout

command: {
  dump: {
      getSHA: exec.Run & {
          cmd: "echo eb4834698045ae4056f314ae0ca3a4030d7c2de8"
          stdout: string
      }
      output: cli.Print & {
          text: yaml.Marshal(outputObject)
      }
  }
}

The behavior that I want is for cue cmd dump to print:

something: hello
another: world
sha: eb4834698045ae4056f314ae0ca3a4030d7c2de8

but it's erroring due to sha not being concrete. Any idea how I can do this?


@myitcv proposed the following:

you need to structure things a little differently

exec cue cmd dump
cmp stdout stdout.golden

-- x.cue --
package tenants

outputObject: {
  something: "hello"
  another:   "world"
}
-- x_tool.cue --
package tenants

import (
  "encoding/yaml"
  "strings"
  "tool/cli"
  "tool/exec"
)

inputs: gitSha: command.dump.getSHA.stdout

command: {
  dump: {
      getSHA: exec.Run & {
          cmd:    "echo eb4834698045ae4056f314ae0ca3a4030d7c2de8"
          stdout: string
      }
      output: cli.Print & {
          text: yaml.Marshal(outputObject & {
              gitSha: strings.TrimSpace(getSHA.stdout)
          })
      }
  }
}
-- stdout.golden --
something: hello
gitSha: eb4834698045ae4056f314ae0ca3a4030d7c2de8
another: world

I include the narrative prose to allow us to see what this might look like as a "user question" concept guide: https://cuelang.org/search/?q=tag:%22user%20question%22

Alternatively, a howto guide wouldn't need to refer to the non-working CUE.