From 04019bbbb452cc5efea16618420e107172d52fbf Mon Sep 17 00:00:00 2001 From: Philipp Rosenkranz Date: Wed, 3 Apr 2019 14:18:46 +0200 Subject: [PATCH] cmd package: all functions are transformed from Run to RunE --- cmd/cmd.go | 4 ++-- cmd/config.go | 11 +++++------ cmd/entries.go | 30 +++++++++++++----------------- cmd/projects.go | 10 ++++------ cmd/services.go | 10 ++++------ 5 files changed, 28 insertions(+), 37 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 480ab2b..824f275 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -21,7 +21,7 @@ func HandleCommands(c config.Config, m mite.MiteApi) error { var rootCmd = &cobra.Command{ Use: "mite-go", Short: "cli client for mite time tracking", - Run: func(cmd *cobra.Command, args []string) { - // list entries for last 7 days + RunE: func(cmd *cobra.Command, args []string) error { + return nil }, } diff --git a/cmd/config.go b/cmd/config.go index e0dae30..7b0ff76 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/spf13/cobra" "github.com/spf13/viper" - "os" "strings" ) @@ -15,10 +14,10 @@ func init() { var configCommand = &cobra.Command{ Use: "config", Short: "sets or reads a config property", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { deps.conf.PrintAll() - return + return nil } firstArgument := args[0] @@ -26,8 +25,7 @@ var configCommand = &cobra.Command{ containsEquals := strings.Index(firstArgument, "=") > 0 err := viper.ReadInConfig() if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } if containsEquals { // write listTo config @@ -35,8 +33,9 @@ var configCommand = &cobra.Command{ configKey := configKeyValue[0] configValue := configKeyValue[1] deps.conf.Set(configKey, configValue) - return + return nil } fmt.Println(deps.conf.Get(configKey)) + return nil }, } diff --git a/cmd/entries.go b/cmd/entries.go index 806a7fc..61b23fb 100644 --- a/cmd/entries.go +++ b/cmd/entries.go @@ -1,11 +1,11 @@ package cmd import ( + "errors" "fmt" "github.com/cheynewallace/tabby" "github.com/leanovate/mite-go/mite" "github.com/spf13/cobra" - "os" "strings" "time" ) @@ -33,7 +33,7 @@ func init() { entriesListCommand.Flags().StringVarP(&listFrom, "from", "f", defaultFrom.Format("2006-01-02"), "list only entries starting at date (in YYYY-MM-DD format)") entriesListCommand.Flags().StringVarP(&listOrder, "order", "o", "asc", "list only entries starting at date (in YYYY-MM-DD format)") entriesCommand.AddCommand(entriesListCommand) - // flags for create + // create entriesCreateCommand.Flags().StringVarP(&createDate, "date", "D", now.Format("2006-01-02"), "day for which to create entry (in YYYY-MM-DD format)") entriesCreateCommand.Flags().DurationVarP(&createDuration, "duration", "d", defaultDuration, "duration of entry (format examples: '1h15m' or '300m' or '6h')") entriesCreateCommand.Flags().StringVarP(&createNote, "note", "n", "", "a note describing what was worked on") @@ -46,24 +46,22 @@ func init() { var entriesCommand = &cobra.Command{ Use: "entries", Short: "lists & adds time entries", - Run: entriesListCommand.Run, + RunE: entriesListCommand.RunE, } var entriesListCommand = &cobra.Command{ Use: "list", Short: "list time entries", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { direction := listOrder to, err := time.Parse("2006-01-02", listTo) if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } from, err := time.Parse("2006-01-02", listFrom) if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{ @@ -72,11 +70,11 @@ var entriesListCommand = &cobra.Command{ Direction: direction, }) if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } printEntries(entries) + return nil }, } @@ -95,7 +93,7 @@ func printEntries(entries []*mite.TimeEntry) { var entriesCreateCommand = &cobra.Command{ Use: "create", Short: "create time entries", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if createProjectId == "" { createProjectId = deps.conf.Get("projectId") } @@ -105,14 +103,12 @@ var entriesCreateCommand = &cobra.Command{ } if createProjectId == "" || createServiceId == "" { - _, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id (either via arguments or config)") - return + return errors.New("please set both the project AND service id (either via arguments or config)") } cDate, err := time.Parse("2006-01-02", createDate) if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } timeEntry := mite.TimeEntryCommand{ @@ -125,10 +121,10 @@ var entriesCreateCommand = &cobra.Command{ entry, err := deps.miteApi.CreateTimeEntry(&timeEntry) if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } printEntries([]*mite.TimeEntry{entry}) + return nil }, } diff --git a/cmd/projects.go b/cmd/projects.go index 5a8fdd4..11aa2a5 100644 --- a/cmd/projects.go +++ b/cmd/projects.go @@ -1,10 +1,8 @@ package cmd import ( - "fmt" "github.com/cheynewallace/tabby" "github.com/spf13/cobra" - "os" ) func init() { @@ -15,17 +13,16 @@ func init() { var projectsCommand = &cobra.Command{ Use: "projects", Short: "list & adds projects", - Run: listProjectsCommand.Run, + RunE: listProjectsCommand.RunE, } var listProjectsCommand = &cobra.Command{ Use: "list", Short: "list projects", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { projects, err := deps.miteApi.Projects() if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } t := tabby.New() @@ -34,5 +31,6 @@ var listProjectsCommand = &cobra.Command{ t.AddLine(project.Id, project.Name, project.Note) } t.Print() + return nil }, } diff --git a/cmd/services.go b/cmd/services.go index 5010277..16de9e4 100644 --- a/cmd/services.go +++ b/cmd/services.go @@ -1,10 +1,8 @@ package cmd import ( - "fmt" "github.com/cheynewallace/tabby" "github.com/spf13/cobra" - "os" ) func init() { @@ -15,17 +13,16 @@ func init() { var servicesCommand = &cobra.Command{ Use: "services", Short: "list & adds services", - Run: listServicesCommand.Run, + RunE: listServicesCommand.RunE, } var listServicesCommand = &cobra.Command{ Use: "list", Short: "list services", - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { services, err := deps.miteApi.Services() if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return + return err } t := tabby.New() @@ -34,5 +31,6 @@ var listServicesCommand = &cobra.Command{ t.AddLine(service.Id, service.Name, service.Note) } t.Print() + return nil }, }