Add #TrimSecret task

Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
This commit is contained in:
Helder Correia 2022-03-03 14:58:30 -01:00
parent de703b1a3f
commit 5ca5f8a729
No known key found for this signature in database
GPG Key ID: C6490D872EF1DCA7
4 changed files with 92 additions and 2 deletions

View File

@ -27,3 +27,14 @@ package dagger
// Contents of the secret // Contents of the secret
output: #Secret output: #Secret
} }
// Trim leading and trailing space characters from a secret
#TrimSecret: {
$dagger: task: _name: "TrimSecret"
// Original secret
input: #Secret
// New trimmed secret
output: #Secret
}

31
plan/task/trimsecret.go Normal file
View File

@ -0,0 +1,31 @@
package task
import (
"context"
"strings"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("TrimSecret", func() Task { return &trimSecretTask{} })
}
type trimSecretTask struct {
}
func (t *trimSecretTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
input, err := pctx.Secrets.FromValue(v.Lookup("input"))
if err != nil {
return nil, err
}
plaintext := strings.TrimSpace(input.PlainText())
secret := pctx.Secrets.New(plaintext)
return compiler.NewValue().FillFields(map[string]interface{}{
"output": secret.MarshalCUE(),
})
}

View File

@ -134,6 +134,12 @@ setup() {
"$DAGGER" up ./newsecret.cue "$DAGGER" up ./newsecret.cue
} }
@test "task: #TrimSecret" {
cd "$TESTDIR"/tasks/trimsecret
"$DAGGER" up ./trimsecret.cue
}
@test "task: #Source" { @test "task: #Source" {
cd "$TESTDIR"/tasks/source cd "$TESTDIR"/tasks/source
"$DAGGER" up ./source.cue "$DAGGER" up ./source.cue

View File

@ -0,0 +1,42 @@
package main
import (
"dagger.io/dagger"
)
dagger.#Plan & {
actions: {
image: dagger.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}
generate: dagger.#Exec & {
input: image.output
args: ["sh", "-c", "echo ' test ' > /secret"]
}
load: dagger.#NewSecret & {
input: generate.output
trimSpace: false
path: "/secret"
}
trim: dagger.#TrimSecret & {
input: load.output
}
verify: dagger.#Exec & {
input: image.output
mounts: secret: {
dest: "/run/secrets/test"
contents: trim.output
}
args: [
"sh", "-c",
#"""
test "$(cat /run/secrets/test)" = "test"
"""#,
]
}
}
}