Skip to content

Commit 436d938

Browse files
authored
fix the plugin loading issues (#3)
* Use http-downloader instead of duplicated code lines
1 parent f9b4370 commit 436d938

File tree

11 files changed

+347
-248
lines changed

11 files changed

+347
-248
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go 1.15
55
require (
66
github.com/gosuri/uilive v0.0.3 // indirect
77
github.com/gosuri/uiprogress v0.0.1
8-
github.com/linuxsuren/cobra-extension v0.0.1
8+
github.com/linuxsuren/cobra-extension v0.0.10
9+
github.com/linuxsuren/http-downloader v0.0.10
910
github.com/mattn/go-isatty v0.0.12 // indirect
1011
github.com/mitchellh/go-homedir v1.1.0
1112
github.com/pkg/errors v0.9.1
1213
github.com/spf13/cobra v1.1.1
13-
github.com/stretchr/testify v1.6.1 // indirect
1414
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
1515
golang.org/x/text v0.3.4 // indirect
1616
gopkg.in/src-d/go-git.v4 v4.13.1

go.sum

Lines changed: 58 additions & 0 deletions
Large diffs are not rendered by default.

pkg/cmd/completion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
)
88

99
// ValidPluginNames returns the valid plugin name list
10-
func ValidPluginNames(cmd *cobra.Command, args []string, prefix string) (pluginNames []string, directive cobra.ShellCompDirective) {
10+
func ValidPluginNames(cmd *cobra.Command, args []string, prefix, pluginOrg, pluginRepo string) (pluginNames []string, directive cobra.ShellCompDirective) {
1111
directive = cobra.ShellCompDirectiveNoFileComp
12-
if plugins, err := pkg.FindPlugins(); err == nil {
12+
if plugins, err := pkg.FindPlugins(pluginOrg, pluginRepo); err == nil {
1313
pluginNames = make([]string, 0)
1414
for i := range plugins {
1515
plugin := plugins[i]

pkg/cmd/fetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error)
5656
return
5757
}
5858

59-
pluginRepoCache := fmt.Sprintf("%s/.jenkins-cli/plugins-repo", userHome)
59+
pluginRepoCache := fmt.Sprintf("%s/.%s/plugins-repo", userHome, c.PluginRepoName)
6060
c.output = cmd.OutOrStdout()
6161

6262
cmd.Printf("start to fetch metadata from '%s', cache to '%s'\n", c.PluginRepo, pluginRepoCache)

pkg/cmd/install.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"compress/gzip"
66
"fmt"
77
"github.com/linuxsuren/go-cli-plugin/pkg"
8+
hd "github.com/linuxsuren/http-downloader/pkg"
89
"github.com/mitchellh/go-homedir"
910
"github.com/spf13/cobra"
1011
"gopkg.in/yaml.v2"
@@ -18,23 +19,27 @@ import (
1819
// NewConfigPluginInstallCmd create a command for fetching plugin metadata
1920
func NewConfigPluginInstallCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) {
2021
pluginInstallCmd := jcliPluginInstallCmd{
21-
PluginOrg: pluginOrg,
22-
PluginRepo: pluginRepo,
22+
PluginOrg: pluginOrg,
23+
PluginRepo: pluginRepo,
24+
PluginRepoName: pluginRepo,
2325
}
2426

2527
cmd = &cobra.Command{
26-
Use: "install",
27-
Short: "install a jcli plugin",
28-
Long: "install a jcli plugin",
29-
Args: cobra.MinimumNArgs(1),
30-
ValidArgsFunction: ValidPluginNames,
31-
RunE: pluginInstallCmd.Run,
28+
Use: "install",
29+
Short: "install a jcli plugin",
30+
Long: "install a jcli plugin",
31+
Args: cobra.MinimumNArgs(1),
32+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
33+
return ValidPluginNames(cmd, args, toComplete, pluginOrg, pluginRepo)
34+
},
35+
RunE: pluginInstallCmd.Run,
3236
}
3337

3438
// add flags
3539
flags := cmd.Flags()
3640
flags.BoolVarP(&pluginInstallCmd.ShowProgress, "show-progress", "", true,
3741
"If you want to show the progress of download")
42+
flags.IntVarP(&pluginInstallCmd.Thread, "thread", "t", 4, "Using multi-thread to download plugin")
3843
return
3944
}
4045

@@ -47,7 +52,7 @@ func (c *jcliPluginInstallCmd) Run(cmd *cobra.Command, args []string) (err error
4752
}
4853

4954
var data []byte
50-
pluginsMetadataFile := fmt.Sprintf("%s/.%s/plugins-repo/%s.yaml", userHome, c.PluginRepo, name)
55+
pluginsMetadataFile := fmt.Sprintf("%s/.%s/plugins-repo/%s.yaml", userHome, c.PluginRepoName, name)
5156
if data, err = ioutil.ReadFile(pluginsMetadataFile); err == nil {
5257
plugin := pkg.Plugin{}
5358
if err = yaml.Unmarshal(data, &plugin); err == nil {
@@ -56,7 +61,7 @@ func (c *jcliPluginInstallCmd) Run(cmd *cobra.Command, args []string) (err error
5661
}
5762

5863
if err == nil {
59-
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, name, true)
64+
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, c.PluginRepoName, name, true)
6065
err = ioutil.WriteFile(cachedMetadataFile, data, 0664)
6166
}
6267
return
@@ -69,15 +74,22 @@ func (c *jcliPluginInstallCmd) download(plu pkg.Plugin) (err error) {
6974
}
7075

7176
link := c.getDownloadLink(plu)
72-
output := fmt.Sprintf("%s/.%s/plugins/%s.tar.gz", userHome, c.PluginOrg,plu.Main)
77+
output := fmt.Sprintf("%s/.%s/plugins-repo/%s.tar.gz", userHome, c.PluginRepoName, plu.Main)
7378

74-
downloader := pkg.HTTPDownloader{
75-
RoundTripper: c.RoundTripper,
76-
TargetFilePath: output,
77-
URL: link,
78-
ShowProgress: c.ShowProgress,
79+
fmt.Printf("start to download from '%s' to '%s'\n", link, output)
80+
if c.Thread > 1 {
81+
err = hd.DownloadFileWithMultipleThread(link, output, c.Thread, c.ShowProgress)
82+
} else {
83+
downloader := hd.HTTPDownloader{
84+
RoundTripper: c.RoundTripper,
85+
TargetFilePath: output,
86+
URL: link,
87+
ShowProgress: c.ShowProgress,
88+
}
89+
err = downloader.DownloadFile()
7990
}
80-
if err = downloader.DownloadFile(); err == nil {
91+
92+
if err == nil {
8193
err = c.extractFiles(plu, output)
8294
}
8395
return

pkg/cmd/list.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import (
66
)
77

88
// NewConfigPluginListCmd create a command for list all jcli plugins
9-
func NewConfigPluginListCmd() (cmd *cobra.Command) {
10-
configPluginListCmd := &configPluginListCmd{}
9+
func NewConfigPluginListCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) {
10+
configPluginListCmd := &configPluginListCmd{
11+
PluginOrg: pluginOrg,
12+
PluginRepo: pluginRepo,
13+
PluginRepoName: pluginRepo,
14+
}
1115

1216
cmd = &cobra.Command{
1317
Use: "list",
@@ -25,7 +29,7 @@ func NewConfigPluginListCmd() (cmd *cobra.Command) {
2529
func (c *configPluginListCmd) RunE(cmd *cobra.Command, args []string) (err error) {
2630
c.Writer = cmd.OutOrStdout()
2731
var plugins []pkg.Plugin
28-
if plugins, err = pkg.FindPlugins(); err == nil {
32+
if plugins, err = pkg.FindPlugins(c.PluginOrg, c.PluginRepoName); err == nil {
2933
err = c.OutputV2(plugins)
3034
}
3135
return

pkg/cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
// AppendPluginCmd create a command as root of config plugin
88
func AppendPluginCmd(root *cobra.Command, pluginOrg, pluginRepo string) {
9-
root.AddCommand(NewConfigPluginListCmd(),
9+
root.AddCommand(NewConfigPluginListCmd(pluginOrg, pluginRepo),
1010
NewConfigPluginFetchCmd(pluginOrg, pluginRepo),
1111
NewConfigPluginInstallCmd(pluginOrg, pluginRepo),
12-
NewConfigPluginUninstallCmd())
12+
NewConfigPluginUninstallCmd(pluginOrg, pluginRepo))
1313
}

pkg/cmd/type.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
type (
1010
configPluginListCmd struct {
1111
cobra_ext.OutputOption
12+
PluginOrg string
13+
PluginRepo string
14+
PluginRepoName string
1215
}
1316

1417
jcliPluginFetchCmd struct {
@@ -27,15 +30,31 @@ type (
2730
jcliPluginInstallCmd struct {
2831
RoundTripper http.RoundTripper
2932
ShowProgress bool
33+
Thread int
3034

31-
output io.Writer
32-
PluginOrg string
33-
PluginRepo string
35+
output io.Writer
36+
PluginOrg string
37+
PluginRepo string
38+
PluginRepoName string
3439
}
3540

3641
jcliPluginUninstallCmd struct {
42+
RoundTripper http.RoundTripper
43+
ShowProgress bool
44+
45+
output io.Writer
46+
PluginOrg string
47+
PluginRepo string
48+
PluginRepoName string
3749
}
3850

3951
jcliPluginUpdateCmd struct {
52+
RoundTripper http.RoundTripper
53+
ShowProgress bool
54+
55+
output io.Writer
56+
PluginOrg string
57+
PluginRepo string
58+
PluginRepoName string
4059
}
4160
)

pkg/cmd/uninstall.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ import (
1010
)
1111

1212
// NewConfigPluginUninstallCmd create a command to uninstall a plugin
13-
func NewConfigPluginUninstallCmd() (cmd *cobra.Command) {
14-
jcliPluginUninstallCmd := jcliPluginUninstallCmd{}
13+
func NewConfigPluginUninstallCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) {
14+
jcliPluginUninstallCmd := jcliPluginUninstallCmd{
15+
PluginOrg: pluginOrg,
16+
PluginRepo: pluginRepo,
17+
PluginRepoName: pluginRepo,
18+
}
1519

1620
cmd = &cobra.Command{
1721
Use: "uninstall",
1822
Short: "Remove a plugin",
1923
Long: "Remove a plugin",
2024
Args: cobra.MinimumNArgs(1),
2125
RunE: jcliPluginUninstallCmd.RunE,
22-
ValidArgsFunction: ValidPluginNames,
26+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
27+
return ValidPluginNames(cmd, args, toComplete, pluginOrg, pluginRepo)
28+
},
2329
}
2430
return
2531
}
@@ -32,13 +38,13 @@ func (c *jcliPluginUninstallCmd) RunE(cmd *cobra.Command, args []string) (err er
3238
}
3339

3440
name := args[0]
35-
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, name, false)
41+
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, c.PluginRepoName, name, false)
3642

3743
var data []byte
3844
if data, err = ioutil.ReadFile(cachedMetadataFile); err == nil {
3945
plugin := &pkg.Plugin{}
4046
if err = yaml.Unmarshal(data, plugin); err == nil {
41-
mainFile := pkg.GetJCLIPluginPath(userHome, plugin.Main, true)
47+
mainFile := pkg.GetJCLIPluginPath(userHome, c.PluginRepoName, plugin.Main, true)
4248

4349
os.Remove(cachedMetadataFile)
4450
os.Remove(mainFile)

pkg/core.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ type PluginError struct {
2929
code int
3030
}
3131

32-
func FindPlugins() (plugins []Plugin, err error) {
32+
func FindPlugins(pluginOrg, pluginRepo string) (plugins []Plugin, err error) {
3333
var userHome string
3434
if userHome, err = homedir.Dir(); err != nil {
3535
return
3636
}
3737

3838
plugins = make([]Plugin, 0)
39-
pluginsDir := fmt.Sprintf("%s/.jenkins-cli/plugins-repo/*.yaml", userHome)
39+
pluginsDir := fmt.Sprintf("%s/.%s/plugins-repo/*.yaml", userHome, pluginRepo)
4040
//fmt.Println("start to parse plugin file from dir", pluginsDir)
4141
var files []string
4242
if files, err = filepath.Glob(pluginsDir); err == nil {
@@ -51,7 +51,7 @@ func FindPlugins() (plugins []Plugin, err error) {
5151
plugin.Main = fmt.Sprintf("jcli-%s-Plugin", plugin.Use)
5252
}
5353

54-
if _, fileErr := os.Stat(GetJCLIPluginPath(userHome, plugin.Main, true)); !os.IsNotExist(fileErr) {
54+
if _, fileErr := os.Stat(GetJCLIPluginPath(userHome, pluginRepo, plugin.Main, true)); !os.IsNotExist(fileErr) {
5555
plugin.Installed = true
5656
}
5757
plugins = append(plugins, plugin)
@@ -65,10 +65,10 @@ func FindPlugins() (plugins []Plugin, err error) {
6565
}
6666

6767
// LoadPlugins loads the plugins
68-
func LoadPlugins(cmd *cobra.Command) {
68+
func LoadPlugins(cmd *cobra.Command, pluginOrg, pluginRepo string) {
6969
var plugins []Plugin
7070
var err error
71-
if plugins, err = FindPlugins(); err != nil {
71+
if plugins, err = FindPlugins(pluginOrg, pluginRepo); err != nil {
7272
cmd.PrintErrln("Cannot load plugins successfully")
7373
return
7474
}
@@ -118,7 +118,7 @@ func LoadPlugins(cmd *cobra.Command) {
118118
return
119119
}
120120

121-
pluginExec := GetJCLIPluginPath(userHome, cmd.Annotations["main"], true)
121+
pluginExec := GetJCLIPluginPath(userHome, pluginRepo, cmd.Annotations["main"], true)
122122
err = callPluginExecutable(cmd, pluginExec, args, cmd.OutOrStdout())
123123
return
124124
},
@@ -127,11 +127,11 @@ func LoadPlugins(cmd *cobra.Command) {
127127
}
128128
}
129129

130-
//GetJCLIPluginPath returns the path of a jcli plugin
131-
func GetJCLIPluginPath(userHome, name string, binary bool) string {
130+
// GetJCLIPluginPath returns the path of a jcli plugin
131+
func GetJCLIPluginPath(userHome, pluginRepoName, name string, binary bool) string {
132132
suffix := ".yaml"
133133
if binary {
134134
suffix = ""
135135
}
136-
return fmt.Sprintf("%s/.jenkins-cli/plugins/%s%s", userHome, name, suffix)
136+
return fmt.Sprintf("%s/.%s/plugins-repo/%s%s", userHome, pluginRepoName, name, suffix)
137137
}

0 commit comments

Comments
 (0)