This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/plan/task/clientenv.go
Tom Chauveau 19c0f999f4
Fix export cache issue
Resolve #1551 and #1020.
We are never returning the result of solved operations so Buildkit could not
cache the layer.
This commit implements a simple system to forward operations' result to the
main build to cache it.

Signed-off-by: Vasek - Tom C <tom.chauveau@epitech.eu>
2022-03-31 20:36:50 +02:00

71 lines
1.5 KiB
Go

package task
import (
"context"
"fmt"
"os"
"cuelang.org/go/cue"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("ClientEnv", func() Task { return &clientEnvTask{} })
}
type clientEnvTask struct {
}
func (t clientEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ *solver.Solver, v *compiler.Value) (*compiler.Value, error) {
log.Ctx(ctx).Debug().Msg("loading environment variables")
fields, err := v.Fields()
if err != nil {
return nil, err
}
envs := make(map[string]interface{})
for _, field := range fields {
if field.Selector == cue.Str("$dagger") {
continue
}
envvar := field.Label()
val, err := t.getEnv(envvar, field.Value, pctx)
if err != nil {
return nil, err
}
envs[envvar] = val
}
return compiler.NewValue().FillFields(envs)
}
func (t clientEnvTask) getEnv(envvar string, v *compiler.Value, pctx *plancontext.Context) (interface{}, error) {
env := os.Getenv(envvar)
if env == "" {
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) {
secret := pctx.Secrets.New(env)
return secret.MarshalCUE(), nil
}
if val.IsConcrete() {
return nil, fmt.Errorf("%s: unexpected concrete value, please use a type", envvar)
}
k := val.IncompleteKind()
if k == cue.StringKind {
return env, nil
}
return nil, fmt.Errorf("%s: unsupported type %q", envvar, k)
}