Skip to content

Commit

Permalink
do not show help on most error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ydkn committed May 22, 2019
1 parent 3742f78 commit 0de1126
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
11 changes: 8 additions & 3 deletions cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ var eventCmd = &cobra.Command{
Use: "event",
Short: "Handle event data",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
terminateWithError("invalid argument(s) received")
return
}

event, err := loadEvent()
if err != nil {
terminateWithHelpAndMessage(cmd, fmt.Sprintf("failed to load event: %s", err.Error()))
terminateWithError(fmt.Sprintf("failed to load event: %s", err.Error()))
return
}

err = validateEvent(event)
if err != nil {
terminateWithHelpAndMessage(cmd, fmt.Sprintf("failed to validate event: %s", err.Error()))
terminateWithError(fmt.Sprintf("failed to validate event: %s", err.Error()))
return
}

err = handleEvent(event)
if err != nil {
terminateWithHelpAndMessage(cmd, fmt.Sprintf("failed to handle event: %s", err.Error()))
terminateWithError(fmt.Sprintf("failed to handle event: %s", err.Error()))
return
}
},
Expand Down
10 changes: 5 additions & 5 deletions cmd/redmineImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ var redmineImportCmd = &cobra.Command{
Short: "Import recipients from redmine",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
terminateWithHelpAndMessage(cmd, "invalid argument(s) received")
terminateWithError("invalid argument(s) received")
return
}

if viper.GetString("redmine-url") == "" {
terminateWithHelpAndMessage(cmd, "redmine url is empty")
terminateWithError("redmine url is empty")
return
}

if viper.GetString("redmine-token") == "" {
terminateWithHelpAndMessage(cmd, "redmine token is empty")
terminateWithError("redmine token is empty")
return
}

etcdClient, err := etcd.New(etcdConfig)
if err != nil {
terminateWithHelpAndMessage(cmd, "unable to connect to etcd")
terminateWithError("unable to connect to etcd")
return
}
defer etcdClient.Close()

err = redmine.Import(viper.GetString("redmine-url"), viper.GetString("redmine-token"), etcdClient)
if err != nil {
terminateWithHelpAndMessage(cmd, err.Error())
terminateWithError(err.Error())
return
}
},
Expand Down
11 changes: 2 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func initConfig() {
// If a config file is found, read it in.
err := viper.ReadInConfig()
if err != nil {
terminateWithError(err)
terminateWithError(err.Error())
}
}

Expand All @@ -69,14 +69,7 @@ func initConfig() {
}
}

func terminateWithHelpAndMessage(cmd *cobra.Command, msg string) {
_ = cmd.Help()

fmt.Fprintln(os.Stderr, "")
func terminateWithError(msg string) {
fmt.Fprintln(os.Stderr, msg)
}

func terminateWithError(err error) {
fmt.Println(err)
os.Exit(1)
}

0 comments on commit 0de1126

Please sign in to comment.