2021-12-20 20:47:42 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("GitPull", func() Task { return &gitPullTask{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type gitPullTask struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c gitPullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
|
|
|
remote, err := v.Lookup("remote").String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ref, err := v.Lookup("ref").String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteRedacted := remote
|
|
|
|
if u, err := url.Parse(remote); err == nil {
|
|
|
|
remoteRedacted = u.Redacted()
|
|
|
|
}
|
|
|
|
|
|
|
|
gitOpts := []llb.GitOption{}
|
|
|
|
var opts struct {
|
|
|
|
KeepGitDir bool
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.Decode(&opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.KeepGitDir {
|
|
|
|
gitOpts = append(gitOpts, llb.KeepGitDir())
|
|
|
|
}
|
2021-12-20 21:30:35 +01:00
|
|
|
|
2021-12-20 20:47:42 +01:00
|
|
|
// Secret
|
2021-12-20 21:30:35 +01:00
|
|
|
if authToken := v.Lookup("authToken"); authToken.Exists() {
|
2021-12-20 20:47:42 +01:00
|
|
|
authTokenSecret, err := pctx.Secrets.FromValue(authToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gitOpts = append(gitOpts, llb.AuthTokenSecret(authTokenSecret.ID()))
|
|
|
|
}
|
2021-12-20 21:30:35 +01:00
|
|
|
|
|
|
|
if authHeader := v.Lookup("authHeader"); authHeader.Exists() {
|
2021-12-20 20:47:42 +01:00
|
|
|
authHeaderSecret, err := pctx.Secrets.FromValue(authHeader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gitOpts = append(gitOpts, llb.AuthHeaderSecret(authHeaderSecret.ID()))
|
|
|
|
}
|
|
|
|
|
|
|
|
gitOpts = append(gitOpts, withCustomName(v, "FetchGit %s@%s", remoteRedacted, ref))
|
|
|
|
|
2021-12-20 21:30:35 +01:00
|
|
|
st := llb.Git(remote, ref, gitOpts...)
|
2021-12-20 20:47:42 +01:00
|
|
|
|
|
|
|
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{}{
|
2021-12-20 21:30:35 +01:00
|
|
|
"output": fs.MarshalCUE(),
|
2021-12-20 20:47:42 +01:00
|
|
|
})
|
|
|
|
}
|