未验证 提交 6afcbfc9 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Add support to open external link (#384)

* Add support to open external link

* Enable to print all details of a config item

* Fix the config generate tests
上级 17c47455
...@@ -18,12 +18,18 @@ import ( ...@@ -18,12 +18,18 @@ import (
// ConfigOptions is the config cmd option // ConfigOptions is the config cmd option
type ConfigOptions struct { type ConfigOptions struct {
ConfigFileLocation string ConfigFileLocation string
Detail bool
} }
var configOptions ConfigOptions var configOptions ConfigOptions
func init() { func init() {
rootCmd.AddCommand(configCmd) rootCmd.AddCommand(configCmd)
// add flags
flags := configCmd.Flags()
flags.BoolVarP(&configOptions.Detail, "detail", "", false,
`Show the all detail of current configuration`)
} }
var configCmd = &cobra.Command{ var configCmd = &cobra.Command{
...@@ -36,7 +42,12 @@ var configCmd = &cobra.Command{ ...@@ -36,7 +42,12 @@ var configCmd = &cobra.Command{
if current == nil { if current == nil {
err = fmt.Errorf("no config file found or no current setting") err = fmt.Errorf("no config file found or no current setting")
} else { } else {
if current.Description != "" { if configOptions.Detail {
var data []byte
if data, err = yaml.Marshal(current); err == nil {
cmd.Println(string(data))
}
} else if current.Description != "" {
cmd.Printf("Current Jenkins's name is %s, url is %s, description is %s\n", current.Name, current.URL, current.Description) cmd.Printf("Current Jenkins's name is %s, url is %s, description is %s\n", current.Name, current.URL, current.Description)
} else { } else {
cmd.Printf("Current Jenkins's name is %s, url is %s\n", current.Name, current.URL) cmd.Printf("Current Jenkins's name is %s, url is %s\n", current.Name, current.URL)
...@@ -51,14 +62,15 @@ var configCmd = &cobra.Command{ ...@@ -51,14 +62,15 @@ var configCmd = &cobra.Command{
// JenkinsServer holds the configuration of your Jenkins // JenkinsServer holds the configuration of your Jenkins
type JenkinsServer struct { type JenkinsServer struct {
Name string `yaml:"name"` Name string `yaml:"name"`
URL string `yaml:"url"` URL string `yaml:"url"`
UserName string `yaml:"username"` UserName string `yaml:"username"`
Token string `yaml:"token"` Token string `yaml:"token"`
Proxy string `yaml:"proxy"` Proxy string `yaml:"proxy,omitempty"`
ProxyAuth string `yaml:"proxyAuth"` ProxyAuth string `yaml:"proxyAuth,omitempty"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify"` InsecureSkipVerify bool `yaml:"insecureSkipVerify"`
Description string `yaml:"description"` Description string `yaml:"description,omitempty"`
Data map[string]string `yaml:"data,omitempty"`
} }
// CommandHook is a hook // CommandHook is a hook
...@@ -83,11 +95,11 @@ type JenkinsMirror struct { ...@@ -83,11 +95,11 @@ type JenkinsMirror struct {
// Config is a global config struct // Config is a global config struct
type Config struct { type Config struct {
Current string `yaml:"current"` Current string `yaml:"current"`
Language string `yaml:"language"` Language string `yaml:"language,omitempty"`
JenkinsServers []JenkinsServer `yaml:"jenkins_servers"` JenkinsServers []JenkinsServer `yaml:"jenkins_servers"`
PreHooks []CommandHook `yaml:"preHooks"` PreHooks []CommandHook `yaml:"preHooks,omitempty"`
PostHooks []CommandHook `yaml:"postHooks"` PostHooks []CommandHook `yaml:"postHooks,omitempty"`
PluginSuites []PluginSuite `yaml:"pluginSuites"` PluginSuites []PluginSuite `yaml:"pluginSuites,omitempty"`
Mirrors []JenkinsMirror `yaml:"mirrors"` Mirrors []JenkinsMirror `yaml:"mirrors"`
} }
......
...@@ -37,19 +37,12 @@ var _ = Describe("config generate command", func() { ...@@ -37,19 +37,12 @@ var _ = Describe("config generate command", func() {
It("should success", func() { It("should success", func() {
Expect(cmdErr).To(BeNil()) Expect(cmdErr).To(BeNil())
Expect(buf.String()).To(Equal(`current: yourServer Expect(buf.String()).To(Equal(`current: yourServer
language: ""
jenkins_servers: jenkins_servers:
- name: yourServer - name: yourServer
url: http://localhost:8080/jenkins url: http://localhost:8080/jenkins
username: admin username: admin
token: 111e3a2f0231198855dceaff96f20540a9 token: 111e3a2f0231198855dceaff96f20540a9
proxy: ""
proxyAuth: ""
insecureSkipVerify: true insecureSkipVerify: true
description: ""
preHooks: []
postHooks: []
pluginSuites: []
mirrors: mirrors:
- name: default - name: default
url: http://mirrors.jenkins.io/ url: http://mirrors.jenkins.io/
......
...@@ -5,7 +5,9 @@ import ( ...@@ -5,7 +5,9 @@ import (
"github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.uber.org/zap"
"os" "os"
"strings"
) )
// OpenOption is the open cmd option // OpenOption is the open cmd option
...@@ -47,38 +49,60 @@ var openCmd = &cobra.Command{ ...@@ -47,38 +49,60 @@ var openCmd = &cobra.Command{
openOption.Browser = os.Getenv("BROWSER") openOption.Browser = os.Getenv("BROWSER")
} }
}, },
RunE: func(_ *cobra.Command, args []string) (err error) { RunE: openOption.run,
var jenkins *JenkinsServer }
var configName string func (o *OpenOption) run(_ *cobra.Command, args []string) (err error) {
if len(args) > 0 { var jenkins *JenkinsServer
configName = args[0]
}
if configName == "" && openOption.Interactive { var configName string
jenkinsNames := getJenkinsNames() if len(args) > 0 {
configName, err = openOption.Select(jenkinsNames, configName = args[0]
i18n.T("Choose a Jenkins which you want to open:"), "") }
}
if err == nil { if configName == "" && openOption.Interactive {
if configName != "" { jenkinsNames := getJenkinsNames()
jenkins = findJenkinsByName(configName) configName, err = openOption.Select(jenkinsNames,
} else { i18n.T("Choose a Jenkins which you want to open:"), "")
jenkins = getCurrentJenkins() }
}
if jenkins != nil && jenkins.URL != "" { jenkinsName, external := o.parseName(configName)
url := jenkins.URL logger.Info("open jenkins",
if openOption.Config { zap.String("jenkins name", jenkinsName),
url = fmt.Sprintf("%s/configure", url) zap.String("external", external))
}
browser := openOption.Browser if err == nil {
err = util.Open(url, browser, openOption.ExecContext) if jenkinsName != "" {
} else { jenkins = findJenkinsByName(jenkinsName)
err = fmt.Errorf("no URL found with Jenkins %s", configName) } else {
jenkins = getCurrentJenkins()
}
if jenkins != nil && jenkins.URL != "" {
url := jenkins.URL
if openOption.Config {
url = fmt.Sprintf("%s/configure", url)
} else if external != "" {
url = jenkins.Data[external]
} }
browser := openOption.Browser
err = util.Open(url, browser, openOption.ExecContext)
} else {
err = fmt.Errorf("no URL found with Jenkins %s", jenkinsName)
} }
return }
}, return
}
// parseName the string expect likes name or name.external
func (o *OpenOption) parseName(configName string) (jenkins, external string) {
array := strings.SplitN(configName, ".", 2)
fmt.Println(array)
if len(array) > 0 {
jenkins = array[0]
}
if len(array) > 1 {
external = array[1]
}
return
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册