-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored cmd & config into their own packages
- Loading branch information
Showing
8 changed files
with
112 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |