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

Add support take url from cmd line options (#291)

* Add support take url from cmd line options

* Add test cases for executePostCmd
上级 f44ffd5f
......@@ -263,6 +263,7 @@ func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *JenkinsSe
jClient.Token = jenkins.Token
jClient.Proxy = jenkins.Proxy
jClient.ProxyAuth = jenkins.ProxyAuth
jClient.InsecureSkipVerify = jenkins.InsecureSkipVerify
}
return
}
......
......@@ -2,6 +2,7 @@ package cmd
import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
)
......@@ -15,3 +16,13 @@ var computerCmd = &cobra.Command{
Short: i18n.T("Manage the computers of your Jenkins"),
Long: i18n.T(`Manage the computers of your Jenkins`),
}
// GetComputerClient returns the client of computer
func GetComputerClient(option CommonOption) (*client.ComputerClient, *JenkinsServer) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: option.RoundTripper,
},
}
return jClient, getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
}
package cmd
import (
"net/http"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra"
......@@ -12,9 +8,8 @@ import (
// ComputerCreateOption option for config list command
type ComputerCreateOption struct {
CommonOption
OutputOption
RoundTripper http.RoundTripper
}
var computerCreateOption ComputerCreateOption
......@@ -31,15 +26,8 @@ It can only create a JNLP agent.`),
Example: `jcli agent create agent-name`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: computerCreateOption.RoundTripper,
},
}
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
err = jClient.Create(args[0])
return
jClient, _ := GetComputerClient(computerCreateOption.CommonOption)
return jClient.Create(args[0])
},
Annotations: map[string]string{
since: "v0.0.24",
......
package cmd
import (
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra"
......@@ -27,15 +25,8 @@ var computerDeleteCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Example: `jcli agent delete agent-name`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: computerDeleteOption.RoundTripper,
},
}
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
err = jClient.Delete(args[0])
return
jClient, _ := GetComputerClient(computerDeleteOption.CommonOption)
return jClient.Delete(args[0])
},
Annotations: map[string]string{
since: "v0.0.24",
......
......@@ -45,6 +45,9 @@ var computerLaunchCmd = &cobra.Command{
Example: `jcli agent launch agent-name
jcli agent launch agent-name --type jnlp`,
PreRunE: func(_ *cobra.Command, args []string) (err error) {
computerLaunchOption.ComputerClient, computerLaunchOption.CurrentJenkins =
GetComputerClient(computerLaunchOption.CommonOption)
if computerLaunchOption.Type != "jnlp" {
return
}
......@@ -64,15 +67,6 @@ jcli agent launch agent-name --type jnlp`,
},
RunE: func(_ *cobra.Command, args []string) (err error) {
name := args[0]
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: computerLaunchOption.RoundTripper,
},
}
computerLaunchOption.ComputerClient = jClient
computerLaunchOption.CurrentJenkins = getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
switch computerLaunchOption.Type {
case "":
err = computerLaunchOption.Launch(name)
......
package cmd
import (
"net/http"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
......@@ -13,9 +11,8 @@ import (
// ComputerListOption option for config list command
type ComputerListOption struct {
CommonOption
OutputOption
RoundTripper http.RoundTripper
}
var computerListOption ComputerListOption
......@@ -30,12 +27,7 @@ var computerListCmd = &cobra.Command{
Short: i18n.T("List all Jenkins agents"),
Long: i18n.T("List all Jenkins agents"),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: computerListOption.RoundTripper,
},
}
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
jClient, _ := GetComputerClient(computerListOption.CommonOption)
var computers client.ComputerList
if computers, err = jClient.List(); err == nil {
......
package cmd
import (
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra"
......@@ -25,12 +23,7 @@ var computerLogCmd = &cobra.Command{
Long: i18n.T("Output the log of the agent"),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: computerLogOption.RoundTripper,
},
}
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
jClient, _ := GetComputerClient(computerLogOption.CommonOption)
var log string
if log, err = jClient.GetLog(args[0]); err == nil {
......
......@@ -51,17 +51,18 @@ var configCmd = &cobra.Command{
// JenkinsServer holds the configuration of your Jenkins
type JenkinsServer struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
UserName string `yaml:"username"`
Token string `yaml:"token"`
Proxy string `yaml:"proxy"`
ProxyAuth string `yaml:"proxyAuth"`
Description string `yaml:"description"`
}
// CommndHook is a hook
type CommndHook struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
UserName string `yaml:"username"`
Token string `yaml:"token"`
Proxy string `yaml:"proxy"`
ProxyAuth string `yaml:"proxyAuth"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify"`
Description string `yaml:"description"`
}
// CommandHook is a hook
type CommandHook struct {
Path string `yaml:"path"`
Command string `yaml:"cmd"`
}
......@@ -84,8 +85,8 @@ type Config struct {
Current string `yaml:"current"`
Language string `yaml:"language"`
JenkinsServers []JenkinsServer `yaml:"jenkins_servers"`
PreHooks []CommndHook `yaml:"preHooks"`
PostHooks []CommndHook `yaml:"postHooks"`
PreHooks []CommandHook `yaml:"preHooks"`
PostHooks []CommandHook `yaml:"postHooks"`
PluginSuites []PluginSuite `yaml:"pluginSuites"`
Mirrors []JenkinsMirror `yaml:"mirrors"`
}
......
......@@ -96,10 +96,11 @@ func getSampleConfig() (sampleConfig Config) {
Current: "yourServer",
JenkinsServers: []JenkinsServer{
{
Name: "yourServer",
URL: "http://localhost:8080/jenkins",
UserName: "admin",
Token: "111e3a2f0231198855dceaff96f20540a9",
Name: "yourServer",
URL: "http://localhost:8080/jenkins",
UserName: "admin",
Token: "111e3a2f0231198855dceaff96f20540a9",
InsecureSkipVerify: true,
},
},
Mirrors: []JenkinsMirror{
......
......@@ -45,6 +45,7 @@ jenkins_servers:
token: 111e3a2f0231198855dceaff96f20540a9
proxy: ""
proxyAuth: ""
insecureSkipVerify: true
description: ""
preHooks: []
postHooks: []
......
......@@ -30,6 +30,13 @@ type RootOptions struct {
Version bool
Debug bool
URL string
Username string
Token string
InsecureSkipVerify bool
Proxy string
ProxyAuth string
Doctor bool
LoggerLevel string
......@@ -71,9 +78,9 @@ More information could found at https://jenkins-zh.cn`,
}
}
// set Header Accept-Language
config = getConfig()
if config != nil {
// set Header Accept-Language
client.SetLanguage(config.Language)
}
......@@ -81,20 +88,21 @@ More information could found at https://jenkins-zh.cn`,
return
},
BashCompletionFunction: jcliBashCompletionFunc,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.Println(i18n.T("Jenkins CLI (jcli) manage your Jenkins"))
if rootOptions.Version {
cmd.Printf("Version: %s\n", app.GetVersion())
cmd.Printf("Commit: %s\n", app.GetCommit())
}
if rootOptions.Jenkins != "" {
current := getCurrentJenkinsFromOptionsOrDie()
current := getCurrentJenkinsFromOptions()
if current != nil {
cmd.Println("Current Jenkins is:", current.Name)
} else {
cmd.Println("Cannot found the configuration")
err = fmt.Errorf("cannot found the configuration: %s", rootOptions.Jenkins)
}
}
return
},
}
......@@ -137,6 +145,20 @@ func init() {
i18n.T("Run the diagnose for current command"))
rootCmd.Flags().BoolVarP(&rootOptions.Version, "version", "v", false,
i18n.T("Print the version of Jenkins CLI"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.URL, "url", "", "",
i18n.T("The URL of Jenkins"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.Username, "username", "", "",
i18n.T("The username of Jenkins"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.Token, "token", "", "",
i18n.T("The token of Jenkins"))
rootCmd.PersistentFlags().BoolVarP(&rootOptions.InsecureSkipVerify, "insecureSkipVerify", "", true,
i18n.T("If skip insecure skip verify"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.Proxy, "proxy", "", "",
i18n.T("The proxy of connection to Jenkins"))
rootCmd.PersistentFlags().StringVarP(&rootOptions.ProxyAuth, "proxy-auth", "", "",
i18n.T("The auth of proxy of connection to Jenkins"))
rootCmd.SetOut(os.Stdout)
}
......@@ -157,9 +179,29 @@ func getCurrentJenkinsFromOptions() (jenkinsServer *JenkinsServer) {
} else {
jenkinsServer = findJenkinsByName(jenkinsOpt)
}
// take URL from options if it's not empty
if jenkinsServer == nil && rootOptions.URL != "" {
jenkinsServer = &JenkinsServer{}
}
if jenkinsServer != nil {
if rootOptions.URL != "" {
jenkinsServer.URL = rootOptions.URL
}
if rootOptions.Username != "" {
jenkinsServer.UserName = rootOptions.Username
}
if rootOptions.Token != "" {
jenkinsServer.Token = rootOptions.Token
}
}
return
}
// Deprecated, please use getCurrentJenkinsFromOptions instead of it
func getCurrentJenkinsFromOptionsOrDie() (jenkinsServer *JenkinsServer) {
if jenkinsServer = getCurrentJenkinsFromOptions(); jenkinsServer == nil {
log.Fatal("Cannot found Jenkins by", rootOptions.Jenkins)
......@@ -204,7 +246,7 @@ func executePreCmd(cmd *cobra.Command, _ []string, writer io.Writer) (err error)
func executePostCmd(cmd *cobra.Command, _ []string, writer io.Writer) (err error) {
config := getConfig()
if config == nil {
err = fmt.Errorf("Cannot find config file")
err = fmt.Errorf("cannot find config file")
return
}
......
......@@ -2,11 +2,12 @@ package cmd
import (
"bytes"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"io/ioutil"
"os"
)
var _ = Describe("Root cmd test", func() {
......@@ -98,7 +99,7 @@ var _ = Describe("Root cmd test", func() {
It("basic use case with one preHook, should success", func() {
config = &Config{
PreHooks: []CommndHook{CommndHook{
PreHooks: []CommandHook{CommandHook{
Path: "test",
Command: successCmd,
}},
......@@ -118,13 +119,13 @@ var _ = Describe("Root cmd test", func() {
It("basic use case with many preHooks, should success", func() {
config = &Config{
PreHooks: []CommndHook{CommndHook{
PreHooks: []CommandHook{CommandHook{
Path: "test",
Command: successCmd,
}, CommndHook{
}, CommandHook{
Path: "test",
Command: "echo 2",
}, CommndHook{
}, CommandHook{
Path: "fake",
Command: successCmd,
}},
......@@ -159,7 +160,7 @@ var _ = Describe("Root cmd test", func() {
It("basic use case with error command, should success", func() {
config = &Config{
PreHooks: []CommndHook{CommndHook{
PreHooks: []CommandHook{CommandHook{
Path: "test",
Command: errorCmd,
}},
......@@ -176,6 +177,162 @@ var _ = Describe("Root cmd test", func() {
Expect(err).To(HaveOccurred())
})
})
Context("execute post cmd", func() {
It("should error", func() {
err := executePostCmd(nil, nil, nil)
Expect(err).To(HaveOccurred())
})
It("basic use case with one postHook, should success", func() {
config = &Config{
PostHooks: []CommandHook{CommandHook{
Path: "test",
Command: successCmd,
}},
}
rootCmd := &cobra.Command{}
subCmd := &cobra.Command{
Use: "test",
}
rootCmd.AddCommand(subCmd)
var buf bytes.Buffer
err := executePostCmd(subCmd, nil, &buf)
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal("1\n"))
})
It("basic use case with many postHooks, should success", func() {
config = &Config{
PostHooks: []CommandHook{CommandHook{
Path: "test",
Command: successCmd,
}, CommandHook{
Path: "test",
Command: "echo 2",
}, CommandHook{
Path: "fake",
Command: successCmd,
}},
}
rootCmd := &cobra.Command{}
subCmd := &cobra.Command{
Use: "test",
}
rootCmd.AddCommand(subCmd)
var buf bytes.Buffer
err := executePostCmd(subCmd, nil, &buf)
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal("1\n2\n"))
})
It("basic use case without postHooks, should success", func() {
config = &Config{}
rootCmd := &cobra.Command{}
subCmd := &cobra.Command{
Use: "test",
}
rootCmd.AddCommand(subCmd)
var buf bytes.Buffer
err := executePostCmd(subCmd, nil, &buf)
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal(""))
})
It("basic use case with error command, should success", func() {
config = &Config{
PostHooks: []CommandHook{CommandHook{
Path: "test",
Command: errorCmd,
}},
}
rootCmd := &cobra.Command{}
subCmd := &cobra.Command{
Use: "test",
}
rootCmd.AddCommand(subCmd)
var buf bytes.Buffer
err := executePostCmd(subCmd, nil, &buf)
Expect(err).To(HaveOccurred())
})
})
Context("basic root command test", func() {
var (
buf *bytes.Buffer
)
BeforeEach(func() {
rootOptions = RootOptions{}
buf = new(bytes.Buffer)
rootCmd.SetOut(buf)
})
It("should contain substring Version:", func() {
rootCmd.SetArgs([]string{"--version"})
_, err := rootCmd.ExecuteC()
Expect(err).NotTo(HaveOccurred())
Expect(buf.String()).To(ContainSubstring("Version:"))
})
It("with a fake jenkins as option", func() {
rootCmd.SetArgs([]string{"--jenkins", "fake"})
_, err := rootCmd.ExecuteC()
Expect(err).To(HaveOccurred())
Expect(buf.String()).To(ContainSubstring("cannot found the configuration:"))
})
It("with an exists jenkins as option", func() {
configFile, err := ioutil.TempFile("/tmp", ".yaml")
Expect(err).NotTo(HaveOccurred())
defer os.Remove(configFile.Name())
data, err := generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(configFile.Name(), data, 0664)
Expect(err).To(BeNil())
rootCmd.SetArgs([]string{"--jenkins", "yourServer", "--configFile", configFile.Name()})
_, err = rootCmd.ExecuteC()
Expect(err).NotTo(HaveOccurred())
Expect(buf.String()).To(ContainSubstring("Current Jenkins is:"))
})
})
Context("use connection from options", func() {
var (
err error
)
BeforeEach(func() {
rootOptions = RootOptions{}
})
AfterEach(func() {
rootOptions = RootOptions{}
})
It("fake jenkins, but with URL from option", func() {
rootCmd.SetArgs([]string{"--configFile", "fake", "--url", "fake-url",
"--username", "fake-user", "--token", "fake-token"})
_, err = rootCmd.ExecuteC()
Expect(err).NotTo(HaveOccurred())
jenkins := getCurrentJenkinsFromOptions()
Expect(jenkins.URL).To(Equal("fake-url"))
Expect(jenkins.UserName).To(Equal("fake-user"))
Expect(jenkins.Token).To(Equal("fake-token"))
})
})
})
// FakeOpt only for test
......
......@@ -22,14 +22,15 @@ func SetLanguage(lan string) {
language = lan
}
// JenkinsCore core informations of Jenkins
// JenkinsCore core information of Jenkins
type JenkinsCore struct {
JenkinsCrumb
URL string
UserName string
Token string
Proxy string
ProxyAuth string
URL string
InsecureSkipVerify bool
UserName string
Token string
Proxy string
ProxyAuth string
Debug bool
Output io.Writer
......@@ -49,7 +50,7 @@ func (j *JenkinsCore) GetClient() (client *http.Client) {
roundTripper = j.RoundTripper
} else {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: &tls.Config{InsecureSkipVerify: j.InsecureSkipVerify},
}
if err := util.SetProxy(j.Proxy, j.ProxyAuth, tr); err != nil {
log.Fatal(err)
......
......@@ -23,9 +23,10 @@ const (
// HTTPDownloader is the downloader for http request
type HTTPDownloader struct {
TargetFilePath string
URL string
ShowProgress bool
TargetFilePath string
URL string
ShowProgress bool
InsecureSkipVerify bool
UserName string
Password string
......@@ -75,7 +76,7 @@ func (h *HTTPDownloader) DownloadFile() error {
tr = h.RoundTripper
} else {
trp := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: &tls.Config{InsecureSkipVerify: h.InsecureSkipVerify},
}
tr = trp
if err = SetProxy(h.Proxy, h.ProxyAuth, trp); err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册