mirror of https://github.com/go-sonic/sonic.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
2 years ago
|
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/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
|
||
|
_, 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,
|
||
|
}
|
||
|
}
|