cobra: check error return values

This commit is contained in:
umarcor 2019-06-07 19:13:15 +02:00
parent c312af1143
commit 4656c87941
8 changed files with 15 additions and 16 deletions

View file

@ -40,7 +40,7 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 { if len(args) < 1 {
er("add needs a name for the command") er(fmt.Errorf("add needs a name for the command"))
} }
wd, err := os.Getwd() wd, err := os.Getwd()
@ -72,7 +72,7 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
func init() { func init() {
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command")
addCmd.Flags().MarkDeprecated("package", "this operation has been removed.") er(addCmd.Flags().MarkDeprecated("package", "this operation has been removed."))
} }
// validateCmdName returns source without any dashes and underscore. // validateCmdName returns source without any dashes and underscore.

View file

@ -14,10 +14,8 @@ func TestGoldenAddCmd(t *testing.T) {
} }
defer os.RemoveAll(command.AbsolutePath) defer os.RemoveAll(command.AbsolutePath)
command.Project.Create() er(command.Project.Create())
if err := command.Create(); err != nil { er(command.Create())
t.Fatal(err)
}
generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName) generatedFile := fmt.Sprintf("%s/cmd/%s.go", command.AbsolutePath, command.CmdName)
goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName) goldenFile := fmt.Sprintf("testdata/%s.go.golden", command.CmdName)

View file

@ -60,8 +60,10 @@ func init() {
} }
func er(msg interface{}) { func er(msg interface{}) {
if msg != nil {
fmt.Println("Error:", msg) fmt.Println("Error:", msg)
os.Exit(1) os.Exit(1)
}
} }
// isEmpty checks if a given path is empty. // isEmpty checks if a given path is empty.

View file

@ -74,5 +74,5 @@ Init will not use an existing directory with contents.`,
func init() { func init() {
initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name") initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
initCmd.MarkFlagRequired("pkg-name") er(initCmd.MarkFlagRequired("pkg-name"))
} }

View file

@ -23,9 +23,7 @@ func TestGoldenInitCmd(t *testing.T) {
project := getProject() project := getProject()
defer os.RemoveAll(project.AbsolutePath) defer os.RemoveAll(project.AbsolutePath)
if err := project.Create(); err != nil { er(project.Create())
t.Fatal(err)
}
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"} expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
for _, f := range expectedFiles { for _, f := range expectedFiles {

View file

@ -16,6 +16,7 @@
package cmd package cmd
import ( import (
"fmt"
"strings" "strings"
"time" "time"
@ -92,7 +93,7 @@ func copyrightLine() string {
func findLicense(name string) License { func findLicense(name string) License {
found := matchLicense(name) found := matchLicense(name)
if found == "" { if found == "" {
er("unknown license: " + name) er(fmt.Errorf("unknown license: " + name))
} }
return Licenses[found] return Licenses[found]
} }

View file

@ -49,7 +49,7 @@ func (p *Project) Create() error {
// create cmd/root.go // create cmd/root.go
if _, err = os.Stat(fmt.Sprintf("%s/cmd", p.AbsolutePath)); os.IsNotExist(err) { if _, err = os.Stat(fmt.Sprintf("%s/cmd", p.AbsolutePath)); os.IsNotExist(err) {
os.Mkdir(fmt.Sprintf("%s/cmd", p.AbsolutePath), 0751) er(os.Mkdir(fmt.Sprintf("%s/cmd", p.AbsolutePath), 0751))
} }
rootFile, err := os.Create(fmt.Sprintf("%s/cmd/root.go", p.AbsolutePath)) rootFile, err := os.Create(fmt.Sprintf("%s/cmd/root.go", p.AbsolutePath))
if err != nil { if err != nil {

View file

@ -47,8 +47,8 @@ func init() {
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")) er(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")))
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")) er(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>") viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache") viper.SetDefault("license", "apache")