From bdae23ff6c19f184c41f1e0bc16e7ffaf1d54bbb Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 3 Jan 2021 16:04:06 +0800 Subject: [PATCH] Fix the plugins repo issue --- go.mod | 5 +---- pkg/cmd/fetch.go | 15 ++++++++------- pkg/cmd/install.go | 2 +- pkg/cmd/list.go | 2 +- pkg/cmd/type.go | 5 +++-- pkg/core.go | 6 +++++- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 4bcaa0c..fcd60d7 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,11 @@ module github.com/linuxsuren/go-cli-plugin go 1.15 require ( - github.com/golang/mock v1.4.4 github.com/gosuri/uilive v0.0.3 // indirect github.com/gosuri/uiprogress v0.0.1 - github.com/linuxsuren/cobra-extension v0.0.1 // indirect + github.com/linuxsuren/cobra-extension v0.0.1 github.com/mattn/go-isatty v0.0.12 // indirect github.com/mitchellh/go-homedir v1.1.0 - github.com/onsi/ginkgo v1.14.2 - github.com/onsi/gomega v1.10.3 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.1.1 github.com/stretchr/testify v1.6.1 // indirect diff --git a/pkg/cmd/fetch.go b/pkg/cmd/fetch.go index a0a8b29..ef11cdb 100644 --- a/pkg/cmd/fetch.go +++ b/pkg/cmd/fetch.go @@ -16,9 +16,9 @@ import ( // NewConfigPluginFetchCmd create a command for fetching plugin metadata func NewConfigPluginFetchCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) { - pluginFetchCmd := jcliPluginFetchCmd{ - PluginOrg: pluginOrg, - PluginRepo: pluginRepo, + pluginFetchCmd := &jcliPluginFetchCmd{ + PluginOrg: pluginOrg, + PluginRepoName: pluginRepo, } cmd = &cobra.Command{ @@ -34,7 +34,7 @@ but you can change it by giving a command parameter.`, pluginFetchCmd.PluginOrg, // add flags flags := cmd.Flags() flags.StringVarP(&pluginFetchCmd.PluginRepo, "plugin-repo", "", - fmt.Sprintf("https://github.com/%s/%s", pluginFetchCmd.PluginOrg, pluginFetchCmd.PluginRepo), + fmt.Sprintf("https://github.com/%s/%s", pluginFetchCmd.PluginOrg, pluginFetchCmd.PluginRepoName), "The plugin git repository URL") flags.BoolVarP(&pluginFetchCmd.Reset, "reset", "", true, "If you want to reset the git local repo when pulling it") @@ -56,11 +56,12 @@ func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error) return } - pluginRepo := fmt.Sprintf("%s/.%s/plugins-repo", userHome, c.PluginRepo) + pluginRepoCache := fmt.Sprintf("%s/.jenkins-cli/plugins-repo", userHome) c.output = cmd.OutOrStdout() + cmd.Printf("start to fetch metadata from '%s', cache to '%s'\n", c.PluginRepo, pluginRepoCache) var r *git.Repository - if r, err = git.PlainOpen(pluginRepo); err == nil { + if r, err = git.PlainOpen(pluginRepoCache); err == nil { var w *git.Worktree if w, err = r.Worktree(); err != nil { return @@ -80,7 +81,7 @@ func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error) } } else { cloneOptions := c.getCloneOptions() - _, err = git.PlainClone(pluginRepo, false, cloneOptions) + _, err = git.PlainClone(pluginRepoCache, false, cloneOptions) } return } diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go index f1053bc..d039599 100644 --- a/pkg/cmd/install.go +++ b/pkg/cmd/install.go @@ -47,7 +47,7 @@ func (c *jcliPluginInstallCmd) Run(cmd *cobra.Command, args []string) (err error } var data []byte - pluginsMetadataFile := fmt.Sprintf("%s/.%s/plugins-repo/%s.yaml", c.PluginRepo, userHome, name) + pluginsMetadataFile := fmt.Sprintf("%s/.%s/plugins-repo/%s.yaml", userHome, c.PluginRepo, name) if data, err = ioutil.ReadFile(pluginsMetadataFile); err == nil { plugin := pkg.Plugin{} if err = yaml.Unmarshal(data, &plugin); err == nil { diff --git a/pkg/cmd/list.go b/pkg/cmd/list.go index b62c4e0..136e9f7 100644 --- a/pkg/cmd/list.go +++ b/pkg/cmd/list.go @@ -7,7 +7,7 @@ import ( // NewConfigPluginListCmd create a command for list all jcli plugins func NewConfigPluginListCmd() (cmd *cobra.Command) { - configPluginListCmd := configPluginListCmd{} + configPluginListCmd := &configPluginListCmd{} cmd = &cobra.Command{ Use: "list", diff --git a/pkg/cmd/type.go b/pkg/cmd/type.go index e5d85b1..c90aa42 100644 --- a/pkg/cmd/type.go +++ b/pkg/cmd/type.go @@ -19,8 +19,9 @@ type ( Password string SSHKeyFile string - output io.Writer - PluginOrg string + output io.Writer + PluginOrg string + PluginRepoName string } jcliPluginInstallCmd struct { diff --git a/pkg/core.go b/pkg/core.go index ffc66cd..88c45ca 100644 --- a/pkg/core.go +++ b/pkg/core.go @@ -37,7 +37,9 @@ func FindPlugins() (plugins []Plugin, err error) { plugins = make([]Plugin, 0) pluginsDir := fmt.Sprintf("%s/.jenkins-cli/plugins-repo/*.yaml", userHome) - if files, err := filepath.Glob(pluginsDir); err == nil { + //fmt.Println("start to parse plugin file from dir", pluginsDir) + var files []string + if files, err = filepath.Glob(pluginsDir); err == nil { for _, metaFile := range files { var data []byte plugin := Plugin{} @@ -54,6 +56,8 @@ func FindPlugins() (plugins []Plugin, err error) { } plugins = append(plugins, plugin) } + } else { + fmt.Println("failed to parse file", metaFile) } } }