Skip to content

Commit

Permalink
entries: default project & service Ids can be set via config
Browse files Browse the repository at this point in the history
* config only provides fallback values
* cmd line arguments override defaults
  • Loading branch information
phiros committed Apr 3, 2019
1 parent a3ce15d commit c164d0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 9 additions & 1 deletion cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,16 @@ var entriesCreateCommand = &cobra.Command{
Use: "create",
Short: "create time entries",
Run: func(cmd *cobra.Command, args []string) {
if createProjectId == "" {
createProjectId = deps.conf.Get("projectId")
}

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

if createProjectId == "" || createServiceId == "" {
_, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id")
_, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id (either via arguments or config)")
return
}

Expand Down
13 changes: 7 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/spf13/viper"
"os"
"path/filepath"
)

type Config interface {
Expand All @@ -16,16 +15,18 @@ type Config interface {
}

type config struct {
fileName string
filePath string
fileType string
fileName string
filePath string
fileType string
fileFullPath 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}
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
return &config{fileName: fileName, filePath: filePath, fileType: fileType, fileFullPath: ffp}
}

func (c *config) GetApiUrl() string {
Expand Down Expand Up @@ -55,7 +56,7 @@ func (c *config) Set(key string, value string) {
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
err = viper.WriteConfigAs(filepath.Join(c.filePath, c.fileName))
err = viper.WriteConfigAs(c.fileFullPath)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
Expand Down

0 comments on commit c164d0b

Please sign in to comment.