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/inputsecretenv.go
Richard Jones 2561f942c6
added trimSpace to inputs secrets
Signed-off-by: Richard Jones <richard@dagger.io>
2021-12-23 09:22:50 -07:00

50 lines
1.0 KiB
Go

package task
import (
"context"
"fmt"
"os"
"strings"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("InputSecretEnv", func() Task { return &inputSecretEnvTask{} })
}
type inputSecretEnvTask struct {
}
func (c *inputSecretEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
lg := log.Ctx(ctx)
var secretEnv struct {
Envvar string
TrimSpace bool
}
if err := v.Decode(&secretEnv); err != nil {
return nil, err
}
lg.Debug().Str("envvar", secretEnv.Envvar).Str("trimSpace", fmt.Sprintf("%t", secretEnv.TrimSpace)).Msg("loading secret")
env := os.Getenv(secretEnv.Envvar)
if env == "" {
return nil, fmt.Errorf("environment variable %q not set", secretEnv.Envvar)
}
if secretEnv.TrimSpace {
env = strings.TrimSpace(env)
}
secret := pctx.Secrets.New(env)
return compiler.NewValue().FillFields(map[string]interface{}{
"contents": secret.MarshalCUE(),
})
}