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
1920func 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
0 commit comments