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

Fix the panic when no config file (#269)

* Fix the panic when no config file

* Add tests for open cmd

* Remove unused functions
上级 6878eb8e
...@@ -2,7 +2,9 @@ package cmd ...@@ -2,7 +2,9 @@ package cmd
import ( import (
"fmt" "fmt"
"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"
) )
......
...@@ -2,11 +2,11 @@ package cmd ...@@ -2,11 +2,11 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"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/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
......
...@@ -31,13 +31,18 @@ var configCmd = &cobra.Command{ ...@@ -31,13 +31,18 @@ var configCmd = &cobra.Command{
Aliases: []string{"cfg"}, Aliases: []string{"cfg"},
Short: i18n.T("Manage the config of jcli"), Short: i18n.T("Manage the config of jcli"),
Long: 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() current := getCurrentJenkins()
if current.Description != "" { if current == nil {
cmd.Printf("Current Jenkins's name is %s, url is %s, description is %s\n", current.Name, current.URL, current.Description) err = fmt.Errorf("no config file found or no current setting")
} else { } 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 Example: ` jcli config generate
jcli config list jcli config list
...@@ -175,7 +180,9 @@ func loadConfig(path string) (err error) { ...@@ -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 // getMirrors returns the mirror list, one official mirror should be returned if user don't give it
func getMirrors() (mirrors []JenkinsMirror) { func getMirrors() (mirrors []JenkinsMirror) {
mirrors = config.Mirrors if config != nil {
mirrors = config.Mirrors
}
if len(mirrors) == 0 { if len(mirrors) == 0 {
mirrors = []JenkinsMirror{ mirrors = []JenkinsMirror{
{ {
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册