Marcel van Lohuizen | dd5e589 | 2018-11-22 23:29:16 +0100 | [diff] [blame] | 1 | # `cue` command reference |
| 2 | |
| 3 | This documentation is a formatted from of the builtin documentation of the |
| 4 | cue help command. |
| 5 | |
| 6 | ## General usage |
| 7 | |
| 8 | ``` |
| 9 | cue [command] |
| 10 | ``` |
| 11 | |
| 12 | The commands are: |
| 13 | |
| 14 | ``` |
| 15 | cmd run a user-defined shell command |
| 16 | eval evaluate a configuration file |
| 17 | export outputs an evaluated configuration in a standard format |
| 18 | extract |
| 19 | fmt formats Cue configuration files. |
| 20 | help Help about any command |
| 21 | import convert other data formats to CUE files |
| 22 | list list packages or modules |
| 23 | serve starts a builtin or user-defined service |
| 24 | |
| 25 | Flags: |
| 26 | --config string config file (default is $HOME/.cue) |
| 27 | --debug give detailed error info |
| 28 | -h, --help help for cue |
| 29 | --pkg string CUE package to evaluate |
| 30 | --root load a Cue package from its root |
| 31 | ``` |
| 32 | |
| 33 | ## Start a bug report |
| 34 | |
| 35 | TODO |
| 36 | |
| 37 | ## Evaluate a CUE file |
| 38 | |
| 39 | ``` |
| 40 | Flags: |
| 41 | --pkg string Cue package to evaluate |
| 42 | ``` |
| 43 | |
| 44 | ## Formatting CUE files |
| 45 | |
| 46 | `fmt` formats the given files or the files for the given packages in place. |
| 47 | |
| 48 | Usage: |
| 49 | ``` |
| 50 | cue fmt [-s] [packages] [flags] |
| 51 | ``` |
| 52 | Flags: |
| 53 | ``` |
| 54 | --config string config file (default is $HOME/.cue) |
| 55 | --debug give detailed error info |
| 56 | -n, --dryrun only run simulation |
| 57 | -p, --package string Cue package to evaluate |
| 58 | -s, --simplify simplify output |
| 59 | ``` |
| 60 | |
| 61 | ## Exporting results |
| 62 | |
| 63 | `export` evaluates the configuration found in the current directory |
| 64 | and prints the emit value to stdout. |
| 65 | |
| 66 | Examples: |
| 67 | Evaluated and emit |
| 68 | |
| 69 | ``` |
| 70 | # a single file |
| 71 | cue export config.cue |
| 72 | |
| 73 | # multiple files: these are combined at the top-level. |
| 74 | # Order doesn't matter. |
| 75 | cue export file1.cue foo/file2.cue |
| 76 | |
| 77 | # all files within the "mypkg" package: this includes all files |
| 78 | # in the current directory and its ancestor directories that are marked |
| 79 | # with the same package. |
| 80 | cue export -p cloud |
| 81 | |
| 82 | # the -p flag can be omitted if the directory only contains files for |
| 83 | # the "mypkg" package. |
| 84 | cue export |
| 85 | ``` |
| 86 | |
| 87 | ### Emit value |
| 88 | |
| 89 | For CUE files, the generated configuration is derived from the top-level single expression, the emit value. For example, the file |
| 90 | |
| 91 | ``` |
| 92 | // config.cue |
| 93 | arg1: 1 |
| 94 | arg2: "my string" |
| 95 | |
| 96 | { |
| 97 | a: arg1 |
| 98 | b: arg2 |
| 99 | } |
| 100 | ``` |
| 101 | yields the following JSON: |
| 102 | ``` |
| 103 | { |
| 104 | "a": 1, |
| 105 | "b": "my string" |
| 106 | } |
| 107 | ``` |
| 108 | In absence of arguments, the current directory is loaded as a package instance. |
| 109 | A package instance for a directory contains all files in the directory and its |
| 110 | ancestor directories, up to the module root, belonging to the same package. |
| 111 | |
| 112 | If the package is not explicitly defined by the '-p' flag, it must be uniquely |
| 113 | defined by the files in the current directory. |
| 114 | |
| 115 | ## Import files in another format |
| 116 | |
| 117 | `import` converts other data formats, like JSON and YAML to CUE files. |
| 118 | The following file formats are currently supported: |
| 119 | |
| 120 | ``` |
| 121 | Format Extensions |
| 122 | JSON .json .jsonl .ndjson |
| 123 | YAML .yaml .yml |
| 124 | ``` |
| 125 | |
| 126 | Files can either be specified explicitly, or inferred from the specified |
| 127 | packages. |
| 128 | In either case, the file extension is replaced with `.cue`. |
| 129 | It will fail if the file already exists by default. |
| 130 | The -f flag overrides this. |
| 131 | |
| 132 | Examples: |
| 133 | |
| 134 | ``` |
| 135 | # Convert individual files: |
| 136 | $ cue import foo.json bar.json # create foo.yaml and bar.yaml |
| 137 | |
| 138 | # Convert all json files in the indicated directories: |
| 139 | $ cue import ./... -type=json |
| 140 | ``` |
| 141 | |
| 142 | |
| 143 | ### The `--path` flag |
| 144 | |
| 145 | By default the parsed files are included as emit values. |
| 146 | This default can be overridden by specifying a path, which has two forms: |
| 147 | |
| 148 | ``` |
| 149 | -p ident* |
| 150 | ``` |
| 151 | |
| 152 | and |
| 153 | ``` |
| 154 | -p ident "->" expr* |
| 155 | ``` |
| 156 | |
| 157 | The first form specifies a fixed path. |
| 158 | The empty path indicates including the value at the root. |
| 159 | The second form allows expressing the path in terms of the imported value. |
| 160 | An unbound identifier in the second form denotes a fixed name. |
| 161 | Packages may be included with the -imports flag. |
| 162 | Imports for top-level core packages are elided. |
| 163 | |
| 164 | |
| 165 | ### Handling multiple documents or streams |
| 166 | |
| 167 | To handle Multi-document files, such as concatenated JSON objects or YAML files |
| 168 | with document separators (---) the user must specify either the -path, -list, or |
| 169 | -files flag. |
| 170 | The -path flag assign each element to a path (identical paths are |
| 171 | treated as usual); -list concatenates the entries, and -files causes each entry |
| 172 | to be written to a different file. |
| 173 | The -files flag may only be used if files are explicitly imported. |
| 174 | The -list flag may be used in combination with the -path |
| 175 | flag, concatenating each entry to the mapped location. |
| 176 | |
| 177 | Examples: |
| 178 | |
| 179 | ``` |
| 180 | $ cat <<EOF > foo.yaml |
| 181 | kind: Service |
| 182 | name: booster |
| 183 | EOF |
| 184 | |
| 185 | # include the parsed file as an emit value: |
| 186 | $ cue import foo.yaml |
| 187 | $ cat foo.cue |
| 188 | { |
| 189 | kind: Service |
| 190 | name: booster |
| 191 | } |
| 192 | |
| 193 | # include the parsed file at the root of the Cue file: |
| 194 | $ cue import -f -p "" foo.yaml |
| 195 | $ cat foo.cue |
| 196 | kind: Service |
| 197 | name: booster |
| 198 | |
| 199 | # include the import config at the mystuff path |
| 200 | $ cue import -f -p mystuff foo.yaml |
| 201 | $ cat foo.cue |
| 202 | myStuff: { |
| 203 | kind: Service |
| 204 | name: booster |
| 205 | } |
| 206 | |
| 207 | # append another object to the input file |
| 208 | $ cat <<EOF >> foo.yaml |
| 209 | --- |
| 210 | kind: Deployment |
| 211 | name: booster |
| 212 | replicas: 1 |
| 213 | |
| 214 | # base the path values on th input |
| 215 | $ cue import -f -p "x -> strings.ToLower(x.kind) x.name" foo.yaml |
| 216 | $ cat foo.cue |
| 217 | service booster: { |
| 218 | kind: "Service" |
| 219 | name: "booster" |
| 220 | } |
| 221 | |
| 222 | deployment booster: { |
| 223 | kind: "Deployment" |
| 224 | name: "booster |
| 225 | replicas: 1 |
| 226 | } |
| 227 | |
| 228 | # base the path values on th input |
| 229 | $ cue import -f -list -foo.yaml |
| 230 | $ cat foo.cue |
| 231 | [{ |
| 232 | kind: "Service" |
| 233 | name: "booster" |
| 234 | }, { |
| 235 | kind: "Deployment" |
| 236 | name: "booster |
| 237 | replicas: 1 |
| 238 | }] |
| 239 | |
| 240 | # base the path values on th input |
| 241 | $ cue import -f -list -p "x->strings.ToLower(x.kind)" foo.yaml |
| 242 | $ cat foo.cue |
| 243 | service: [{ |
| 244 | kind: "Service" |
| 245 | name: "booster" |
| 246 | } |
| 247 | deployment: [{ |
| 248 | kind: "Deployment" |
| 249 | name: "booster |
| 250 | replicas: 1 |
| 251 | }] |
| 252 | ``` |
| 253 | |
| 254 | Usage: |
| 255 | ``` |
| 256 | cue import [flags] |
| 257 | ``` |
| 258 | |
| 259 | Flags: |
| 260 | ``` |
| 261 | --dryrun force overwriting existing files |
| 262 | --files force overwriting existing files |
| 263 | --fix string apply given fix |
| 264 | -f, --force force overwriting existing files |
| 265 | -h, --help help for import |
| 266 | --list concatenate multiple objects into a list |
| 267 | -n, --name string glob filter for file names |
| 268 | -o, --out string alternative output or - for stdout |
| 269 | -p, --path string path to include root |
| 270 | --type string only apply to files of this type |
| 271 | ``` |
| 272 | |
| 273 | ## Extracting CUE files from source code |
| 274 | |
| 275 | TODO: doc |
| 276 | |
| 277 | ## Running scripts with CUE |
| 278 | |
| 279 | `cmd` executes user-defined named commands for each of the listed instances. |
| 280 | |
| 281 | Commands define actions on instances. |
| 282 | For instance, they may define how to upload a configuration to Kubernetes. |
| 283 | Commands are defined in cue files ending with `_tool.cue` while otherwise using |
| 284 | the same packaging rules: tool files must have a matching package clause and |
| 285 | the same rules as for normal files define which will be included in which |
| 286 | package. |
| 287 | Tool files have access to the package scope, but none of the fields defined |
| 288 | in a tool file influence the output of a package. |
| 289 | Tool files are typically defined at the module root so that they apply |
| 290 | to all instances. |
| 291 | |
| 292 | |
| 293 | ### Tasks |
| 294 | |
| 295 | Each command consists of one or more tasks. |
| 296 | A task may load or write a file, consult a user on the command line, |
| 297 | or fetch a web page, and so on. |
| 298 | Each task has inputs and outputs. |
| 299 | Outputs are typically are typically filled out by the task implementation as |
| 300 | the task completes. |
| 301 | |
| 302 | Inputs of tasks my refer to outputs of other tasks. |
| 303 | The cue tool does a static analysis of the configuration and only starts tasks |
| 304 | that are fully specified. |
| 305 | Upon completion of each task, cue rewrites the instance, |
| 306 | filling in the completed task, and reevaluates which other tasks can now start, |
| 307 | and so on until all tasks have completed. |
| 308 | |
| 309 | |
| 310 | ### Command definition |
| 311 | |
| 312 | Commands are defined at the top-level of the configuration and all follow the |
| 313 | following pattern: |
| 314 | |
| 315 | ``` |
| 316 | command <Name>: { // from "cue/tool".Command |
| 317 | // usage gives a short usage pattern of the command. |
| 318 | // Example: |
| 319 | // fmt [-n] [-x] [packages] |
| 320 | usage: Name | string |
| 321 | |
| 322 | // short gives a brief on-line description of the command. |
| 323 | // Example: |
| 324 | // reformat package sources |
| 325 | short: "" | string |
| 326 | |
| 327 | // long gives a detailed description of the command, including |
| 328 | // a description of flags usage and examples. |
| 329 | long: "" | string |
| 330 | |
| 331 | // A task defines a single action to be run as part of this command. |
| 332 | // Each task can have inputs and outputs, depending on the type |
| 333 | // task. The outputs are initially unspecified, but are filled out |
| 334 | // by the tooling |
| 335 | // |
| 336 | task <Name>: { // from "cue/tool".Task |
| 337 | // supported fields depend on type |
| 338 | } |
| 339 | |
| 340 | VarValue = string | bool | int | float | [...string|int|float] |
| 341 | |
| 342 | // var declares values that can be set by command line flags or |
| 343 | // environment variables. |
| 344 | // |
| 345 | // Example: |
| 346 | // // environment to run in |
| 347 | // var env: "test" | "prod" |
| 348 | // The tool would print documentation of this flag as: |
| 349 | // Flags: |
| 350 | // --env string environment to run in: test(default) or prod |
| 351 | var <Name>: VarValue |
| 352 | |
| 353 | // flag defines a command line flag. |
| 354 | // |
| 355 | // Example: |
| 356 | // var env: "test" | "prod" |
| 357 | // |
| 358 | // // augment the flag information for var |
| 359 | // flag env: { |
| 360 | // shortFlag: "e" |
| 361 | // description: "environment to run in" |
| 362 | // } |
| 363 | // |
| 364 | // The tool would print documentation of this flag as: |
| 365 | // Flags: |
| 366 | // -e, --env string environment to run in: test(default), staging, or prod |
| 367 | // |
| 368 | flag <Name>: { // from "cue/tool".Flag |
| 369 | // value defines the possible values for this flag. |
| 370 | // The default is string. Users can define default values by |
| 371 | // using disjunctions. |
| 372 | value: env[Name].value | VarValue |
| 373 | |
| 374 | // name, if set, allows var to be set with the command-line flag |
| 375 | // of the given name. null disables the command line flag. |
| 376 | name: Name | null | string |
| 377 | |
| 378 | // short defines an abbreviated version of the flag. |
| 379 | // Disabled by default. |
| 380 | short: null | string |
| 381 | } |
| 382 | |
| 383 | // populate flag with the default values for |
| 384 | flag: { "\(k)": { value: v } | null for k, v in var } |
| 385 | |
| 386 | // env defines environment variables. It is populated with values |
| 387 | // for var. |
| 388 | // |
| 389 | // To specify a var without an equivalent environment variable, |
| 390 | // either specify it as a flag directly or disable the equally |
| 391 | // named env entry explicitly: |
| 392 | // |
| 393 | // var foo: string |
| 394 | // env foo: null // don't use environment variables for foo |
| 395 | // |
| 396 | env <Name>: { |
| 397 | // name defines the environment variable that sets this flag. |
| 398 | name: "CUE_VAR_" + strings.Upper(Name) | string | null |
| 399 | |
| 400 | // The value retrieved from the environment variable or null |
| 401 | // if not set. |
| 402 | value: null | string | bytes |
| 403 | } |
| 404 | env: { "\(k)": { value: v } | null for k, v in var } |
| 405 | } |
| 406 | ``` |
| 407 | |
| 408 | Available tasks can be found in the package documentation at |
| 409 | |
| 410 | ``` |
| 411 | cmd/cue/tool/tasks. |
| 412 | ``` |
| 413 | |
| 414 | More on tasks can be found in the tasks topic. |
| 415 | |
| 416 | Examples |
| 417 | A simple file using command line execution: |
| 418 | |
| 419 | hello.cue: |
| 420 | ``` |
| 421 | package foo |
| 422 | |
| 423 | import "cue/tool/tasks/exec" |
| 424 | |
| 425 | city: "Amsterdam" |
| 426 | ``` |
| 427 | |
| 428 | hello_tool.cue: |
| 429 | ``` |
| 430 | package foo |
| 431 | |
| 432 | // Say hello! |
| 433 | command hello: { |
| 434 | // whom to say hello to |
| 435 | var who: "World" | string |
| 436 | |
| 437 | task print: exec.Run({ |
| 438 | cmd: "echo Hello \(var.who)! Welcome to \(city)." |
| 439 | }) |
| 440 | } |
| 441 | ``` |
| 442 | |
| 443 | Invoking the script on the command line: |
| 444 | |
| 445 | ``` |
| 446 | $ cue cmd echo |
| 447 | Hello World! Welcome to Amsterdam. |
| 448 | |
| 449 | $ cue cmd echo -who you |
| 450 | Hello you! Welcome to Amsterdam. |
| 451 | ``` |
| 452 | An example with tasks depending on each other: |
| 453 | |
| 454 | ``` |
| 455 | package foo |
| 456 | |
| 457 | import "cue/tool/tasks/exec" |
| 458 | |
| 459 | city: "Amsterdam" |
| 460 | |
| 461 | // Say hello! |
| 462 | command hello: { |
| 463 | var file: "out.txt" | string // save transcript to this file |
| 464 | |
| 465 | task ask: cli.Ask({ |
| 466 | prompt: "What is your name?" |
| 467 | response: string |
| 468 | }) |
| 469 | |
| 470 | // starts after ask |
| 471 | task echo: exec.Run({ |
| 472 | cmd: "echo Hello \(task.ask.response)!"] |
| 473 | stdout: string // capture stdout |
| 474 | }) |
| 475 | |
| 476 | // starts after echo |
| 477 | task write: file.Append({ |
| 478 | filename: var.file |
| 479 | contents: task.echo.stdout |
| 480 | }) |
| 481 | |
| 482 | // also starts after echo |
| 483 | task print: cli.Print({ |
| 484 | contents: task.echo.stdout |
| 485 | }) |
| 486 | } |
| 487 | ``` |