Skip to content

Commit

Permalink
cmd package: all functions are transformed from Run to RunE
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
1 parent c164d0b commit 04019bb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
11 changes: 5 additions & 6 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)

Expand All @@ -15,28 +14,28 @@ 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]
configKey := firstArgument
containsEquals := strings.Index(firstArgument, "=") > 0
err := viper.ReadInConfig()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return
return err
}
if containsEquals {
// write listTo config
configKeyValue := strings.Split(firstArgument, "=")
configKey := configKeyValue[0]
configValue := configKeyValue[1]
deps.conf.Set(configKey, configValue)
return
return nil
}
fmt.Println(deps.conf.Get(configKey))
return nil
},
}
30 changes: 13 additions & 17 deletions cmd/entries.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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")
Expand All @@ -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{
Expand All @@ -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
},
}

Expand All @@ -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")
}
Expand All @@ -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{
Expand All @@ -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
},
}
10 changes: 4 additions & 6 deletions cmd/projects.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/spf13/cobra"
"os"
)

func init() {
Expand All @@ -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()
Expand All @@ -34,5 +31,6 @@ var listProjectsCommand = &cobra.Command{
t.AddLine(project.Id, project.Name, project.Note)
}
t.Print()
return nil
},
}
10 changes: 4 additions & 6 deletions cmd/services.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/spf13/cobra"
"os"
)

func init() {
Expand All @@ -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()
Expand All @@ -34,5 +31,6 @@ var listServicesCommand = &cobra.Command{
t.AddLine(service.Id, service.Name, service.Note)
}
t.Print()
return nil
},
}

0 comments on commit 04019bb

Please sign in to comment.