Skip to content

Commit 04019bb

Browse files
committed
cmd package: all functions are transformed from Run to RunE
1 parent c164d0b commit 04019bb

File tree

5 files changed

+28
-37
lines changed

5 files changed

+28
-37
lines changed

cmd/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func HandleCommands(c config.Config, m mite.MiteApi) error {
2121
var rootCmd = &cobra.Command{
2222
Use: "mite-go",
2323
Short: "cli client for mite time tracking",
24-
Run: func(cmd *cobra.Command, args []string) {
25-
// list entries for last 7 days
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
return nil
2626
},
2727
}

cmd/config.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"github.com/spf13/cobra"
66
"github.com/spf13/viper"
7-
"os"
87
"strings"
98
)
109

@@ -15,28 +14,28 @@ func init() {
1514
var configCommand = &cobra.Command{
1615
Use: "config",
1716
Short: "sets or reads a config property",
18-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1918
if len(args) == 0 {
2019
deps.conf.PrintAll()
21-
return
20+
return nil
2221
}
2322

2423
firstArgument := args[0]
2524
configKey := firstArgument
2625
containsEquals := strings.Index(firstArgument, "=") > 0
2726
err := viper.ReadInConfig()
2827
if err != nil {
29-
_, _ = fmt.Fprintln(os.Stderr, err)
30-
return
28+
return err
3129
}
3230
if containsEquals {
3331
// write listTo config
3432
configKeyValue := strings.Split(firstArgument, "=")
3533
configKey := configKeyValue[0]
3634
configValue := configKeyValue[1]
3735
deps.conf.Set(configKey, configValue)
38-
return
36+
return nil
3937
}
4038
fmt.Println(deps.conf.Get(configKey))
39+
return nil
4140
},
4241
}

