/
git_fetcher.go
53 lines (46 loc) · 1.32 KB
/
git_fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package theme
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"go.uber.org/fx"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/util"
"github.com/go-sonic/sonic/util/xerr"
)
type gitThemeFetcherImpl struct {
fx.Out
PropertyScanner PropertyScanner
}
func (g gitThemeFetcherImpl) FetchTheme(ctx context.Context, file interface{}) (*dto.ThemeProperty, error) {
gitURL := file.(string)
splits := strings.Split(gitURL, "/")
lastSplit := splits[len(splits)-1]
tempDir := os.TempDir()
themeDirName := lastSplit
tmpThemeDir := filepath.Join(tempDir, themeDirName)
if util.FileIsExisted(tmpThemeDir) {
err := os.RemoveAll(tmpThemeDir)
if err != nil {
return nil, xerr.WithStatus(err, xerr.StatusBadRequest).WithMsg("delete tmp theme directory err")
}
}
_, err := git.PlainClone(filepath.Join(tempDir, themeDirName), false, &git.CloneOptions{
URL: gitURL,
})
if err != nil {
return nil, xerr.WithStatus(err, xerr.StatusBadRequest).WithMsg(err.Error())
}
themeProperty, err := g.PropertyScanner.ReadThemeProperty(ctx, filepath.Join(tempDir, themeDirName))
if err != nil {
return nil, err
}
return themeProperty, nil
}
func NewGitThemeFetcher(propertyScanner PropertyScanner) ThemeFetcher {
return &gitThemeFetcherImpl{
PropertyScanner: propertyScanner,
}
}