From 3b07e63bafc849fdc70f217701f82fb5cb5763f9 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie Date: Wed, 8 Jul 2020 08:40:31 +0800 Subject: [PATCH] Add timeout option for plugin check command (#422) --- app/cmd/plugin_check.go | 17 ++++++++++++----- app/cmd/plugin_list.go | 3 +-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/cmd/plugin_check.go b/app/cmd/plugin_check.go index acc2e10..81095c0 100644 --- a/app/cmd/plugin_check.go +++ b/app/cmd/plugin_check.go @@ -2,10 +2,10 @@ package cmd import ( "fmt" - "github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/i18n" "io/ioutil" "net/http" + "time" "github.com/jenkins-zh/jenkins-cli/client" "github.com/spf13/cobra" @@ -14,33 +14,40 @@ import ( // PluginCheckoutOption is the option for plugin checkout command type PluginCheckoutOption struct { RoundTripper http.RoundTripper + // Timeout is the timeout setting for check Jenkins update-center + Timeout int64 } var pluginCheckoutOption PluginCheckoutOption func init() { pluginCmd.AddCommand(pluginCheckCmd) + + flags := pluginCheckCmd.Flags() + flags.Int64VarP(&pluginCheckoutOption.Timeout, "timeout", "", 30, + "Timeout in second setting for checking Jenkins update-center") } var pluginCheckCmd = &cobra.Command{ Use: "check", Short: i18n.T("Check update center server"), Long: i18n.T(`Check update center server`), - Run: func(cmd *cobra.Command, _ []string) { + RunE: func(cmd *cobra.Command, _ []string) (err error) { jClient := &client.PluginManager{ JenkinsCore: client.JenkinsCore{ RoundTripper: pluginCheckoutOption.RoundTripper, + Timeout: time.Duration(pluginCheckoutOption.Timeout) * time.Second, }, } - getCurrentJenkinsAndClientOrDie(&(jClient.JenkinsCore)) + getCurrentJenkinsAndClient(&(jClient.JenkinsCore)) - err := jClient.CheckUpdate(func(response *http.Response) { + err = jClient.CheckUpdate(func(response *http.Response) { code := response.StatusCode if code != 200 { contentData, _ := ioutil.ReadAll(response.Body) cmd.PrintErrln(fmt.Sprintf("response code is %d, content: %s", code, string(contentData))) } }) - helper.CheckErr(cmd, err) + return }, } diff --git a/app/cmd/plugin_list.go b/app/cmd/plugin_list.go index bb0f802..cf0e398 100644 --- a/app/cmd/plugin_list.go +++ b/app/cmd/plugin_list.go @@ -26,8 +26,7 @@ var pluginListCmd = &cobra.Command{ Use: "list", Short: i18n.T("Print all the plugins which are installed"), Long: i18n.T("Print all the plugins which are installed"), - Example: ` jcli plugin list --filter name=github - jcli plugin list --filter hasUpdate + Example: ` jcli plugin list --filter ShortName=github jcli plugin list --no-headers`, RunE: func(cmd *cobra.Command, _ []string) (err error) { jClient := &client.PluginManager{ -- GitLab