diff --git a/app/cmd/plugin.go b/app/cmd/plugin.go index 651e69382fbd5291356c55397b483f7a1ffabb53..fe5898209cd6fb96e2504027a24f1ed4813fcd77 100644 --- a/app/cmd/plugin.go +++ b/app/cmd/plugin.go @@ -1,174 +1,24 @@ package cmd import ( - "fmt" - "io/ioutil" - "log" - "net/http" - "os" - "strings" - - "github.com/linuxsuren/jenkins-cli/client" - "github.com/linuxsuren/jenkins-cli/util" "github.com/spf13/cobra" ) // PluginOptions contains the command line options type PluginOptions struct { - OutputOption - - Upload bool - CheckUpdate bool - Open bool - List bool - - Install []string - Uninstall string - - Filter []string } +var pluginOpt PluginOptions + func init() { rootCmd.AddCommand(pluginCmd) - pluginCmd.Flags().BoolVarP(&pluginOpt.Upload, "upload", "u", false, "Upload plugin to your Jenkins server") - pluginCmd.Flags().BoolVarP(&pluginOpt.CheckUpdate, "check", "c", false, "Checkout update center server") - pluginCmd.Flags().BoolVarP(&pluginOpt.Open, "open", "o", false, "Open the browse with the address of plugin manager") - pluginCmd.Flags().BoolVarP(&pluginOpt.List, "list", "l", false, "Print all the plugins which are installed") - pluginCmd.Flags().StringVarP(&pluginOpt.Format, "format", "", TableOutputFormat, "Format the output") - pluginCmd.Flags().StringVarP(&pluginOpt.Uninstall, "uninstall", "", "", "Uninstall a plugin by shortName") - pluginCmd.Flags().StringArrayVarP(&pluginOpt.Filter, "filter", "", []string{}, "Filter for the list, like: active, hasUpdate, downgradable, enable, name=foo") } -var pluginOpt PluginOptions - var pluginCmd = &cobra.Command{ Use: "plugin", Short: "Manage the plugins of Jenkins", Long: `Manage the plugins of Jenkins`, Run: func(cmd *cobra.Command, args []string) { - jenkins := getCurrentJenkins() - jclient := &client.PluginManager{} - jclient.URL = jenkins.URL - jclient.UserName = jenkins.UserName - jclient.Token = jenkins.Token - jclient.Proxy = jenkins.Proxy - jclient.ProxyAuth = jenkins.ProxyAuth - - if pluginOpt.Upload { - jclient.Upload() - } - - if pluginOpt.CheckUpdate { - jclient.CheckUpdate(func(response *http.Response) { - code := response.StatusCode - if code == 200 { - fmt.Println("update site updated.") - } else { - contentData, _ := ioutil.ReadAll(response.Body) - log.Fatal(fmt.Sprintf("response code is %d, content: %s", - code, string(contentData))) - } - }) - } - - if pluginOpt.Open { - if jenkins.URL != "" { - open(fmt.Sprintf("%s/pluginManager", jenkins.URL)) - } else { - log.Fatal(fmt.Sprintf("No URL fond from %s", jenkins.Name)) - } - } - - if pluginOpt.List { - var ( - filter bool - hasUpdate bool - downgradable bool - enable bool - active bool - pluginName string - ) - if pluginOpt.Filter != nil { - filter = true - for _, f := range pluginOpt.Filter { - switch f { - case "hasUpdate": - hasUpdate = true - case "downgradable": - downgradable = true - case "enable": - enable = true - case "active": - active = true - case "name": - downgradable = true - } - - if strings.HasPrefix(f, "name=") { - pluginName = strings.TrimPrefix(f, "name=") - } - } - } - - if plugins, err := jclient.GetPlugins(); err == nil { - filteredPlugins := make([]client.InstalledPlugin, 0) - for _, plugin := range plugins.Plugins { - if filter { - if hasUpdate && !plugin.HasUpdate { - continue - } - - if downgradable && !plugin.Downgradable { - continue - } - - if enable && !plugin.Enable { - continue - } - - if active && !plugin.Active { - continue - } - - if pluginName != "" && !strings.Contains(plugin.ShortName, pluginName) { - continue - } - - filteredPlugins = append(filteredPlugins, plugin) - } - } - - if data, err := pluginOpt.Output(filteredPlugins); err == nil { - if len(data) > 0 { - fmt.Println(string(data)) - } - } else { - log.Fatal(err) - } - } else { - log.Fatal(err) - } - } - - if pluginOpt.Uninstall != "" { - if err := jclient.UninstallPlugin(pluginOpt.Uninstall); err != nil { - log.Fatal(err) - } - } + cmd.Help() }, } - -func (o *PluginOptions) Output(obj interface{}) (data []byte, err error) { - if data, err = o.OutputOption.Output(obj); err != nil { - pluginList := obj.([]client.InstalledPlugin) - table := util.CreateTable(os.Stdout) - table.AddRow("number", "name", "version", "update") - for i, plugin := range pluginList { - table.AddRow(fmt.Sprintf("%d", i), plugin.ShortName, plugin.Version, fmt.Sprintf("%v", plugin.HasUpdate)) - } - table.Render() - err = nil - data = []byte{} - } - return -} diff --git a/app/cmd/plugin_check.go b/app/cmd/plugin_check.go new file mode 100644 index 0000000000000000000000000000000000000000..5d54d855840af48a7ab8bee814265093c00ffc71 --- /dev/null +++ b/app/cmd/plugin_check.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + + "github.com/linuxsuren/jenkins-cli/client" + "github.com/spf13/cobra" +) + +func init() { + pluginCmd.AddCommand(pluginCheckCmd) +} + +var pluginCheckCmd = &cobra.Command{ + Use: "check", + Short: "Checkout update center server", + Long: `Checkout update center server`, + Run: func(cmd *cobra.Command, args []string) { + jenkins := getCurrentJenkins() + jclient := &client.PluginManager{} + jclient.URL = jenkins.URL + jclient.UserName = jenkins.UserName + jclient.Token = jenkins.Token + jclient.Proxy = jenkins.Proxy + jclient.ProxyAuth = jenkins.ProxyAuth + + jclient.CheckUpdate(func(response *http.Response) { + code := response.StatusCode + if code == 200 { + fmt.Println("update site updated.") + } else { + contentData, _ := ioutil.ReadAll(response.Body) + log.Fatal(fmt.Sprintf("response code is %d, content: %s", + code, string(contentData))) + } + }) + }, +} diff --git a/app/cmd/plugin_list.go b/app/cmd/plugin_list.go new file mode 100644 index 0000000000000000000000000000000000000000..bccae9a6e952d0523ccc459d7ab7f2bf5337fcfb --- /dev/null +++ b/app/cmd/plugin_list.go @@ -0,0 +1,124 @@ +package cmd + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/linuxsuren/jenkins-cli/client" + "github.com/linuxsuren/jenkins-cli/util" + "github.com/spf13/cobra" +) + +type PluginListOption struct { + OutputOption + + Filter []string +} + +var pluginListOption PluginListOption + +func init() { + pluginCmd.AddCommand(pluginListCmd) + pluginListCmd.Flags().StringArrayVarP(&pluginListOption.Filter, "filter", "", []string{}, "Filter for the list, like: active, hasUpdate, downgradable, enable, name=foo") +} + +var pluginListCmd = &cobra.Command{ + Use: "list", + Short: "Print all the plugins which are installed", + Long: `Print all the plugins which are installed`, + Run: func(cmd *cobra.Command, args []string) { + jenkins := getCurrentJenkins() + jclient := &client.PluginManager{} + jclient.URL = jenkins.URL + jclient.UserName = jenkins.UserName + jclient.Token = jenkins.Token + jclient.Proxy = jenkins.Proxy + jclient.ProxyAuth = jenkins.ProxyAuth + + var ( + filter bool + hasUpdate bool + downgradable bool + enable bool + active bool + pluginName string + ) + if pluginListOption.Filter != nil { + filter = true + for _, f := range pluginListOption.Filter { + switch f { + case "hasUpdate": + hasUpdate = true + case "downgradable": + downgradable = true + case "enable": + enable = true + case "active": + active = true + case "name": + downgradable = true + } + + if strings.HasPrefix(f, "name=") { + pluginName = strings.TrimPrefix(f, "name=") + } + } + } + + if plugins, err := jclient.GetPlugins(); err == nil { + filteredPlugins := make([]client.InstalledPlugin, 0) + for _, plugin := range plugins.Plugins { + if filter { + if hasUpdate && !plugin.HasUpdate { + continue + } + + if downgradable && !plugin.Downgradable { + continue + } + + if enable && !plugin.Enable { + continue + } + + if active && !plugin.Active { + continue + } + + if pluginName != "" && !strings.Contains(plugin.ShortName, pluginName) { + continue + } + + filteredPlugins = append(filteredPlugins, plugin) + } + } + + if data, err := pluginListOption.Output(filteredPlugins); err == nil { + if len(data) > 0 { + fmt.Println(string(data)) + } + } else { + log.Fatal(err) + } + } else { + log.Fatal(err) + } + }, +} + +func (o *PluginListOption) Output(obj interface{}) (data []byte, err error) { + if data, err = o.OutputOption.Output(obj); err != nil { + pluginList := obj.([]client.InstalledPlugin) + table := util.CreateTable(os.Stdout) + table.AddRow("number", "name", "version", "update") + for i, plugin := range pluginList { + table.AddRow(fmt.Sprintf("%d", i), plugin.ShortName, plugin.Version, fmt.Sprintf("%v", plugin.HasUpdate)) + } + table.Render() + err = nil + data = []byte{} + } + return +} diff --git a/app/cmd/plugin_open.go b/app/cmd/plugin_open.go new file mode 100644 index 0000000000000000000000000000000000000000..41ea2c845489a32b9c0e160eb0d54142b9505c42 --- /dev/null +++ b/app/cmd/plugin_open.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "fmt" + "log" + + "github.com/spf13/cobra" +) + +func init() { + pluginCmd.AddCommand(pluginOpenCmd) +} + +var pluginOpenCmd = &cobra.Command{ + Use: "open", + Short: "Openout update center server", + Long: `Openout update center server`, + Run: func(cmd *cobra.Command, args []string) { + jenkins := getCurrentJenkins() + + if jenkins.URL != "" { + open(fmt.Sprintf("%s/pluginManager", jenkins.URL)) + } else { + log.Fatal(fmt.Sprintf("No URL fond from %s", jenkins.Name)) + } + }, +} diff --git a/app/cmd/plugin_uninstall.go b/app/cmd/plugin_uninstall.go new file mode 100644 index 0000000000000000000000000000000000000000..7e999399054e55061b0e67bc39e2b177b4d8e052 --- /dev/null +++ b/app/cmd/plugin_uninstall.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "log" + + "github.com/linuxsuren/jenkins-cli/client" + "github.com/spf13/cobra" +) + +func init() { + pluginCmd.AddCommand(pluginUninstallCmd) +} + +var pluginUninstallCmd = &cobra.Command{ + Use: "uninstall [pluginName]", + Short: "Uninstall the plugins", + Long: `Uninstall the plugins`, + Run: func(cmd *cobra.Command, args []string) { + var pluginName string + if len(args) == 0 { + cmd.Help() + return + } + + pluginName = args[0] + + jenkins := getCurrentJenkins() + jclient := &client.PluginManager{} + jclient.URL = jenkins.URL + jclient.UserName = jenkins.UserName + jclient.Token = jenkins.Token + jclient.Proxy = jenkins.Proxy + jclient.ProxyAuth = jenkins.ProxyAuth + + if err := jclient.UninstallPlugin(pluginName); err != nil { + log.Fatal(err) + } + }, +} diff --git a/app/cmd/plugin_upload.go b/app/cmd/plugin_upload.go new file mode 100644 index 0000000000000000000000000000000000000000..a1365c502e5e589778140cefb92545548a26d4c0 --- /dev/null +++ b/app/cmd/plugin_upload.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "github.com/linuxsuren/jenkins-cli/client" + "github.com/spf13/cobra" +) + +func init() { + pluginCmd.AddCommand(pluginUploadCmd) +} + +var pluginUploadCmd = &cobra.Command{ + Use: "upload", + Short: "Upload the plugin from local to your Jenkins", + Long: `Upload the plugin from local to your Jenkins`, + Run: func(cmd *cobra.Command, args []string) { + jenkins := getCurrentJenkins() + jclient := &client.PluginManager{} + jclient.URL = jenkins.URL + jclient.UserName = jenkins.UserName + jclient.Token = jenkins.Token + jclient.Proxy = jenkins.Proxy + jclient.ProxyAuth = jenkins.ProxyAuth + + jclient.Upload() + }, +}