Skip to content

Commit

Permalink
refactored cmd & config into their own packages
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
1 parent df789d9 commit ac9fe75
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 83 deletions.
24 changes: 13 additions & 11 deletions cmd.go → cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package main
package cmd

import (
"fmt"
"os"

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

type dependencies struct {
conf config.Config
}

var deps dependencies

func HandleCommands(c config.Config) error {
deps = dependencies{conf: c}
return rootCmd.Execute()
}

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
},
}

func cmdLineHandler() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
8 changes: 4 additions & 4 deletions cmd-config.go → cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

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

Expand All @@ -34,9 +34,9 @@ var configCommand = &cobra.Command{
configKeyValue := strings.Split(firstArgument, "=")
configKey := configKeyValue[0]
configValue := configKeyValue[1]
configSet(configKey, configValue)
deps.conf.Set(configKey, configValue)
return
}
fmt.Println(configGet(configKey))
fmt.Println(deps.conf.Get(configKey))
},
}
4 changes: 2 additions & 2 deletions cmd-entries.go → cmd/entries.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

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

direction := listOrder

Expand Down
4 changes: 2 additions & 2 deletions cmd-projects.go → cmd/projects.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand All @@ -23,7 +23,7 @@ var listProjectsCommand = &cobra.Command{
Use: "list",
Short: "list projects",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
projects, err := api.Projects()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
Expand Down
4 changes: 2 additions & 2 deletions list-services.go → cmd/services.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand All @@ -23,7 +23,7 @@ var listServicesCommand = &cobra.Command{
Use: "list",
Short: "list services",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
api := mite.NewMiteApi(deps.conf.GetApiUrl(), deps.conf.GetApiKey())
services, err := api.Services()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
Expand Down
55 changes: 0 additions & 55 deletions config.go

This file was deleted.

71 changes: 71 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"fmt"
"github.com/spf13/viper"
"os"
"path/filepath"
)

type Config interface {
GetApiUrl() string
GetApiKey() string
Get(key string) string
Set(key string, value string)
PrintAll()
}

type config struct {
fileName string
filePath string
fileType string
}

func NewConfig(fileName, filePath, fileType string) Config {
viper.AddConfigPath("$HOME")
viper.SetConfigName(fileName)
viper.SetConfigType(fileType)
return &config{fileName: fileName, filePath: filePath, fileType: fileType}
}

func (c *config) GetApiUrl() string {
return c.Get("api.url")
}

func (c *config) GetApiKey() string {
return c.Get("api.key")
}

func (c *config) Get(key string) string {
err := viper.ReadInConfig()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
return viper.GetString(key)
}

func (c *config) Set(key string, value string) {
err := viper.ReadInConfig()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

viper.Set(key, value)
err = viper.MergeInConfig()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
err = viper.WriteConfigAs(filepath.Join(c.filePath, c.fileName))
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
}

func (c *config) PrintAll() {
err := viper.ReadInConfig()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
wholeConfig := viper.AllSettings()
fmt.Printf("%v\n", wholeConfig)
}
25 changes: 18 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package main

import (
"github.com/spf13/viper"
"fmt"
"github.com/leanovate/mite-go/cmd"
"github.com/leanovate/mite-go/config"
"github.com/mitchellh/go-homedir"
"os"
)

const configFileName = ".mite-go.toml"
const configPath = "$HOME"
const configFileName = ".mite-go"
const configType = "toml"

func main() {
viper.AddConfigPath(configPath)
viper.SetConfigName(".mite-go")
viper.SetConfigType("toml")
cmdLineHandler()
homeDirectory, err := homedir.Dir()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
c := config.NewConfig(configFileName, homeDirectory, configType)

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

0 comments on commit ac9fe75

Please sign in to comment.