Skip to content

Commit 7418522

Browse files
committed
app: introduced application dependency container
1 parent ea8ebf8 commit 7418522

File tree

11 files changed

+108
-93
lines changed

11 files changed

+108
-93
lines changed

.goreleaser.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ builds:
1212
- windows
1313
goarch:
1414
- amd64
15+
ldflags:
16+
- -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}}
17+
- ./usemsan=-msan
1518
archive:
1619
replacements:
1720
darwin: Darwin

app/app.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"github.com/leanovate/mite-go/config"
6+
"github.com/leanovate/mite-go/mite"
7+
"github.com/mitchellh/go-homedir"
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
type Application struct {
13+
Conf config.Config
14+
MiteApi mite.Api
15+
Version string
16+
Commit string
17+
Date string
18+
}
19+
20+
const defaultConfigFileName = ".mite.toml"
21+
22+
// these flags will be overwritten during the build process by goreleaser
23+
var (
24+
version = "dev"
25+
commit = "none"
26+
date = "none"
27+
)
28+
29+
func NewApplication(fullConfigPath string) (*Application, error) {
30+
homeDirectory, err := homedir.Dir()
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
if fullConfigPath == "" {
36+
fullConfigPath = filepath.Join(homeDirectory, defaultConfigFileName)
37+
}
38+
39+
c := config.NewConfig(fullConfigPath)
40+
api := mite.NewApi(c.GetApiUrl(), c.GetApiKey(), version)
41+
42+
if c.GetApiUrl() == "" {
43+
_, _ = fmt.Fprintln(os.Stderr, "please configure your API url by executing: 'mite config api.url=<your mite api url>'")
44+
}
45+
46+
if c.GetApiKey() == "" {
47+
_, _ = fmt.Fprintln(os.Stderr, "please configure your API key by executing: 'mite config api.key=<your mite api key>'")
48+
}
49+
return &Application{Conf: c, MiteApi: api, Version: version, Commit: commit, Date: date}, nil
50+
}

cmd/cmd.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,47 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/leanovate/mite-go/config"
6-
"github.com/leanovate/mite-go/mite"
5+
"github.com/leanovate/mite-go/app"
76
"github.com/spf13/cobra"
7+
"path/filepath"
88
)
99

10-
type dependencies struct {
11-
conf config.Config
12-
miteApi mite.Api
13-
version Version
14-
}
15-
16-
type Version struct {
17-
Version string
18-
Commit string
19-
Date string
20-
}
10+
var application *app.Application
2111

