Skip to content

Commit ac9fe75

Browse files
committed
refactored cmd & config into their own packages
1 parent df789d9 commit ac9fe75

File tree

8 files changed

+112
-83
lines changed

8 files changed

+112
-83
lines changed

cmd.go renamed to cmd/cmd.go

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

33
import (
4-
"fmt"
5-
"os"
6-
4+
"github.com/leanovate/mite-go/config"
75
"github.com/spf13/cobra"
86
)
97

8+
type dependencies struct {
9+
conf config.Config
10+
}
11+
12+
var deps dependencies
13+
14+
func HandleCommands(c config.Config) error {
15+
deps = dependencies{conf: c}
16+
return rootCmd.Execute()
17+
}
18+
1019
var rootCmd = &cobra.Command{
1120
Use: "mite-go",
1221
Short: "cli client for mite time tracking",
1322
Run: func(cmd *cobra.Command, args []string) {
1423
// list entries for last 7 days
1524
},
1625
}
17-
18-
func cmdLineHandler() {
19-
if err := rootCmd.Execute(); err != nil {
20-
fmt.Println(err)
21-
os.Exit(1)
22-
}
23-
}

cmd-config.go renamed to cmd/config.go

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

33
import (
44
"fmt"
@@ -17,7 +17,7 @@ var configCommand = &cobra.Command{
1717
Short: "sets or reads a config property",
1818
Run: func(cmd *cobra.Command, args []string) {
1919
if len(args) == 0 {
20-
configPrintAll()
20+
deps.conf.PrintAll()
2121
return
2222
}
2323

@@ -34,9 +34,9 @@ var configCommand = &cobra.Command{
3434
configKeyValue := strings.Split(firstArgument, "=")
3535
configKey := configKeyValue[0]
3636
configValue := configKeyValue[1]
37-
configSet(configKey, configValue)
37+
deps.conf.Set(configKey, configValue)
3838
return
3939
}
40-
fmt.Println(configGet(configKey))
40+
fmt.Println(deps.conf.Get(configKey))
4141
},
4242
}

cmd-entries.go renamed to cmd/entries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"fmt"
@@ -36,7 +36,7 @@ var entriesListCommand = &cobra.Command{
3636
Use: "list",
3737
Short: "list time entries",
3838
Run: func(cmd *cobra.Command, args []string) {
39-
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
39+
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
4040

4141
direction := listOrder
4242

cmd-projects.go renamed to cmd/projects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"fmt"
@@ -23,7 +23,7 @@ var listProjectsCommand = &cobra.Command{
2323
Use: "list",
2424
Short: "list projects",
2525
Run: func(cmd *cobra.Command, args []string) {
26-
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
26+
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
2727
projects, err := api.Projects()
2828
if err != nil {
2929
_, _ = fmt.Fprintln(os.Stderr, err)

list-services.go renamed to cmd/services.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"fmt"
@@ -23,7 +23,7 @@ var listServicesCommand = &cobra.Command{
2323
Use: "list",
2424
Short: "list services",
2525
Run: func(cmd *cobra.Command, args []string) {
26-
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
26+
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
2727
services, err := api.Services()
2828
if err != nil {
2929
_, _ = fmt.Fprintln(os.Stderr, err)

config.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

config/config.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/viper"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
type Config interface {
11+
GetApiUrl() string
12+
GetApiKey() string
13+
Get(key string) string
14+
Set(key string, value string)
15+
PrintAll()
16+
}
17+
18+
type config struct {
19+
fileName string
20+
filePath string
21+
fileType string
22+
}
23+
24+
func NewConfig(fileName, filePath, fileType string) Config {
25+
viper.AddConfigPath("$HOME")
26+
viper.SetConfigName(fileName)
27+
viper.SetConfigType(fileType)
28+
return &config{fileName: fileName, filePath: filePath, fileType: fileType}
29+
}
30+
31+
func (c *config) GetApiUrl() string {
32+
return c.Get("api.url")
33+
}
34+
35+
func (c *config) GetApiKey() string {
36+
return c.Get("api.key")
37+
}
38+
39+
func (c *config) Get(key string) string {
40+
err := viper.ReadInConfig()
41+
if err != nil {
42+
_, _ = fmt.Fprintln(os.Stderr, err)
43+
}
44+
return viper.GetString(key)
45+
}
46+
47+
func (c *config) Set(key string, value string) {
48+
err := viper.ReadInConfig()
49+
if err != nil {
50+
_, _ = fmt.Fprintln(os.Stderr, err)
51+
}
52+
53+
viper.Set(key, value)
54+
err = viper.MergeInConfig()
55+
if err != nil {
56+
_, _ = fmt.Fprintln(os.Stderr, err)
57+
}
58+
err = viper.WriteConfigAs(filepath.Join(c.filePath, c.fileName))
59+
if err != nil {
60+
_, _ = fmt.Fprintln(os.Stderr, err)
61+
}
62+
}
63+
64+
func (c *config) PrintAll() {
65+
err := viper.ReadInConfig()
66+
if err != nil {
67+
_, _ = fmt.Fprintln(os.Stderr, err)
68+
}
69+
wholeConfig := viper.AllSettings()
70+
fmt.Printf("%v\n", wholeConfig)
71+
}

main.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package main
22

33
import (
4-
"github.com/spf13/viper"
4+
"fmt"
5+
"github.com/leanovate/mite-go/cmd"
6+
"github.com/leanovate/mite-go/config"
7+
"github.com/mitchellh/go-homedir"
8+
"os"
59
)
610

7-
const configFileName = ".mite-go.toml"
8-
const configPath = "$HOME"
11+
const configFileName = ".mite-go"
12+
const configType = "toml"
913

1014
func main() {
11-
viper.AddConfigPath(configPath)
12-
viper.SetConfigName(".mite-go")
13-
viper.SetConfigType("toml")
14-
cmdLineHandler()
15+
homeDirectory, err := homedir.Dir()
16+
if err != nil {
17+
_, _ = fmt.Fprintln(os.Stderr, err)
18+
}
19+
c := config.NewConfig(configFileName, homeDirectory, configType)
20+
21+
err = cmd.HandleCommands(c)
22+
if err != nil {
23+
_, _ = fmt.Fprintln(os.Stderr, err)
24+
os.Exit(1)
25+
}
1526
}

0 commit comments

Comments
 (0)