Skip to content

Commit

Permalink
fix: Refactor for 0.8 changes
Browse files Browse the repository at this point in the history
Signed-off-by: Mikołaj Baranowski <[email protected]>
  • Loading branch information
mikolajb committed Aug 8, 2023
1 parent e5a39f0 commit 7e5f9e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions catalog/githubcli/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions catalog/gotest/mage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gotest

import (
"context"
"fmt"
"os"

"dagger.io/dagger"
Expand Down Expand Up @@ -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")
Expand Down
22 changes: 13 additions & 9 deletions daggers/containers/customizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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}).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7e5f9e4

Please sign in to comment.