2021-12-20 20:47:42 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
2021-12-22 19:02:52 +01:00
|
|
|
"strings"
|
2021-12-20 20:47:42 +01:00
|
|
|
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
2021-12-22 19:02:52 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-12-20 20:47:42 +01:00
|
|
|
"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) {
|
2021-12-22 19:02:52 +01:00
|
|
|
var gitPull struct {
|
|
|
|
Remote string
|
|
|
|
Ref string
|
|
|
|
KeepGitDir bool
|
2021-12-22 19:56:58 +01:00
|
|
|
Auth struct {
|
|
|
|
Username string
|
|
|
|
}
|
2021-12-20 20:47:42 +01:00
|
|
|
}
|
|
|
|
|
2021-12-22 19:02:52 +01:00
|
|
|
if err := v.Decode(&gitPull); err != nil {
|
|
|
|
return nil, err
|
2021-12-20 20:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gitOpts := []llb.GitOption{}
|
|
|
|
|
2021-12-22 19:02:52 +01:00
|
|
|
lg := log.Ctx(ctx)
|
2021-12-20 20:47:42 +01:00
|
|
|
|
2021-12-22 19:02:52 +01:00
|
|
|
if gitPull.KeepGitDir {
|
|
|
|
lg.Debug().Str("keepGitDir", "true").Msg("adding git option")
|
2021-12-20 20:47:42 +01:00
|
|
|
gitOpts = append(gitOpts, llb.KeepGitDir())
|
|
|
|
}
|
2021-12-20 21:30:35 +01:00
|
|
|
|
2021-12-22 19:56:58 +01:00
|
|
|
if gitPull.Auth.Username != "" {
|
|
|
|
pwd := v.Lookup("auth.password")
|
2021-12-22 19:02:52 +01:00
|
|
|
|
|
|
|
pwdSecret, err := pctx.Secrets.FromValue(pwd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remote, err := url.Parse(gitPull.Remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:43:45 +01:00
|
|
|
lg.Debug().Str("username", gitPull.Auth.Username).Str("password", "***").Msg("using username:password auth")
|
2021-12-22 19:56:58 +01:00
|
|
|
remote.User = url.UserPassword(gitPull.Auth.Username, strings.TrimSpace(pwdSecret.PlainText()))
|
2021-12-22 19:02:52 +01:00
|
|
|
gitPull.Remote = remote.String()
|
2021-12-22 19:56:58 +01:00
|
|
|
} else if authToken := v.Lookup("auth.authToken"); plancontext.IsSecretValue(authToken) {
|
2021-12-20 20:47:42 +01:00
|
|
|
authTokenSecret, err := pctx.Secrets.FromValue(authToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-22 19:02:52 +01:00
|
|
|
lg.Debug().Str("authToken", "***").Msg("adding git option")
|
2021-12-20 20:47:42 +01:00
|
|
|
gitOpts = append(gitOpts, llb.AuthTokenSecret(authTokenSecret.ID()))
|
2021-12-22 19:56:58 +01:00
|
|
|
} else if authHeader := v.Lookup("auth.authHeader"); plancontext.IsSecretValue(authHeader) {
|
2021-12-20 20:47:42 +01:00
|
|
|
authHeaderSecret, err := pctx.Secrets.FromValue(authHeader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-22 19:02:52 +01:00
|
|
|
lg.Debug().Str("authHeader", "***").Msg("adding git option")
|
2021-12-20 20:47:42 +01:00
|
|
|
gitOpts = append(gitOpts, llb.AuthHeaderSecret(authHeaderSecret.ID()))
|
|
|
|
}
|
|
|
|
|
2021-12-22 19:02:52 +01:00
|
|
|
remoteRedacted := gitPull.Remote
|
|
|
|
if u, err := url.Parse(gitPull.Remote); err == nil {
|
|
|
|
remoteRedacted = u.Redacted()
|
|
|
|
}
|
|
|
|
|
|
|
|
gitOpts = append(gitOpts, withCustomName(v, "GitPull %s@%s", remoteRedacted, gitPull.Ref))
|
2021-12-20 20:47:42 +01:00
|
|
|
|
2021-12-22 19:02:52 +01:00
|
|
|
st := llb.Git(gitPull.Remote, gitPull.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
|
|
|
})
|
|
|
|
}
|