2021-12-23 00:56:45 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-23 01:01:46 +01:00
|
|
|
"fmt"
|
2021-12-23 00:56:45 +01:00
|
|
|
"io/fs"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/opencontainers/go-digest"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("HTTPFetch", func() Task { return &httpFetchTask{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpFetchTask struct {
|
|
|
|
}
|
|
|
|
|
2022-03-23 23:02:17 +01:00
|
|
|
func (c *httpFetchTask) Run(ctx context.Context, pctx *plancontext.Context, s *solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
2021-12-23 00:56:45 +01:00
|
|
|
var httpFetch struct {
|
|
|
|
Source string
|
|
|
|
Checksum string
|
|
|
|
Dest string
|
|
|
|
Permissions int
|
|
|
|
UID int
|
|
|
|
GID int
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.Decode(&httpFetch); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
linkRedacted := httpFetch.Source
|
|
|
|
if u, err := url.Parse(httpFetch.Source); err == nil {
|
|
|
|
linkRedacted = u.Redacted()
|
|
|
|
}
|
|
|
|
|
|
|
|
httpOpts := []llb.HTTPOption{}
|
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
if httpFetch.Checksum != "" {
|
|
|
|
lg.Debug().Str("checksum", httpFetch.Checksum).Msg("adding http option")
|
|
|
|
dgst, err := digest.Parse(httpFetch.Checksum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
httpOpts = append(httpOpts, llb.Checksum(dgst))
|
|
|
|
}
|
|
|
|
if httpFetch.Dest != "" {
|
|
|
|
lg.Debug().Str("dest", httpFetch.Dest).Msg("adding http option")
|
|
|
|
httpOpts = append(httpOpts, llb.Filename(httpFetch.Dest))
|
|
|
|
}
|
|
|
|
if httpFetch.Permissions != 0 {
|
2021-12-23 01:01:46 +01:00
|
|
|
lg.Debug().Str("permissions", fmt.Sprint(httpFetch.Permissions)).Msg("adding http option")
|
2021-12-23 00:56:45 +01:00
|
|
|
httpOpts = append(httpOpts, llb.Chmod(fs.FileMode(httpFetch.Permissions)))
|
|
|
|
}
|
|
|
|
if httpFetch.UID != 0 && httpFetch.GID != 0 {
|
2021-12-23 01:01:46 +01:00
|
|
|
lg.Debug().Str("uid", fmt.Sprint(httpFetch.UID)).Str("gid", fmt.Sprint(httpFetch.GID)).Msg("adding http option")
|
2021-12-23 00:56:45 +01:00
|
|
|
httpOpts = append(httpOpts, llb.Chown(httpFetch.UID, httpFetch.GID))
|
|
|
|
}
|
|
|
|
|
|
|
|
httpOpts = append(httpOpts, withCustomName(v, "FetchHTTP %s", linkRedacted))
|
|
|
|
|
|
|
|
st := llb.HTTP(httpFetch.Source, httpOpts...)
|
|
|
|
|
|
|
|
result, err := s.Solve(ctx, st, pctx.Platform.Get())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := pctx.FS.New(result)
|
|
|
|
return compiler.NewValue().FillFields(map[string]interface{}{
|
|
|
|
"output": fs.MarshalCUE(),
|
|
|
|
})
|
|
|
|
}
|