cmd/entries.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/cheynewallace/tabby"
67
"github.com/leanovate/mite-go/mite"
78
"github.com/spf13/cobra"
8-
"os"
99
"strings"
1010
"time"
1111
)
@@ -33,7 +33,7 @@ func init() {
3333
entriesListCommand.Flags().StringVarP(&listFrom, "from", "f", defaultFrom.Format("2006-01-02"), "list only entries starting at date (in YYYY-MM-DD format)")
3434
entriesListCommand.Flags().StringVarP(&listOrder, "order", "o", "asc", "list only entries starting at date (in YYYY-MM-DD format)")
3535
entriesCommand.AddCommand(entriesListCommand)
36-
// flags for create
36+
// create
3737
entriesCreateCommand.Flags().StringVarP(&createDate, "date", "D", now.Format("2006-01-02"), "day for which to create entry (in YYYY-MM-DD format)")
3838
entriesCreateCommand.Flags().DurationVarP(&createDuration, "duration", "d", defaultDuration, "duration of entry (format examples: '1h15m' or '300m' or '6h')")
3939
entriesCreateCommand.Flags().StringVarP(&createNote, "note", "n", "", "a note describing what was worked on")
@@ -46,24 +46,22 @@ func init() {
4646
var entriesCommand = &cobra.Command{
4747
Use: "entries",
4848
Short: "lists & adds time entries",
49-
Run: entriesListCommand.Run,
49+
RunE: entriesListCommand.RunE,
5050
}
5151

5252
var entriesListCommand = &cobra.Command{
5353
Use: "list",
5454
Short: "list time entries",
55-
Run: func(cmd *cobra.Command, args []string) {
55+
RunE: func(cmd *cobra.Command, args []string) error {
5656
direction := listOrder
5757

5858
to, err := time.Parse("2006-01-02", listTo)
5959
if err != nil {
60-
_, _ = fmt.Fprintln(os.Stderr, err)
61-
return
60+
return err
6261
}
6362
from, err := time.Parse("2006-01-02", listFrom)
6463
if err != nil {
65-
_, _ = fmt.Fprintln(os.Stderr, err)
66-
return
64+
return err
6765
}
6866

6967
entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
@@ -72,11 +70,11 @@ var entriesListCommand = &cobra.Command{
7270
Direction: direction,
7371
})
7472
if err != nil {
75-
_, _ = fmt.Fprintln(os.Stderr, err)
76-
return
73+
return err
7774
}
7875

7976
printEntries(entries)
77+
return nil
8078
},
8179
}
8280

@@ -95,7 +93,7 @@ func printEntries(entries []*mite.TimeEntry) {
9593
var entriesCreateCommand = &cobra.Command{
9694
Use: "create",
9795
Short: "create time entries",
98-
Run: func(cmd *cobra.Command, args []string) {
96+
RunE: func(cmd *cobra.Command, args []string) error {
9997
if createProjectId == "" {
10098
createProjectId = deps.conf.Get("projectId")
10199
}
@@ -105,14 +103,12 @@ var entriesCreateCommand = &cobra.Command{
105103
}
106104

107105
if createProjectId == "" || createServiceId == "" {
108-
_, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id (either via arguments or config)")
109-
return
106+
return errors.New("please set both the project AND service id (either via arguments or config)")
110107
}
111108

112109
cDate, err := time.Parse("2006-01-02", createDate)
113110
if err != nil {
114-
_, _ = fmt.Fprintln(os.Stderr, err)
115-
return
111+
return err
116112
}
117113

118114
timeEntry := mite.TimeEntryCommand{
@@ -125,10 +121,10 @@ var entriesCreateCommand = &cobra.Command{
125121

126122
entry, err := deps.miteApi.CreateTimeEntry(&timeEntry)
127123
if err != nil {
128-
_, _ = fmt.Fprintln(os.Stderr, err)
129-
return
124+
return err
130125
}
131126

132127
printEntries([]*mite.TimeEntry{entry})
128+
return nil
133129
},
134130
}

cmd/projects.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"github.com/cheynewallace/tabby"
65
"github.com/spf13/cobra"
7-
"os"
86
)
97

108
func init() {
@@ -15,17 +13,16 @@ func init() {
1513
var projectsCommand = &cobra.Command{
1614
Use: "projects",
1715
Short: "list & adds projects",
18-
Run: listProjectsCommand.Run,
16+
RunE: listProjectsCommand.RunE,
1917
}
2018

2119
var listProjectsCommand = &cobra.Command{
2220
Use: "list",
2321
Short: "list projects",
24-
Run: func(cmd *cobra.Command, args []string) {
22+
RunE: func(cmd *cobra.Command, args []string) error {
2523
projects, err := deps.miteApi.Projects()
2624
if err != nil {
27-
_, _ = fmt.Fprintln(os.Stderr, err)
28-
return
25+
return err
2926
}
3027

3128
t := tabby.New()
@@ -34,5 +31,6 @@ var listProjectsCommand = &cobra.Command{
3431
t.AddLine(project.Id, project.Name, project.Note)
3532
}
3633
t.Print()
34+
return nil
3735
},
3836
}

cmd/services.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"github.com/cheynewallace/tabby"
65
"github.com/spf13/cobra"
7-
"os"
86
)
97

108
func init() {
@@ -15,17 +13,16 @@ func init() {
1513
var servicesCommand = &cobra.Command{
1614
Use: "services",
1715
Short: "list & adds services",
18-
Run: listServicesCommand.Run,
16+
RunE: listServicesCommand.RunE,
1917
}
2018

2119
var listServicesCommand = &cobra.Command{
2220
Use: "list",
2321
Short: "list services",
24-
Run: func(cmd *cobra.Command, args []string) {
22+
RunE: func(cmd *cobra.Command, args []string) error {
2523
services, err := deps.miteApi.Services()
2624
if err != nil {
27-
_, _ = fmt.Fprintln(os.Stderr, err)
28-
return
25+
return err
2926
}
3027

3128
t := tabby.New()
@@ -34,5 +31,6 @@ var listServicesCommand = &cobra.Command{
3431
t.AddLine(service.Id, service.Name, service.Note)
3532
}
3633
t.Print()
34+
return nil
3735
},
3836
}

0 commit comments

Comments
 (0)