From 1dedfdac1bed17b18687f0707f691a0a2b888ce3 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie Date: Thu, 12 Dec 2019 21:45:08 +0800 Subject: [PATCH] Fix the panic when no config file (#269) * Fix the panic when no config file * Add tests for open cmd * Remove unused functions --- app/cmd/casc_open.go | 2 ++ app/cmd/center_start.go | 2 +- app/cmd/config.go | 17 ++++++++++++----- app/cmd/plugin_open.go | 1 + 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/cmd/casc_open.go b/app/cmd/casc_open.go index b294363..d80db68 100644 --- a/app/cmd/casc_open.go +++ b/app/cmd/casc_open.go @@ -2,7 +2,9 @@ package cmd import ( "fmt" + "github.com/jenkins-zh/jenkins-cli/app/i18n" + "github.com/jenkins-zh/jenkins-cli/util" "github.com/spf13/cobra" ) diff --git a/app/cmd/center_start.go b/app/cmd/center_start.go index a9ea218..bf9e8f2 100644 --- a/app/cmd/center_start.go +++ b/app/cmd/center_start.go @@ -2,11 +2,11 @@ package cmd import ( "fmt" + "os" "github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/util" "github.com/mitchellh/go-homedir" - "os" "github.com/spf13/cobra" ) diff --git a/app/cmd/config.go b/app/cmd/config.go index ebaa1c7..55e0f56 100644 --- a/app/cmd/config.go +++ b/app/cmd/config.go @@ -31,13 +31,18 @@ var configCmd = &cobra.Command{ Aliases: []string{"cfg"}, Short: i18n.T("Manage the config of jcli"), Long: i18n.T("Manage the config of jcli"), - Run: func(cmd *cobra.Command, _ []string) { + RunE: func(cmd *cobra.Command, _ []string) (err error) { current := getCurrentJenkins() - if current.Description != "" { - cmd.Printf("Current Jenkins's name is %s, url is %s, description is %s\n", current.Name, current.URL, current.Description) + if current == nil { + err = fmt.Errorf("no config file found or no current setting") } else { - cmd.Printf("Current Jenkins's name is %s, url is %s\n", current.Name, current.URL) + if current.Description != "" { + cmd.Printf("Current Jenkins's name is %s, url is %s, description is %s\n", current.Name, current.URL, current.Description) + } else { + cmd.Printf("Current Jenkins's name is %s, url is %s\n", current.Name, current.URL) + } } + return }, Example: ` jcli config generate jcli config list @@ -175,7 +180,9 @@ func loadConfig(path string) (err error) { // getMirrors returns the mirror list, one official mirror should be returned if user don't give it func getMirrors() (mirrors []JenkinsMirror) { - mirrors = config.Mirrors + if config != nil { + mirrors = config.Mirrors + } if len(mirrors) == 0 { mirrors = []JenkinsMirror{ { diff --git a/app/cmd/plugin_open.go b/app/cmd/plugin_open.go index 25d6de5..9beaba1 100644 --- a/app/cmd/plugin_open.go +++ b/app/cmd/plugin_open.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/jenkins-zh/jenkins-cli/util" "github.com/spf13/cobra" ) -- GitLab