Skip to content

Commit

Permalink
Added API as dependencies for command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
1 parent 8d6620f commit 0b97509
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
8 changes: 5 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package cmd

import (
"github.com/leanovate/mite-go/config"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
)

type dependencies struct {
conf config.Config
conf config.Config
miteApi mite.MiteApi
}

var deps dependencies

func HandleCommands(c config.Config) error {
deps = dependencies{conf: c}
func HandleCommands(c config.Config, m mite.MiteApi) error {
deps = dependencies{conf: c, miteApi: m}
return rootCmd.Execute()
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ var entriesListCommand = &cobra.Command{
Use: "list",
Short: "list time entries",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())

direction := listOrder

to, err := time.Parse("2006-01-02", listTo)
Expand All @@ -51,7 +49,7 @@ var entriesListCommand = &cobra.Command{
return
}

entries, err := api.TimeEntries(&mite.TimeEntryParameters{
entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryParameters{
To: &to,
From: &from,
Direction: direction,
Expand Down
4 changes: 1 addition & 3 deletions cmd/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"os"
)
Expand All @@ -23,8 +22,7 @@ var listProjectsCommand = &cobra.Command{
Use: "list",
Short: "list projects",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
projects, err := api.Projects()
projects, err := deps.miteApi.Projects()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return
Expand Down
4 changes: 1 addition & 3 deletions cmd/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"os"
)
Expand All @@ -23,8 +22,7 @@ var listServicesCommand = &cobra.Command{
Use: "list",
Short: "list services",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
services, err := api.Services()
services, err := deps.miteApi.Services()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/leanovate/mite-go/cmd"
"github.com/leanovate/mite-go/config"
"github.com/leanovate/mite-go/mite"
"github.com/mitchellh/go-homedir"
"os"
)
Expand All @@ -17,8 +18,9 @@ func main() {
_, _ = fmt.Fprintln(os.Stderr, err)
}
c := config.NewConfig(configFileName, homeDirectory, configType)
api := mite.NewMiteApi(c.GetApiUrl(), c.GetApiKey())

err = cmd.HandleCommands(c)
err = cmd.HandleCommands(c, api)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down

0 comments on commit 0b97509

Please sign in to comment.