Injecting values



Tags and the CLI

CUE can inject data from the command line into your configurations. It uses the @tag()

tags.cue

package inject

// @tag() is how we inject data
env:      *"dev" | string @tag(env)      // env has a default
database: string          @tag(database) // database is "required"

// A schema for DBs with some defaults
#DB: {
	host: #hosts[env]
	port: string | *"5432"
	db:   database

	// interpolate the fields into the connection string
	conn: "postgres://\(host):\(port)/\(db)"
}

// setup our database host mapping
#hosts: [string]: string
#hosts: {
	dev: "postgres.dev"
	stg: "postgres.stg"
	prd: "postgres.prd"
}
# -t key=value  -e to eval a specific value
$ cue eval tags.cue -t database="foo" -e "#DB.conn"
"postgres://postgres.dev:5432/foo"

Combining files

We can combine data using multiple files and change the results by selecting different files when running cue.

Given the following files:

app.cue

package app

_secrets: {
	username: string
	password: string
}

app: {
	creds: {
		user: _secrets.username
		pass: _secrets.password
	}
}

dev.cue

_secrets: {
	username: "dev-user"
	password: "dev-pass"
}

prd.cue

_secrets: {
	username: "prd-user"
	password: "prd-pass"
}

We can run cue eval and switch environmental settings.

cue eval app.cue dev.cue

app: {
	creds: {
		user: "dev-user"
		pass: "dev-pass"
	}
}

cue eval app.cue prd.cue

app: {
	creds: {
		user: "prd-user"
		pass: "prd-pass"
	}
}

There are more advance patterns using cross directory packages and conditional imports. Visit the following links to see how.

  • [cross directory packages]
  • [conditional imports]
We'll never share your email with anyone else.
2024 Hofstadter, Inc