feat: Allow default values in client: env
(#2122)
Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
This commit is contained in:
parent
732ef96a06
commit
80ae63928b
@ -1,11 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"dagger.io/dagger"
|
||||||
|
"universe.dagger.io/docker"
|
||||||
|
)
|
||||||
|
|
||||||
dagger.#Plan & {
|
dagger.#Plan & {
|
||||||
client: env: {
|
client: env: {
|
||||||
|
// load as a string
|
||||||
REGISTRY_USER: string
|
REGISTRY_USER: string
|
||||||
|
// load as a secret
|
||||||
REGISTRY_TOKEN: dagger.#Secret
|
REGISTRY_TOKEN: dagger.#Secret
|
||||||
|
// load as a string, using a default if not defined
|
||||||
|
BASE_IMAGE: string | *"registry.example.com/image"
|
||||||
}
|
}
|
||||||
|
|
||||||
actions: pull: docker.#Pull & {
|
actions: pull: docker.#Pull & {
|
||||||
source: "registry.example.com/image"
|
source: client.env.BASE_IMAGE
|
||||||
auth: {
|
auth: {
|
||||||
username: client.env.REGISTRY_USER
|
username: client.env.REGISTRY_USER
|
||||||
secret: client.env.REGISTRY_TOKEN
|
secret: client.env.REGISTRY_TOKEN
|
||||||
|
@ -44,21 +44,26 @@ func (t clientEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ *so
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t clientEnvTask) getEnv(envvar string, v *compiler.Value, pctx *plancontext.Context) (interface{}, error) {
|
func (t clientEnvTask) getEnv(envvar string, v *compiler.Value, pctx *plancontext.Context) (interface{}, error) {
|
||||||
env := os.Getenv(envvar)
|
// Resolve default in disjunction if a type hasn't been specified
|
||||||
if env == "" {
|
val, hasDefault := v.Default()
|
||||||
|
|
||||||
|
env, hasEnv := os.LookupEnv(envvar)
|
||||||
|
if !hasEnv {
|
||||||
|
if hasDefault {
|
||||||
|
// we can only have default strings so
|
||||||
|
// don't worry about secret values here
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("environment variable %q not set", envvar)
|
return nil, fmt.Errorf("environment variable %q not set", envvar)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve default in disjunction if a type hasn't been specified
|
|
||||||
val, _ := v.Default()
|
|
||||||
|
|
||||||
if plancontext.IsSecretValue(val) {
|
if plancontext.IsSecretValue(val) {
|
||||||
secret := pctx.Secrets.New(env)
|
secret := pctx.Secrets.New(env)
|
||||||
return secret.MarshalCUE(), nil
|
return secret.MarshalCUE(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if val.IsConcrete() {
|
if val.IsConcrete() {
|
||||||
return nil, fmt.Errorf("%s: unexpected concrete value, please use a type", envvar)
|
return nil, fmt.Errorf("%s: unexpected concrete value, please use a type or set a default", envvar)
|
||||||
}
|
}
|
||||||
|
|
||||||
k := val.IncompleteKind()
|
k := val.IncompleteKind()
|
||||||
|
@ -190,7 +190,7 @@ setup() {
|
|||||||
|
|
||||||
run "$DAGGER" "do" -p ./plan/client/env/usage.cue test
|
run "$DAGGER" "do" -p ./plan/client/env/usage.cue test
|
||||||
assert_failure
|
assert_failure
|
||||||
assert_output --regexp "environment variable \"TEST_(STRING|SECRET)\" not set"
|
assert_output --regexp "environment variable \"TEST_(STRING|DEFAULT|SECRET)\" not set"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "plan/client/env concrete" {
|
@test "plan/client/env concrete" {
|
||||||
|
5
tests/plan/client/env/usage.cue
vendored
5
tests/plan/client/env/usage.cue
vendored
@ -9,6 +9,7 @@ dagger.#Plan & {
|
|||||||
client: env: {
|
client: env: {
|
||||||
TEST_STRING: string
|
TEST_STRING: string
|
||||||
TEST_SECRET: dagger.#Secret
|
TEST_SECRET: dagger.#Secret
|
||||||
|
TEST_DEFAULT: string | *"hello world"
|
||||||
}
|
}
|
||||||
actions: {
|
actions: {
|
||||||
image: core.#Pull & {
|
image: core.#Pull & {
|
||||||
@ -19,6 +20,10 @@ dagger.#Plan & {
|
|||||||
input: image.output
|
input: image.output
|
||||||
args: ["test", client.env.TEST_STRING, "=", "foo"]
|
args: ["test", client.env.TEST_STRING, "=", "foo"]
|
||||||
}
|
}
|
||||||
|
default: core.#Exec & {
|
||||||
|
input: image.output
|
||||||
|
args: ["test", client.env.TEST_DEFAULT, "=", "hello world"]
|
||||||
|
}
|
||||||
secret: core.#Exec & {
|
secret: core.#Exec & {
|
||||||
input: image.output
|
input: image.output
|
||||||
mounts: secret: {
|
mounts: secret: {
|
||||||
|
Reference in New Issue
Block a user