Skip to content

Commit

Permalink
fix the plugin loading issues (#3)
Browse files Browse the repository at this point in the history
* Use http-downloader instead of duplicated code lines
  • Loading branch information
LinuxSuRen committed Jan 27, 2021
1 parent f9b4370 commit 436d938
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 248 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.15
require (
github.com/gosuri/uilive v0.0.3 // indirect
github.com/gosuri/uiprogress v0.0.1
github.com/linuxsuren/cobra-extension v0.0.1
github.com/linuxsuren/cobra-extension v0.0.10
github.com/linuxsuren/http-downloader v0.0.10
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
golang.org/x/text v0.3.4 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
Expand Down
58 changes: 58 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

// ValidPluginNames returns the valid plugin name list
func ValidPluginNames(cmd *cobra.Command, args []string, prefix string) (pluginNames []string, directive cobra.ShellCompDirective) {
func ValidPluginNames(cmd *cobra.Command, args []string, prefix, pluginOrg, pluginRepo string) (pluginNames []string, directive cobra.ShellCompDirective) {
directive = cobra.ShellCompDirectiveNoFileComp
if plugins, err := pkg.FindPlugins(); err == nil {
if plugins, err := pkg.FindPlugins(pluginOrg, pluginRepo); err == nil {
pluginNames = make([]string, 0)
for i := range plugins {
plugin := plugins[i]
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error)
return
}

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

cmd.Printf("start to fetch metadata from '%s', cache to '%s'\n", c.PluginRepo, pluginRepoCache)
Expand Down
46 changes: 29 additions & 17 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"fmt"
"github.com/linuxsuren/go-cli-plugin/pkg"
hd "github.com/linuxsuren/http-downloader/pkg"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand All @@ -18,23 +19,27 @@ import (
// NewConfigPluginInstallCmd create a command for fetching plugin metadata
func NewConfigPluginInstallCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) {
pluginInstallCmd := jcliPluginInstallCmd{
PluginOrg: pluginOrg,
PluginRepo: pluginRepo,
PluginOrg: pluginOrg,
PluginRepo: pluginRepo,
PluginRepoName: pluginRepo,
}

cmd = &cobra.Command{
Use: "install",
Short: "install a jcli plugin",
Long: "install a jcli plugin",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: ValidPluginNames,
RunE: pluginInstallCmd.Run,
Use: "install",
Short: "install a jcli plugin",
Long: "install a jcli plugin",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
return ValidPluginNames(cmd, args, toComplete, pluginOrg, pluginRepo)
},
RunE: pluginInstallCmd.Run,
}

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

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

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

if err == nil {
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, name, true)
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, c.PluginRepoName, name, true)
err = ioutil.WriteFile(cachedMetadataFile, data, 0664)
}
return
Expand All @@ -69,15 +74,22 @@ func (c *jcliPluginInstallCmd) download(plu pkg.Plugin) (err error) {
}

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

downloader := pkg.HTTPDownloader{
RoundTripper: c.RoundTripper,
TargetFilePath: output,
URL: link,
ShowProgress: c.ShowProgress,
fmt.Printf("start to download from '%s' to '%s'\n", link, output)
if c.Thread > 1 {
err = hd.DownloadFileWithMultipleThread(link, output, c.Thread, c.ShowProgress)
} else {
downloader := hd.HTTPDownloader{
RoundTripper: c.RoundTripper,
TargetFilePath: output,
URL: link,
ShowProgress: c.ShowProgress,
}
err = downloader.DownloadFile()
}
if err = downloader.DownloadFile(); err == nil {

if err == nil {
err = c.extractFiles(plu, output)
}
return
Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
)

// NewConfigPluginListCmd create a command for list all jcli plugins
func NewConfigPluginListCmd() (cmd *cobra.Command) {
configPluginListCmd := &configPluginListCmd{}
func NewConfigPluginListCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) {
configPluginListCmd := &configPluginListCmd{
PluginOrg: pluginOrg,
PluginRepo: pluginRepo,
PluginRepoName: pluginRepo,
}

cmd = &cobra.Command{
Use: "list",
Expand All @@ -25,7 +29,7 @@ func NewConfigPluginListCmd() (cmd *cobra.Command) {
func (c *configPluginListCmd) RunE(cmd *cobra.Command, args []string) (err error) {
c.Writer = cmd.OutOrStdout()
var plugins []pkg.Plugin
if plugins, err = pkg.FindPlugins(); err == nil {
if plugins, err = pkg.FindPlugins(c.PluginOrg, c.PluginRepoName); err == nil {
err = c.OutputV2(plugins)
}
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

// AppendPluginCmd create a command as root of config plugin
func AppendPluginCmd(root *cobra.Command, pluginOrg, pluginRepo string) {
root.AddCommand(NewConfigPluginListCmd(),
root.AddCommand(NewConfigPluginListCmd(pluginOrg, pluginRepo),
NewConfigPluginFetchCmd(pluginOrg, pluginRepo),
NewConfigPluginInstallCmd(pluginOrg, pluginRepo),
NewConfigPluginUninstallCmd())
NewConfigPluginUninstallCmd(pluginOrg, pluginRepo))
}
25 changes: 22 additions & 3 deletions pkg/cmd/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
type (
configPluginListCmd struct {
cobra_ext.OutputOption
PluginOrg string
PluginRepo string
PluginRepoName string
}

jcliPluginFetchCmd struct {
Expand All @@ -27,15 +30,31 @@ type (
jcliPluginInstallCmd struct {
RoundTripper http.RoundTripper
ShowProgress bool
Thread int

output io.Writer
PluginOrg string
PluginRepo string
output io.Writer
PluginOrg string
PluginRepo string
PluginRepoName string
}

jcliPluginUninstallCmd struct {
RoundTripper http.RoundTripper
ShowProgress bool

output io.Writer
PluginOrg string
PluginRepo string
PluginRepoName string
}

jcliPluginUpdateCmd struct {
RoundTripper http.RoundTripper
ShowProgress bool

output io.Writer
PluginOrg string
PluginRepo string
PluginRepoName string
}
)
16 changes: 11 additions & 5 deletions pkg/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import (
)

// NewConfigPluginUninstallCmd create a command to uninstall a plugin
func NewConfigPluginUninstallCmd() (cmd *cobra.Command) {
jcliPluginUninstallCmd := jcliPluginUninstallCmd{}
func NewConfigPluginUninstallCmd(pluginOrg, pluginRepo string) (cmd *cobra.Command) {
jcliPluginUninstallCmd := jcliPluginUninstallCmd{
PluginOrg: pluginOrg,
PluginRepo: pluginRepo,
PluginRepoName: pluginRepo,
}

cmd = &cobra.Command{
Use: "uninstall",
Short: "Remove a plugin",
Long: "Remove a plugin",
Args: cobra.MinimumNArgs(1),
RunE: jcliPluginUninstallCmd.RunE,
ValidArgsFunction: ValidPluginNames,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
return ValidPluginNames(cmd, args, toComplete, pluginOrg, pluginRepo)
},
}
return
}
Expand All @@ -32,13 +38,13 @@ func (c *jcliPluginUninstallCmd) RunE(cmd *cobra.Command, args []string) (err er
}

name := args[0]
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, name, false)
cachedMetadataFile := pkg.GetJCLIPluginPath(userHome, c.PluginRepoName, name, false)

var data []byte
if data, err = ioutil.ReadFile(cachedMetadataFile); err == nil {
plugin := &pkg.Plugin{}
if err = yaml.Unmarshal(data, plugin); err == nil {
mainFile := pkg.GetJCLIPluginPath(userHome, plugin.Main, true)
mainFile := pkg.GetJCLIPluginPath(userHome, c.PluginRepoName, plugin.Main, true)

os.Remove(cachedMetadataFile)
os.Remove(mainFile)
Expand Down
18 changes: 9 additions & 9 deletions pkg/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ type PluginError struct {
code int
}

func FindPlugins() (plugins []Plugin, err error) {
func FindPlugins(pluginOrg, pluginRepo string) (plugins []Plugin, err error) {
var userHome string
if userHome, err = homedir.Dir(); err != nil {
return
}

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

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

// LoadPlugins loads the plugins
func LoadPlugins(cmd *cobra.Command) {
func LoadPlugins(cmd *cobra.Command, pluginOrg, pluginRepo string) {
var plugins []Plugin
var err error
if plugins, err = FindPlugins(); err != nil {
if plugins, err = FindPlugins(pluginOrg, pluginRepo); err != nil {
cmd.PrintErrln("Cannot load plugins successfully")
return
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func LoadPlugins(cmd *cobra.Command) {
return
}

pluginExec := GetJCLIPluginPath(userHome, cmd.Annotations["main"], true)
pluginExec := GetJCLIPluginPath(userHome, pluginRepo, cmd.Annotations["main"], true)
err = callPluginExecutable(cmd, pluginExec, args, cmd.OutOrStdout())
return
},
Expand All @@ -127,11 +127,11 @@ func LoadPlugins(cmd *cobra.Command) {
}
}

//GetJCLIPluginPath returns the path of a jcli plugin
func GetJCLIPluginPath(userHome, name string, binary bool) string {
// GetJCLIPluginPath returns the path of a jcli plugin
func GetJCLIPluginPath(userHome, pluginRepoName, name string, binary bool) string {
suffix := ".yaml"
if binary {
suffix = ""
}
return fmt.Sprintf("%s/.jenkins-cli/plugins/%s%s", userHome, name, suffix)
return fmt.Sprintf("%s/.%s/plugins-repo/%s%s", userHome, pluginRepoName, name, suffix)
}
Loading

0 comments on commit 436d938

Please sign in to comment.