Init project
This commit is contained in:
39
internal/tools/git/gitlib.go
Normal file
39
internal/tools/git/gitlib.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
)
|
||||
type GitLib struct {
|
||||
SSHKey string
|
||||
SSHKeyPassword string
|
||||
}
|
||||
|
||||
func (g *GitLib) CloneRepo (ctx context.Context, gitURL, revision string) (string, error) {
|
||||
log := log.FromContext(ctx)
|
||||
workdir, err := os.MkdirTemp("/tmp", "helmdownloader")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Info("A temporary directory is created", "path", workdir)
|
||||
if len(g.SSHKey) > 0 {
|
||||
keys, err := ssh.NewPublicKeys("git", []byte(g.SSHKey), g.SSHKeyPassword)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = git.PlainClone(workdir, false, &git.CloneOptions{URL: gitURL, Auth: keys})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
_, err = git.PlainClone(workdir, false, &git.CloneOptions{URL: gitURL})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return workdir, nil
|
||||
}
|
Reference in New Issue
Block a user