diff --git a/app/cmd/plugin.go b/app/cmd/plugin.go index cc7e1739e0a53556520a25da94a9300e285f7496..bf396389406152b1dc4ef9baa260f126c56c15f6 100644 --- a/app/cmd/plugin.go +++ b/app/cmd/plugin.go @@ -24,6 +24,9 @@ type PluginOptions struct { Upload bool CheckUpdate bool Open bool + List bool + + Uninstall string } func init() { @@ -31,6 +34,8 @@ func init() { pluginCmd.PersistentFlags().BoolVarP(&pluginOpt.Upload, "upload", "u", false, "Upload plugin to your Jenkins server") pluginCmd.PersistentFlags().BoolVarP(&pluginOpt.CheckUpdate, "check", "c", false, "Checkout update center server") pluginCmd.PersistentFlags().BoolVarP(&pluginOpt.Open, "open", "o", false, "Open the browse with the address of plugin manager") + pluginCmd.PersistentFlags().BoolVarP(&pluginOpt.List, "list", "l", false, "Print all the plugins which are installed") + pluginCmd.PersistentFlags().StringVarP(&pluginOpt.Uninstall, "uninstall", "", "", "Uninstall a plugin by shortName") viper.BindPFlag("upload", pluginCmd.PersistentFlags().Lookup("upload")) } @@ -80,15 +85,13 @@ var pluginCmd = &cobra.Command{ } } + config := getCurrentJenkins() + jenkins := getCurrentJenkins() + jclient := &client.PluginManager{} + jclient.URL = jenkins.URL + jclient.UserName = config.UserName + jclient.Token = config.Token if pluginOpt.CheckUpdate { - jclient := &client.PluginManager{} - - crumb, config := getCrumb() - jclient.CrumbRequestField = crumb.CrumbRequestField - jclient.Crumb = crumb.Crumb - jclient.URL = config.URL - jclient.UserName = config.UserName - jclient.Token = config.Token jclient.CheckUpdate(func(response *http.Response) { code := response.StatusCode if code == 200 { @@ -102,13 +105,28 @@ var pluginCmd = &cobra.Command{ } if pluginOpt.Open { - jenkins := getCurrentJenkins() 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 { + if plugins, err := jclient.GetPlugins(); err == nil { + for i, plugin := range plugins.Plugins { + fmt.Println("num:", i, "name:", plugin.ShortName, "version:", plugin.Version) + } + } else { + log.Fatal(err) + } + } + + if pluginOpt.Uninstall != "" { + if err := jclient.UninstallPlugin(pluginOpt.Uninstall); err != nil { + log.Fatal(err) + } + } }, } diff --git a/client/pluginManger.go b/client/pluginManger.go index d5683c2cb4c1182df05772108a11ffa369f7594c..8e6ad2ffbeb74baf67d3b73aa8dc7a0104d36d2f 100644 --- a/client/pluginManger.go +++ b/client/pluginManger.go @@ -2,7 +2,9 @@ package client import ( "crypto/tls" + "encoding/json" "fmt" + "io/ioutil" "log" "net/http" ) @@ -11,6 +13,30 @@ type PluginManager struct { JenkinsCore } +// PluginList represent a list of plugins +type PluginList struct { + Plugins []Plugin +} + +// Plugin represent the plugin from Jenkins +type Plugin struct { + Active bool + ShortName string + LongName string + Version string + URL string + HasUpdate bool + Enable bool + Downgradable bool + Pinned bool + RequiredCoreVesion string + MinimumJavaVersion string + SupportDynamicLoad string + Deleted bool + Bundled bool + BackVersion string +} + // CheckUpdate fetch the lastest plugins from update center site func (p *PluginManager) CheckUpdate(handle func(*http.Response)) { api := fmt.Sprintf("%s/pluginManager/checkUpdatesServer", p.URL) @@ -36,6 +62,80 @@ func (p *PluginManager) CheckUpdate(handle func(*http.Response)) { } } +func (p *PluginManager) GetPlugins() (pluginList *PluginList, err error) { + api := fmt.Sprintf("%s/pluginManager/api/json?pretty=true&depth=1", p.URL) + var ( + req *http.Request + response *http.Response + ) + + req, err = http.NewRequest("GET", api, nil) + if err == nil { + p.AuthHandle(req) + } else { + return + } + + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client := &http.Client{Transport: tr} + if response, err = client.Do(req); err == nil { + code := response.StatusCode + var data []byte + data, err = ioutil.ReadAll(response.Body) + if code == 200 { + if err == nil { + pluginList = &PluginList{} + err = json.Unmarshal(data, pluginList) + } + } else { + log.Fatal(string(data)) + } + } else { + log.Fatal(err) + } + return +} + +// UninstallPlugin uninstall a plugin by name +func (p *PluginManager) UninstallPlugin(name string) (err error) { + api := fmt.Sprintf("%s/pluginManager/plugin/%s/uninstall", p.URL, name) + var ( + req *http.Request + response *http.Response + ) + + req, err = http.NewRequest("POST", api, nil) + if err == nil { + p.AuthHandle(req) + } else { + return + } + + if err = p.CrumbHandle(req); err != nil { + return + } + + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client := &http.Client{Transport: tr} + if response, err = client.Do(req); err == nil { + code := response.StatusCode + var data []byte + data, err = ioutil.ReadAll(response.Body) + if code == 200 { + fmt.Println("uninstall succeed.") + } else { + log.Fatal(string(data)) + } + } else { + log.Fatal(err) + } + return +} + func (p *PluginManager) handleCheck(handle func(*http.Response)) func(*http.Response) { if handle == nil { handle = func(*http.Response) {}