未验证 提交 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)
...@@ -55,10 +66,11 @@ type JenkinsServer struct { ...@@ -55,10 +66,11 @@ type JenkinsServer struct {
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,7 +49,10 @@ var openCmd = &cobra.Command{ ...@@ -47,7 +49,10 @@ 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,
}
func (o *OpenOption) run(_ *cobra.Command, args []string) (err error) {
var jenkins *JenkinsServer var jenkins *JenkinsServer
var configName string var configName string
...@@ -61,9 +66,14 @@ var openCmd = &cobra.Command{ ...@@ -61,9 +66,14 @@ var openCmd = &cobra.Command{
i18n.T("Choose a Jenkins which you want to open:"), "") i18n.T("Choose a Jenkins which you want to open:"), "")
} }
jenkinsName, external := o.parseName(configName)
logger.Info("open jenkins",
zap.String("jenkins name", jenkinsName),
zap.String("external", external))
if err == nil { if err == nil {
if configName != "" { if jenkinsName != "" {
jenkins = findJenkinsByName(configName) jenkins = findJenkinsByName(jenkinsName)
} else { } else {
jenkins = getCurrentJenkins() jenkins = getCurrentJenkins()
} }
...@@ -72,13 +82,27 @@ var openCmd = &cobra.Command{ ...@@ -72,13 +82,27 @@ var openCmd = &cobra.Command{
url := jenkins.URL url := jenkins.URL
if openOption.Config { if openOption.Config {
url = fmt.Sprintf("%s/configure", url) url = fmt.Sprintf("%s/configure", url)
} else if external != "" {
url = jenkins.Data[external]
} }
browser := openOption.Browser browser := openOption.Browser
err = util.Open(url, browser, openOption.ExecContext) err = util.Open(url, browser, openOption.ExecContext)
} else { } else {
err = fmt.Errorf("no URL found with Jenkins %s", configName) 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.
先完成此消息的编辑!
想要评论请 注册