feat(*): support repositories

Signed-off-by: storyicon <yuanchao@bilibili.com>
This commit is contained in:
storyicon 2021-07-23 14:56:00 +08:00
parent 1c73b92f0f
commit 9a99c53c5b
No known key found for this signature in database
GPG key ID: 245915D985F966CF
18 changed files with 542 additions and 255 deletions

View file

@ -16,8 +16,15 @@ package pluginmanager
import (
"context"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/mholt/archiver"
"github.com/storyicon/powerproto/pkg/util/command"
"github.com/storyicon/powerproto/pkg/util/logger"
)
@ -89,3 +96,62 @@ func ListGitTags(ctx context.Context, log logger.Logger, repo string) ([]string,
}
return tags, nil
}
// GithubArchive is github archive
type GithubArchive 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,
}
}
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{
uri: uri,
commit: commitId,
workspace: workspace,
}, 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)
}
// Clear is used to clear the workspace
func (c *GithubArchive) Clear() error {
return os.RemoveAll(c.workspace)
}