diff --git a/docs/reference/universe/dagger/op.md b/docs/reference/universe/dagger/op.md index a862f0d8..91c8953b 100644 --- a/docs/reference/universe/dagger/op.md +++ b/docs/reference/universe/dagger/op.md @@ -82,6 +82,16 @@ _No input._ _No output._ +## op.#FetchHTTP + +### op.#FetchHTTP Inputs + +_No input._ + +### op.#FetchHTTP Outputs + +_No output._ + ## op.#Load ### op.#Load Inputs diff --git a/environment/pipeline.go b/environment/pipeline.go index 28170def..d94507dd 100644 --- a/environment/pipeline.go +++ b/environment/pipeline.go @@ -19,6 +19,7 @@ import ( "github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb" bkgw "github.com/moby/buildkit/frontend/gateway/client" bkpb "github.com/moby/buildkit/solver/pb" + digest "github.com/opencontainers/go-digest" "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" @@ -209,6 +210,8 @@ func (p *Pipeline) doOp(ctx context.Context, op *compiler.Value, st llb.State) ( return p.PushContainer(ctx, op, st) case "fetch-git": return p.FetchGit(ctx, op, st) + case "fetch-http": + return p.FetchHTTP(ctx, op, st) case "local": return p.Local(ctx, op, st) case "load": @@ -817,6 +820,55 @@ func (p *Pipeline) FetchGit(ctx context.Context, op *compiler.Value, st llb.Stat ), nil } +func (p *Pipeline) FetchHTTP(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) { + link, err := op.Lookup("url").String() + if err != nil { + return st, err + } + + linkRedacted := link + if u, err := url.Parse(link); err == nil { + linkRedacted = u.Redacted() + } + + httpOpts := []llb.HTTPOption{} + var opts struct { + Checksum string + Filename string + Mode int64 + UID int + GID int + } + + if err := op.Decode(&opts); err != nil { + return st, err + } + + if opts.Checksum != "" { + dgst, err := digest.Parse(opts.Checksum) + if err != nil { + return st, err + } + httpOpts = append(httpOpts, llb.Checksum(dgst)) + } + if opts.Filename != "" { + httpOpts = append(httpOpts, llb.Filename(opts.Filename)) + } + if opts.Mode != 0 { + httpOpts = append(httpOpts, llb.Chmod(fs.FileMode(opts.Mode))) + } + if opts.UID != 0 && opts.GID != 0 { + httpOpts = append(httpOpts, llb.Chown(opts.UID, opts.GID)) + } + + httpOpts = append(httpOpts, llb.WithCustomName(p.vertexNamef("FetchHTTP %s", linkRedacted))) + + return llb.HTTP( + link, + httpOpts..., + ), nil +} + func (p *Pipeline) DockerBuild(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) { var ( dockerContext = op.Lookup("context") diff --git a/stdlib/dagger/op/op.cue b/stdlib/dagger/op/op.cue index 123e48f4..9e492089 100644 --- a/stdlib/dagger/op/op.cue +++ b/stdlib/dagger/op/op.cue @@ -85,6 +85,16 @@ package op authHeaderSecret?: string | bytes } +#FetchHTTP: { + do: "fetch-http" + url: string + checksum?: string + filename?: string + mode?: int | *0o644 + uid?: int + gid?: int +} + #Copy: { do: "copy" from: _ diff --git a/stdlib/dagger/op/op_fullop.cue b/stdlib/dagger/op/op_fullop.cue index 3fa35920..5fa28a34 100644 --- a/stdlib/dagger/op/op_fullop.cue +++ b/stdlib/dagger/op/op_fullop.cue @@ -7,6 +7,7 @@ package op #FetchContainer | #PushContainer | #FetchGit | + #FetchHTTP | #Exec | #Local | #Copy | diff --git a/tests/ops.bats b/tests/ops.bats index 943acdef..f2f21b59 100644 --- a/tests/ops.bats +++ b/tests/ops.bats @@ -113,6 +113,14 @@ setup() { # assert_failure } +@test "op.#FetchHTTP" { + run "$DAGGER" compute "$TESTDIR"/ops/fetch-http/exist + assert_success + + run "$DAGGER" compute "$TESTDIR"/ops/fetch-http/nonexistent + assert_failure +} + @test "op.#Exec" { run "$DAGGER" compute "$TESTDIR"/ops/exec/invalid assert_failure diff --git a/tests/ops/fetch-http/exist/main.cue b/tests/ops/fetch-http/exist/main.cue new file mode 100644 index 00000000..ba7ffe43 --- /dev/null +++ b/tests/ops/fetch-http/exist/main.cue @@ -0,0 +1,9 @@ +package testing + +import "alpha.dagger.io/dagger/op" + +#up: [ + op.#FetchHTTP & { + url: "https://releases.dagger.io/dagger/latest_version" + }, +] diff --git a/tests/ops/fetch-http/nonexistent/main.cue b/tests/ops/fetch-http/nonexistent/main.cue new file mode 100644 index 00000000..b2a452e6 --- /dev/null +++ b/tests/ops/fetch-http/nonexistent/main.cue @@ -0,0 +1,9 @@ +package testing + +import "alpha.dagger.io/dagger/op" + +#up: [ + op.#FetchHTTP & { + url: "https://releases.dagger.io/dagger/nonexistent_version" + }, +]