feat: refactor pluginmanager to fetch via Git for better support

This commit is contained in:
Technofab 2022-11-20 17:17:08 +01:00
parent 61809b4a10
commit f394814c2e
No known key found for this signature in database
GPG key ID: A0AA746B951C8830
4 changed files with 38 additions and 63 deletions

View file

@ -16,14 +16,11 @@ package pluginmanager
import (
"context"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/mholt/archiver"
"github.com/storyicon/powerproto/pkg/util"
"github.com/storyicon/powerproto/pkg/util/command"
@ -78,61 +75,50 @@ func ListGitTags(ctx context.Context, log logger.Logger, repo string) ([]string,
return append(malformed, wellFormed...), nil
}
// GithubArchive is github archive
type GithubArchive struct {
// GitRepository describes a local git repository
type GitRepository struct {
uri string
commit string
workspace string
}
// GetGithubArchive is used to download github archive
func GetGithubArchive(ctx context.Context, uri string, commitId string) (*GithubArchive, error) {
filename := fmt.Sprintf("%s.zip", commitId)
addr := fmt.Sprintf("%s/archive/%s", uri, filename)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, addr, nil)
if err != nil {
return nil, &ErrHTTPDownload{
Url: addr,
Err: err,
}
}
func GetGitRepository(ctx context.Context, uri string, commitId string, log logger.Logger) (*GitRepository, error) {
workspace, err := os.MkdirTemp("", "")
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, &ErrHTTPDownload{
Url: addr,
Err: err,
}
}
zipFilePath := filepath.Join(workspace, filename)
if err := downloadFile(resp, zipFilePath); err != nil {
return nil, &ErrHTTPDownload{
Url: addr,
Err: err,
Code: resp.StatusCode,
}
}
zip := archiver.NewZip()
if err := zip.Unarchive(zipFilePath, workspace); err != nil {
return nil, err
}
return &GithubArchive{
repo := GitRepository{
uri: uri,
commit: commitId,
workspace: workspace,
}, nil
}
// clone via the git command instead of using for example "go-git" so that the authentication is not our problem
_, err = command.Execute(ctx, log, workspace, "git", []string{
"clone", uri, repo.GetLocalDir(),
}, nil)
if err != nil {
return nil, err
}
_, err = command.Execute(ctx, log, repo.GetLocalDir(), "git", []string{
"reset", "--hard", commitId,
}, nil)
if err != nil {
return nil, err
}
return &repo, nil
}
// GetLocalDir is used to get local dir of archive
func (c *GithubArchive) GetLocalDir() string {
dir := path.Base(c.uri) + "-" + c.commit
return filepath.Join(c.workspace, dir)
// GetLocalDir is used to get local dir of repo
func (r *GitRepository) GetLocalDir() string {
dir := path.Base(r.uri) + "-" + r.commit
return filepath.Join(r.workspace, dir)
}
// Clear is used to clear the workspace
func (c *GithubArchive) Clear() error {
return os.RemoveAll(c.workspace)
func (r *GitRepository) Clear() error {
return os.RemoveAll(r.workspace)
}

View file

@ -165,7 +165,7 @@ func (b *BasicPluginManager) InstallGitRepo(ctx context.Context, uri string, com
if exists {
return local, nil
}
release, err := GetGithubArchive(ctx, uri, commitId)
release, err := GetGitRepository(ctx, uri, commitId, b.Logger)
if err != nil {
return "", err
}