22-
var deps dependencies
23-
24-
func HandleCommands(c config.Config, m mite.Api, v Version) error {
25-
deps = dependencies{conf: c, miteApi: m, version: v}
12+
func HandleCommands() error {
2613
rootCmd.Flags().BoolP("version", "v", false, "prints version")
14+
rootCmd.PersistentFlags().StringP("config", "c", "", "alternative config file location")
15+
2716
return rootCmd.Execute()
2817
}
2918

3019
var rootCmd = &cobra.Command{
3120
Use: "mite",
3221
Short: "cli client for mite time tracking",
22+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
23+
var fullConfigPath string
24+
configArg, err := cmd.Flags().GetString("config")
25+
if err == nil && configArg != "" {
26+
fullConfigPath, err = filepath.Abs(configArg)
27+
if err != nil {
28+
return err
29+
}
30+
}
31+
32+
application, err = app.NewApplication(fullConfigPath)
33+
if err != nil {
34+
return err
35+
}
36+
return nil
37+
},
3338
RunE: func(cmd *cobra.Command, args []string) error {
3439
printShortVersion, err := cmd.Flags().GetBool("version")
3540
if err != nil {
3641
return err
3742
}
3843

3944
if printShortVersion {
40-
fmt.Printf("%s\n", deps.version.Version)
45+
fmt.Printf("%s\n", application.Version)
4146
}
4247
return nil
4348
},

cmd/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var configCommand = &cobra.Command{
1616
Short: "sets or reads a config property",
1717
RunE: func(cmd *cobra.Command, args []string) error {
1818
if len(args) == 0 {
19-
deps.conf.PrintAll()
19+
application.Conf.PrintAll()
2020
return nil
2121
}
2222

@@ -32,10 +32,10 @@ var configCommand = &cobra.Command{
3232
configKeyValue := strings.Split(firstArgument, "=")
3333
configKey := configKeyValue[0]
3434
configValue := configKeyValue[1]
35-
deps.conf.Set(configKey, configValue)
35+
application.Conf.Set(configKey, configValue)
3636
return nil
3737
}
38-
fmt.Println(deps.conf.Get(configKey))
38+
fmt.Println(application.Conf.Get(configKey))
3939
return nil
4040
},
4141
}

cmd/entries.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var entriesListCommand = &cobra.Command{
8383
return err
8484
}
8585

86-
entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
86+
entries, err := application.MiteApi.TimeEntries(&domain.TimeEntryQuery{
8787
To: &to,
8888
From: &from,
8989
Direction: direction,
@@ -144,7 +144,7 @@ var entriesCreateCommand = &cobra.Command{
144144
ServiceId: cServiceId,
145145
}
146146

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

157157
func projectAndServiceId() (projectId, servicesId string) {
158158
if createProjectId == "" && createActivity != "" {
159-
activity := deps.conf.GetActivity(createActivity)
159+
activity := application.Conf.GetActivity(createActivity)
160160
createProjectId = activity.ProjectId
161161
}
162162

163163
if createServiceId == "" && createActivity != "" {
164-
activity := deps.conf.GetActivity(createActivity)
164+
activity := application.Conf.GetActivity(createActivity)
165165
createServiceId = activity.ServiceId
166166
}
167167

168168
if createProjectId == "" {
169-
createProjectId = deps.conf.Get("projectId")
169+
createProjectId = application.Conf.Get("projectId")
170170
}
171171

172172
if createServiceId == "" {
173-
createServiceId = deps.conf.Get("serviceId")
173+
createServiceId = application.Conf.Get("serviceId")
174174
}
175175

176176
return projectId, servicesId
@@ -185,7 +185,7 @@ var entriesEditCommand = &cobra.Command{
185185
return err
186186
}
187187

188-
entry, err := deps.miteApi.TimeEntry(entryId)
188+
entry, err := application.MiteApi.TimeEntry(entryId)
189189
if err != nil {
190190
return err
191191
}
@@ -221,7 +221,7 @@ var entriesEditCommand = &cobra.Command{
221221
}
222222

223223
if editActivity != "" {
224-
activity := deps.conf.GetActivity(editActivity)
224+
activity := application.Conf.GetActivity(editActivity)
225225

226226
projectId, err := domain.ParseProjectId(activity.ProjectId)
227227
if err != nil {
@@ -252,12 +252,12 @@ var entriesEditCommand = &cobra.Command{
252252
command.ServiceId = serviceId
253253
}
254254

255-
err = deps.miteApi.EditTimeEntry(entryId, &command)
255+
err = application.MiteApi.EditTimeEntry(entryId, &command)
256256
if err != nil {
257257
return err
258258
}
259259

260-
entry, err = deps.miteApi.TimeEntry(entryId)
260+
entry, err = application.MiteApi.TimeEntry(entryId)
261261
if err != nil {
262262
return err
263263
}
@@ -276,6 +276,6 @@ var entriesDeleteCommand = &cobra.Command{
276276
return err
277277
}
278278

279-
return deps.miteApi.DeleteTimeEntry(entryId)
279+
return application.MiteApi.DeleteTimeEntry(entryId)
280280
},
281281
}

cmd/projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var listProjectsCommand = &cobra.Command{
2020
Use: "list",
2121
Short: "list projects",
2222
RunE: func(cmd *cobra.Command, args []string) error {
23-
projects, err := deps.miteApi.Projects()
23+
projects, err := application.MiteApi.Projects()
2424
if err != nil {
2525
return err
2626
}

cmd/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var listServicesCommand = &cobra.Command{
2020
Use: "list",
2121
Short: "list services",
2222
RunE: func(cmd *cobra.Command, args []string) error {
23-
services, err := deps.miteApi.Services()
23+
services, err := application.MiteApi.Services()
2424
if err != nil {
2525
return err
2626
}

cmd/tracker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var trackerStatusCommand = &cobra.Command{
3030
Use: "status",
3131
Short: "shows the status of the time tracker",
3232
RunE: func(cmd *cobra.Command, args []string) (err error) {
33-
tracking, err := deps.miteApi.Tracker()
33+
tracking, err := application.MiteApi.Tracker()
3434
if err != nil {
3535
return err
3636
}
@@ -61,7 +61,7 @@ var trackerStartCommand = &cobra.Command{
6161
return err
6262
}
6363

64-
tracking, stopped, err := deps.miteApi.StartTracker(entryId)
64+
tracking, stopped, err := application.MiteApi.StartTracker(entryId)
6565
if err != nil {
6666
return err
6767
}
@@ -92,7 +92,7 @@ var trackerStopCommand = &cobra.Command{
9292
return err
9393
}
9494

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

112-
entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
112+
entries, err := application.MiteApi.TimeEntries(&domain.TimeEntryQuery{
113113
To: &today,
114114
From: &today,
115115
Direction: "desc",

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var versionCommand = &cobra.Command{
1313
Use: "version",
1414
Short: "prints version",
1515
RunE: func(cmd *cobra.Command, args []string) error {
16-
fmt.Printf("Version: %s\nCommit: %s\nDate: %s\n", deps.version.Version, deps.version.Commit, deps.version.Date)
16+
fmt.Printf("Version: %s\nCommit: %s\nDate: %s\n", application.Version, application.Commit, application.Date)
1717
return nil
1818
},
1919
}

config/config.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/spf13/viper"
77
"os"
8-
"runtime"
98
)
109

1110
type Config interface {
@@ -18,9 +17,6 @@ type Config interface {
1817
}
1918

2019
type config struct {
21-
fileName string
22-
filePath string
23-
fileType string
2420
fileFullPath string
2521
}
2622

@@ -29,22 +25,11 @@ type Activity struct {
2925
ServiceId string
3026
}
3127

32-
func NewConfig(fileName, filePath, fileType string) Config {
33-
viper.AddConfigPath("$HOME")
34-
viper.SetConfigName(fileName)
35-
viper.SetConfigType(fileType)
36-
ffp := fullConfigPath(filePath, fileName, fileType)
37-
createConfigFileIfNonExistent(ffp)
28+
func NewConfig(fullPath string) Config {
29+
createConfigFileIfNonExistent(fullPath)
30+
viper.SetConfigFile(fullPath)
3831

39-
return &config{fileName: fileName, filePath: filePath, fileType: fileType, fileFullPath: ffp}
40-
}
41-
42-
func fullConfigPath(filePath string, fileName string, fileType string) string {
43-
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
44-
if runtime.GOOS == "windows" {
45-
ffp = fmt.Sprintf("%s\\%s.%s", filePath, fileName, fileType)
46-
}
47-
return ffp
32+
return &config{fileFullPath: fullPath}
4833
}
4934

5035
func createConfigFileIfNonExistent(ffp string) {
@@ -103,7 +88,12 @@ func (c *config) PrintAll() {
10388
if err != nil {
10489
panic(err)
10590
}
106-
defer file.Close()
91+
defer func() {
92+
err = file.Close()
93+
if err != nil {
94+
panic(err)
95+
}
96+
}()
10797

10898
scanner := bufio.NewScanner(file)
10999
for scanner.Scan() {

0 commit comments

Comments
 (0)