Skip to content

Commit

Permalink
app: introduced application dependency container
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 9, 2019
1 parent ea8ebf8 commit 7418522
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ builds:
- windows
goarch:
- amd64
ldflags:
- -s -w -X github.com/leanovate/mite-go/app.version={{.Version}} -X github.com/leanovate/mite-go/app.commit={{.Commit}} -X github.com/leanovate/mite-go/app.date={{.Date}}
- ./usemsan=-msan
archive:
replacements:
darwin: Darwin
Expand Down
50 changes: 50 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package app

import (
"fmt"
"github.com/leanovate/mite-go/config"
"github.com/leanovate/mite-go/mite"
"github.com/mitchellh/go-homedir"
"os"
"path/filepath"
)

type Application struct {
Conf config.Config
MiteApi mite.Api
Version string
Commit string
Date string
}

const defaultConfigFileName = ".mite.toml"

// these flags will be overwritten during the build process by goreleaser
var (
version = "dev"
commit = "none"
date = "none"
)

func NewApplication(fullConfigPath string) (*Application, error) {
homeDirectory, err := homedir.Dir()
if err != nil {
return nil, err
}

if fullConfigPath == "" {
fullConfigPath = filepath.Join(homeDirectory, defaultConfigFileName)
}

c := config.NewConfig(fullConfigPath)
api := mite.NewApi(c.GetApiUrl(), c.GetApiKey(), version)

if c.GetApiUrl() == "" {
_, _ = fmt.Fprintln(os.Stderr, "please configure your API url by executing: 'mite config api.url=<your mite api url>'")
}

if c.GetApiKey() == "" {
_, _ = fmt.Fprintln(os.Stderr, "please configure your API key by executing: 'mite config api.key=<your mite api key>'")
}
return &Application{Conf: c, MiteApi: api, Version: version, Commit: commit, Date: date}, nil
}
41 changes: 23 additions & 18 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,47 @@ package cmd

import (
"fmt"
"github.com/leanovate/mite-go/config"
"github.com/leanovate/mite-go/mite"
"github.com/leanovate/mite-go/app"
"github.com/spf13/cobra"
"path/filepath"
)

type dependencies struct {
conf config.Config
miteApi mite.Api
version Version
}

type Version struct {
Version string
Commit string
Date string
}
var application *app.Application

var deps dependencies

func HandleCommands(c config.Config, m mite.Api, v Version) error {
deps = dependencies{conf: c, miteApi: m, version: v}
func HandleCommands() error {
rootCmd.Flags().BoolP("version", "v", false, "prints version")
rootCmd.PersistentFlags().StringP("config", "c", "", "alternative config file location")

return rootCmd.Execute()
}

var rootCmd = &cobra.Command{
Use: "mite",
Short: "cli client for mite time tracking",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var fullConfigPath string
configArg, err := cmd.Flags().GetString("config")
if err == nil && configArg != "" {
fullConfigPath, err = filepath.Abs(configArg)
if err != nil {
return err
}
}

application, err = app.NewApplication(fullConfigPath)
if err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
printShortVersion, err := cmd.Flags().GetBool("version")
if err != nil {
return err
}

if printShortVersion {
fmt.Printf("%s\n", deps.version.Version)
fmt.Printf("%s\n", application.Version)
}
return nil
},
Expand Down
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var configCommand = &cobra.Command{
Short: "sets or reads a config property",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
deps.conf.PrintAll()
application.Conf.PrintAll()
return nil
}

Expand All @@ -32,10 +32,10 @@ var configCommand = &cobra.Command{
configKeyValue := strings.Split(firstArgument, "=")
configKey := configKeyValue[0]
configValue := configKeyValue[1]
deps.conf.Set(configKey, configValue)
application.Conf.Set(configKey, configValue)
return nil
}
fmt.Println(deps.conf.Get(configKey))
fmt.Println(application.Conf.Get(configKey))
return nil
},
}
22 changes: 11 additions & 11 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var entriesListCommand = &cobra.Command{
return err
}

entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
entries, err := application.MiteApi.TimeEntries(&domain.TimeEntryQuery{
To: &to,
From: &from,
Direction: direction,
Expand Down Expand Up @@ -144,7 +144,7 @@ var entriesCreateCommand = &cobra.Command{
ServiceId: cServiceId,
}

entry, err := deps.miteApi.CreateTimeEntry(&timeEntry)
entry, err := application.MiteApi.CreateTimeEntry(&timeEntry)
if err != nil {
return err
}
Expand All @@ -156,21 +156,21 @@ var entriesCreateCommand = &cobra.Command{

func projectAndServiceId() (projectId, servicesId string) {
if createProjectId == "" && createActivity != "" {
activity := deps.conf.GetActivity(createActivity)
activity := application.Conf.GetActivity(createActivity)
createProjectId = activity.ProjectId
}

if createServiceId == "" && createActivity != "" {
activity := deps.conf.GetActivity(createActivity)
activity := application.Conf.GetActivity(createActivity)
createServiceId = activity.ServiceId
}

if createProjectId == "" {
createProjectId = deps.conf.Get("projectId")
createProjectId = application.Conf.Get("projectId")
}

if createServiceId == "" {
createServiceId = deps.conf.Get("serviceId")
createServiceId = application.Conf.Get("serviceId")
}

return projectId, servicesId
Expand All @@ -185,7 +185,7 @@ var entriesEditCommand = &cobra.Command{
return err
}

entry, err := deps.miteApi.TimeEntry(entryId)
entry, err := application.MiteApi.TimeEntry(entryId)
if err != nil {
return err
}
Expand Down Expand Up @@ -221,7 +221,7 @@ var entriesEditCommand = &cobra.Command{
}

if editActivity != "" {
activity := deps.conf.GetActivity(editActivity)
activity := application.Conf.GetActivity(editActivity)

projectId, err := domain.ParseProjectId(activity.ProjectId)
if err != nil {
Expand Down Expand Up @@ -252,12 +252,12 @@ var entriesEditCommand = &cobra.Command{
command.ServiceId = serviceId
}

err = deps.miteApi.EditTimeEntry(entryId, &command)
err = application.MiteApi.EditTimeEntry(entryId, &command)
if err != nil {
return err
}

entry, err = deps.miteApi.TimeEntry(entryId)
entry, err = application.MiteApi.TimeEntry(entryId)
if err != nil {
return err
}
Expand All @@ -276,6 +276,6 @@ var entriesDeleteCommand = &cobra.Command{
return err
}

return deps.miteApi.DeleteTimeEntry(entryId)
return application.MiteApi.DeleteTimeEntry(entryId)
},
}
2 changes: 1 addition & 1 deletion cmd/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var listProjectsCommand = &cobra.Command{
Use: "list",
Short: "list projects",
RunE: func(cmd *cobra.Command, args []string) error {
projects, err := deps.miteApi.Projects()
projects, err := application.MiteApi.Projects()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var listServicesCommand = &cobra.Command{
Use: "list",
Short: "list services",
RunE: func(cmd *cobra.Command, args []string) error {
services, err := deps.miteApi.Services()
services, err := application.MiteApi.Services()
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var trackerStatusCommand = &cobra.Command{
Use: "status",
Short: "shows the status of the time tracker",
RunE: func(cmd *cobra.Command, args []string) (err error) {
tracking, err := deps.miteApi.Tracker()
tracking, err := application.MiteApi.Tracker()
if err != nil {
return err
}
Expand Down Expand Up @@ -61,7 +61,7 @@ var trackerStartCommand = &cobra.Command{
return err
}

tracking, stopped, err := deps.miteApi.StartTracker(entryId)
tracking, stopped, err := application.MiteApi.StartTracker(entryId)
if err != nil {
return err
}
Expand Down Expand Up @@ -92,7 +92,7 @@ var trackerStopCommand = &cobra.Command{
return err
}

stopped, err := deps.miteApi.StopTracker(entryId)
stopped, err := application.MiteApi.StopTracker(entryId)
if err != nil {
return err
}
Expand All @@ -109,7 +109,7 @@ var trackerStopCommand = &cobra.Command{
func fetchLatestTimeEntryForToday() (domain.TimeEntryId, error) {
today := domain.Today()

entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
entries, err := application.MiteApi.TimeEntries(&domain.TimeEntryQuery{
To: &today,
From: &today,
Direction: "desc",
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var versionCommand = &cobra.Command{
Use: "version",
Short: "prints version",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Version: %s\nCommit: %s\nDate: %s\n", deps.version.Version, deps.version.Commit, deps.version.Date)
fmt.Printf("Version: %s\nCommit: %s\nDate: %s\n", application.Version, application.Commit, application.Date)
return nil
},
}
30 changes: 10 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/spf13/viper"
"os"
"runtime"
)

type Config interface {
Expand All @@ -18,9 +17,6 @@ type Config interface {
}

type config struct {
fileName string
filePath string
fileType string
fileFullPath string
}

Expand All @@ -29,22 +25,11 @@ type Activity struct {
ServiceId string
}

func NewConfig(fileName, filePath, fileType string) Config {
viper.AddConfigPath("$HOME")
viper.SetConfigName(fileName)
viper.SetConfigType(fileType)
ffp := fullConfigPath(filePath, fileName, fileType)
createConfigFileIfNonExistent(ffp)
func NewConfig(fullPath string) Config {
createConfigFileIfNonExistent(fullPath)
viper.SetConfigFile(fullPath)

return &config{fileName: fileName, filePath: filePath, fileType: fileType, fileFullPath: ffp}
}

func fullConfigPath(filePath string, fileName string, fileType string) string {
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
if runtime.GOOS == "windows" {
ffp = fmt.Sprintf("%s\\%s.%s", filePath, fileName, fileType)
}
return ffp
return &config{fileFullPath: fullPath}
}

func createConfigFileIfNonExistent(ffp string) {
Expand Down Expand Up @@ -103,7 +88,12 @@ func (c *config) PrintAll() {
if err != nil {
panic(err)
}
defer file.Close()
defer func() {
err = file.Close()
if err != nil {
panic(err)
}
}()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
Expand Down
Loading

0 comments on commit 7418522

Please sign in to comment.