Skip to content

Commit

Permalink
feat: expand env variable by default
Browse files Browse the repository at this point in the history
The `withEnvVariable` API will expand shell env variable by default. And
add `noExpand` option to do not expand when it's set to `true`. The
`expand` option is now deprecated but still compatible with the previous
version.

Updates #7171

Signed-off-by: Thanabodee Charoenpiriyakij <[email protected]>
  • Loading branch information
wingyplus committed May 6, 2024
1 parent 84afe02 commit a2ce4d5
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 28 deletions.
64 changes: 56 additions & 8 deletions core/integration/container_test.go
Expand Up @@ -1090,7 +1090,16 @@ func TestContainerWithEnvVariableExpand(t *testing.T) {
t.Run("add env var without expansion", func(t *testing.T) {
out, err := c.Container().
From(alpineImage).
WithEnvVariable("FOO", "foo:$PATH").
WithEnvVariable("FOO", "foo:$PATH", dagger.ContainerWithEnvVariableOpts{Expand: false, NoExpand: true}).
WithExec([]string{"printenv", "FOO"}).
Stdout(ctx)

require.NoError(t, err)
require.Equal(t, "foo:$PATH\n", out)

out, err = c.Container().
From(alpineImage).
WithEnvVariable("FOO", "foo:$PATH", dagger.ContainerWithEnvVariableOpts{NoExpand: true}).
WithExec([]string{"printenv", "FOO"}).
Stdout(ctx)

Expand All @@ -1099,24 +1108,63 @@ func TestContainerWithEnvVariableExpand(t *testing.T) {
})

t.Run("add env var with expansion", func(t *testing.T) {
out, err := c.Container().
expected := "/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n"

ctr := c.Container().
From(alpineImage).
WithEnvVariable("USER_PATH", "/opt").
WithEnvVariable("USER_PATH", "/opt")

out, err := ctr.
WithEnvVariable(
"PATH",
"${USER_PATH}/bin:$PATH",
dagger.ContainerWithEnvVariableOpts{
Expand: true,
NoExpand: true,
},
).
WithExec([]string{"printenv", "PATH"}).
Stdout(ctx)

require.NoError(t, err)
require.Equal(t, expected, out)

out, err = ctr.
WithEnvVariable(
"PATH",
"${USER_PATH}/bin:$PATH",
dagger.ContainerWithEnvVariableOpts{
NoExpand: false,
},
).
WithExec([]string{"printenv", "PATH"}).
Stdout(ctx)

require.NoError(t, err)
require.Equal(t, expected, out)

out, err = ctr.
WithEnvVariable("PATH", "${USER_PATH}/bin:$PATH").
WithExec([]string{"printenv", "PATH"}).
Stdout(ctx)

require.NoError(t, err)
require.Equal(t, expected, out)

out, err = ctr.
WithEnvVariable(
"PATH",
"${USER_PATH}/bin:$PATH",
dagger.ContainerWithEnvVariableOpts{
Expand: true,
Expand: true,
NoExpand: true,
},
).
WithExec([]string{"printenv", "PATH"}).
Stdout(ctx)

require.NoError(t, err)
require.Equal(t,
"/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n",
out,
)
require.Equal(t, expected, out)
})
}

Expand Down
16 changes: 9 additions & 7 deletions core/schema/container.go
Expand Up @@ -117,10 +117,11 @@ func (s *containerSchema) Install() {
Doc(`Retrieves this container plus the given environment variable.`).
ArgDoc("name", `The name of the environment variable (e.g., "HOST").`).
ArgDoc("value", `The value of the environment variable. (e.g., "localhost").`).
ArgDoc("expand",
"Replace `${VAR}` or `$VAR` in the value according to the current "+
ArgDoc("noExpand",
"Do not replace `${VAR}` or `$VAR` in the value according to the current "+
`environment variables defined in the container (e.g.,
"/opt/bin:$PATH").`),
"/opt/bin:$PATH").`).
ArgDeprecated("expand", "The environment variable will be expand by default."),

dagql.Func("withSecretVariable", s.withSecretVariable).
Doc(`Retrieves this container plus an env variable containing the given secret.`).
Expand Down Expand Up @@ -725,16 +726,17 @@ func (s *containerSchema) workdir(ctx context.Context, parent *core.Container, a
}

type containerWithVariableArgs struct {
Name string
Value string
Expand bool `default:"false"`
Name string
Value string
Expand bool `default:"false"`
NoExpand bool `default:"false"`
}

func (s *containerSchema) withEnvVariable(ctx context.Context, parent *core.Container, args containerWithVariableArgs) (*core.Container, error) {
return parent.UpdateImageConfig(ctx, func(cfg specs.ImageConfig) specs.ImageConfig {
value := args.Value

if args.Expand {
if !args.NoExpand || args.Expand {
value = os.Expand(value, func(k string) string {
v, _ := core.LookupEnv(cfg.Env, k)
return v
Expand Down
11 changes: 7 additions & 4 deletions docs/docs-graphql/schema.graphqls
Expand Up @@ -441,15 +441,18 @@ type Container {

"""Retrieves this container plus the given environment variable."""
withEnvVariable(
"""
Replace `${VAR}` or `$VAR` in the value according to the current environment
variables defined in the container (e.g., "/opt/bin:$PATH").
"""
"""DEPRECATED: The environment variable will be expand by default."""
expand: Boolean = false

"""The name of the environment variable (e.g., "HOST")."""
name: String!

"""
Do not replace `${VAR}` or `$VAR` in the value according to the current
environment variables defined in the container (e.g., "/opt/bin:$PATH").
"""
noExpand: Boolean = false

"""The value of the environment variable. (e.g., "localhost")."""
value: String!
): Container!
Expand Down
7 changes: 5 additions & 2 deletions sdk/elixir/lib/dagger/gen/container.ex

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion sdk/go/dagger.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion sdk/php/generated/Container.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions sdk/python/src/dagger/client/gen.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion sdk/rust/crates/dagger-sdk/src/gen.rs
Expand Up @@ -1346,9 +1346,12 @@ pub struct ContainerWithEntrypointOpts {
}
#[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithEnvVariableOpts {
/// Replace `${VAR}` or `$VAR` in the value according to the current environment variables defined in the container (e.g., "/opt/bin:$PATH").
/// DEPRECATED: The environment variable will be expand by default.
#[builder(setter(into, strip_option), default)]
pub expand: Option<bool>,
/// Do not replace `${VAR}` or `$VAR` in the value according to the current environment variables defined in the container (e.g., "/opt/bin:$PATH").
#[builder(setter(into, strip_option), default)]
pub no_expand: Option<bool>,
}
#[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithExecOpts<'a> {
Expand Down Expand Up @@ -2202,6 +2205,9 @@ impl Container {
if let Some(expand) = opts.expand {
query = query.arg("expand", expand);
}
if let Some(no_expand) = opts.no_expand {
query = query.arg("noExpand", no_expand);
}
Container {
proc: self.proc.clone(),
selection: query,
Expand Down
10 changes: 8 additions & 2 deletions sdk/typescript/api/client.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a2ce4d5

Please sign in to comment.