instal yaml for plugin

This commit is contained in:
天元
2020-08-11 21:21:41 +08:00
parent 0174305213
commit ae4ae3fff6
4 changed files with 58 additions and 10 deletions

View File

@@ -27,6 +27,10 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)
type Source struct {
RepoName string `json:"repoName"`
}
// Template defines the content of a plugin
type Template struct {
Name string `json:"name"`
@@ -38,6 +42,9 @@ type Template struct {
//trait only
AppliesTo []string `json:"appliesTo,omitempty"`
// Plugin Source
Source *Source `json:"source,omitempty"`
}
type DefinitionType string

View File

@@ -159,7 +159,7 @@ func NewAddonListCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
}
func ListRepoAddons(table *uitable.Table, repoDir string, ioStreams cmdutil.IOStreams) error {
templates, err := plugins.LoadTempFromLocal(repoDir)
templates, err := plugins.LoadPluginsFromLocal(repoDir)
if err != nil {
return err
}
@@ -167,12 +167,22 @@ func ListRepoAddons(table *uitable.Table, repoDir string, ioStreams cmdutil.IOSt
return nil
}
baseDir := filepath.Base(repoDir)
var status string
//TODO(wonderflow): check status whether install or not
status = "uninstalled"
for _, p := range templates {
status := CheckInstalled(baseDir, p)
table.AddRow(baseDir+"/"+p.Name, p.Type, p.Type, status, p.AppliesTo)
}
ioStreams.Info(table.String())
return nil
}
func CheckInstalled(repoName string, tmp types.Template) string {
var status = "uninstalled"
dir, _ := system.GetDefinitionDir()
installed, _ := plugins.LoadTempFromLocal(dir)
for _, i := range installed {
if i.Source != nil && i.Source.RepoName == repoName && i.Name == tmp.Name && i.CrdName == tmp.CrdName {
return "installed"
}
}
return status
}

View File

@@ -45,6 +45,37 @@ func SinkTemp2Local(templates []types.Template, dir string) int {
return success
}
func LoadPluginsFromLocal(dir string) ([]types.Template, error) {
var tmps []types.Template
files, err := ioutil.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), ".cue") {
continue
}
data, err := ioutil.ReadFile(filepath.Join(dir, f.Name()))
if err != nil {
fmt.Printf("read file %s err %v\n", f.Name(), err)
continue
}
tmp, err := GetDefinitionFromURL(data, filepath.Join(dir, ".tmp"))
if err != nil {
fmt.Printf("get definition of %s err %v\n", f.Name(), err)
continue
}
tmps = append(tmps, tmp)
}
return tmps, nil
}
func LoadTempFromLocal(dir string) ([]types.Template, error) {
var tmps []types.Template
files, err := ioutil.ReadDir(dir)

View File

@@ -218,11 +218,12 @@ func (g *GithubAddon) SyncRemoteAddons() error {
}
repoDir := filepath.Join(dir, g.repoName)
system.StatAndCreate(repoDir)
var tmps []types.Template
var success, total int
for _, addon := range dirs {
if *addon.Type != "file" {
continue
}
total++
fileContent, _, _, err := g.client.Repositories.GetContents(g.ctx, g.cfg.Owner, g.cfg.Repo, *addon.Path, &github.RepositoryContentGetOptions{Ref: g.cfg.Ref})
if err != nil {
return err
@@ -234,14 +235,13 @@ func (g *GithubAddon) SyncRemoteAddons() error {
return fmt.Errorf("decode github content %s err %v", *fileContent.Path, err)
}
}
tmp, err := GetDefinitionFromURL(data, repoDir)
err = ioutil.WriteFile(filepath.Join(repoDir, *fileContent.Name), data, 0644)
if err != nil {
fmt.Printf("get definition of %s err %v\n", *addon.Path, err)
fmt.Printf("write definition %s to %s err %v\n", *fileContent.Name, repoDir, err)
continue
}
tmps = append(tmps, tmp)
success++
}
success := SinkTemp2Local(tmps, repoDir)
fmt.Printf("successfully sync %d remote addons\n", success)
fmt.Printf("successfully sync %d/%d from %s remote addons \n", success, total, g.repoName)
return nil
}