未验证 提交 2190600b 编写于 作者: Z Zhao Xiaojie 提交者: GitHub

Merge pull request #56 from LinuxSuRen/fea/install-plugins

Allow user to search plugins when install
......@@ -35,7 +35,6 @@ func init() {
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().StringArrayVarP(&pluginOpt.Install, "install", "", []string{}, "Install a plugin by shortName")
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")
}
......@@ -151,12 +150,6 @@ var pluginCmd = &cobra.Command{
}
}
if pluginOpt.Install != nil && len(pluginOpt.Install) > 0 {
if err := jclient.InstallPlugin(pluginOpt.Install); err != nil {
log.Fatal(err)
}
}
if pluginOpt.Uninstall != "" {
if err := jclient.UninstallPlugin(pluginOpt.Uninstall); err != nil {
log.Fatal(err)
......
package cmd
import (
"fmt"
"log"
"github.com/AlecAivazis/survey"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
func init() {
pluginCmd.AddCommand(pluginInstallCmd)
}
var pluginInstallCmd = &cobra.Command{
Use: "install [pluginName]",
Short: "Install the plugins",
Long: `Install the plugins`,
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
plugins := make([]string, len(args))
plugins = append(plugins, args...)
if len(plugins) == 0 {
for {
var keyword string
prompt := &survey.Input{Message: "Please input the keyword to search your plugin!"}
if err := survey.AskOne(prompt, &keyword); err != nil {
log.Fatal(err)
}
if availablePlugins, err := jclient.GetAvailablePlugins(); err == nil {
matchedPlugins := searchPlugins(availablePlugins, keyword)
optinalPlugins := convertToArray(matchedPlugins)
if len(optinalPlugins) == 0 {
fmt.Println("Cannot find any plugins by your keyword, or they already installed.")
continue
}
prompt := &survey.MultiSelect{
Message: "Please select the plugins whose you want to install:",
Options: convertToArray(matchedPlugins),
}
selectedPlugins := []string{}
survey.AskOne(prompt, &selectedPlugins)
plugins = append(plugins, selectedPlugins...)
break
} else {
log.Fatal(err)
}
}
fmt.Println("Going to install", plugins)
}
jclient.InstallPlugin(plugins)
},
}
func convertToArray(aviablePlugins []client.AvailablePlugin) (plugins []string) {
plugins = make([]string, 0)
for _, plugin := range aviablePlugins {
if plugin.Installed {
continue
}
plugins = append(plugins, plugin.Name)
}
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册