From 7e5f9e423ed33c6ad85d33122c6a6a701a43a59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Baranowski?= Date: Tue, 8 Aug 2023 12:12:55 +0200 Subject: [PATCH] fix: Refactor for 0.8 changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MikoĊ‚aj Baranowski --- catalog/githubcli/dagger.go | 4 ++-- catalog/gotest/mage.go | 5 +++-- daggers/containers/customizer.go | 22 +++++++++++++--------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/catalog/githubcli/dagger.go b/catalog/githubcli/dagger.go index cfc8be3..04c6908 100644 --- a/catalog/githubcli/dagger.go +++ b/catalog/githubcli/dagger.go @@ -64,9 +64,9 @@ func GetContainer( return nil, err } - _, err = container.ExitCode(ctx) + container, err = container.Sync(ctx) if err != nil { - return nil, err + return nil, fmt.Errorf("error while syncing with container: %w", err) } return container.WithEntrypoint([]string{"gh"}), nil diff --git a/catalog/gotest/mage.go b/catalog/gotest/mage.go index 6f52e01..960f695 100644 --- a/catalog/gotest/mage.go +++ b/catalog/gotest/mage.go @@ -2,6 +2,7 @@ package gotest import ( "context" + "fmt" "os" "dagger.io/dagger" @@ -53,9 +54,9 @@ func runUnitTests(ctx context.Context, container *dagger.Container) error { WithExec([]string{"test", "-v", "-race", "-coverprofile", "coverage.txt", "-covermode", "atomic", "./..."}). WithExec([]string{"tool", "cover", "-html=coverage.txt", "-o", "coverage.html"}) - _, err := testContainer.ExitCode(ctx) // execute all steps and return the exit code + testContainer, err := testContainer.Sync(ctx) // execute all steps and return the exit code if err != nil { - return err + return fmt.Errorf("error while syncing with container: %w", err) } srcDir := testContainer.Directory("/src") diff --git a/daggers/containers/customizer.go b/daggers/containers/customizer.go index 9115e73..b11e971 100644 --- a/daggers/containers/customizer.go +++ b/daggers/containers/customizer.go @@ -14,6 +14,8 @@ import ( "github.com/mesosphere/d2iq-daggers/daggers" ) +const ghTokenEnvVarName = "GITHUB_TOKEN" + // ContainerCustomizerFn is a function that customizes a container. type ContainerCustomizerFn func(*daggers.Runtime, *dagger.Container) (*dagger.Container, error) @@ -161,7 +163,7 @@ func InstallGithubCli(version string, extensions ...string) ContainerCustomizerF return nil, err } - token := runtime.Client().Host().EnvVariable("GITHUB_TOKEN").Secret() + token := runtime.Client().SetSecret(ghTokenEnvVarName, os.Getenv(ghTokenEnvVarName)) c = c.WithSecretVariable("GITHUB_TOKEN", token). WithExec([]string{"tar", "-xf", dest, "-C", extractDir}). @@ -211,9 +213,9 @@ func WithEnvVariables(env map[string]string) ContainerCustomizerFn { // WithHostEnvVariable sets the given environment variable in the container from the host. func WithHostEnvVariable(ctx context.Context, name string) ContainerCustomizerFn { return func(runtime *daggers.Runtime, c *dagger.Container) (*dagger.Container, error) { - val, err := runtime.Client().Host().EnvVariable(name).Value(ctx) - if err != nil { - return nil, fmt.Errorf("failed to get host env variable %q: %w", name, err) + val, ok := os.LookupEnv(name) + if !ok { + return nil, fmt.Errorf("failed to get host env variable %q", name) } return c.WithEnvVariable(name, val), nil @@ -278,7 +280,7 @@ func sliceToKeyMap(keys []string) map[string]bool { // WithHostEnvSecret sets the given environment variable in the container from the host as a secret. func WithHostEnvSecret(name string) ContainerCustomizerFn { return func(runtime *daggers.Runtime, c *dagger.Container) (*dagger.Container, error) { - secret := runtime.Client().Host().EnvVariable(name).Secret() + secret := runtime.Client().SetSecret(name, os.Getenv(name)) return c.WithSecretVariable(name, secret), nil } @@ -288,7 +290,9 @@ func WithHostEnvSecret(name string) ContainerCustomizerFn { func WithHostEnvSecrets(include ...string) ContainerCustomizerFn { return func(runtime *daggers.Runtime, c *dagger.Container) (*dagger.Container, error) { for _, name := range include { - c = c.WithSecretVariable(name, runtime.Client().Host().EnvVariable(name).Secret()) + secret := runtime.Client().SetSecret(name, os.Getenv(name)) + + c = c.WithSecretVariable(name, secret) } return c, nil @@ -330,9 +334,9 @@ func WithGitHubEnvs(ctx context.Context) ContainerCustomizerFn { // WithSSHSocket mounts the SSH socket from the host into the container and sets the SSH_AUTH_SOCK environment variable. func WithSSHSocket(ctx context.Context) ContainerCustomizerFn { return func(runtime *daggers.Runtime, c *dagger.Container) (*dagger.Container, error) { - path, err := runtime.Client().Host().EnvVariable("SSH_AUTH_SOCK").Value(ctx) - if err != nil { - return nil, fmt.Errorf("failed to get host env variable %q: %w", "SSH_AUTH_SOCK", err) + path, ok := os.LookupEnv("SSH_AUTH_SOCK") + if !ok { + return nil, fmt.Errorf("failed to get host env variable %q", "SSH_AUTH_SOCK") } socket := runtime.Client().Host().UnixSocket(path)