Skip to content

Commit c164d0b

Browse files
committed
entries: default project & service Ids can be set via config
* config only provides fallback values * cmd line arguments override defaults
1 parent a3ce15d commit c164d0b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

cmd/entries.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,16 @@ var entriesCreateCommand = &cobra.Command{
9696
Use: "create",
9797
Short: "create time entries",
9898
Run: func(cmd *cobra.Command, args []string) {
99+
if createProjectId == "" {
100+
createProjectId = deps.conf.Get("projectId")
101+
}
102+
103+
if createServiceId == "" {
104+
createServiceId = deps.conf.Get("serviceId")
105+
}
106+
99107
if createProjectId == "" || createServiceId == "" {
100-
_, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id")
108+
_, _ = fmt.Fprintln(os.Stderr, "please set both the project AND service id (either via arguments or config)")
101109
return
102110
}
103111

config/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"github.com/spf13/viper"
66
"os"
7-
"path/filepath"
87
)
98

109
type Config interface {
@@ -16,16 +15,18 @@ type Config interface {
1615
}
1716

1817
type config struct {
19-
fileName string
20-
filePath string
21-
fileType string
18+
fileName string
19+
filePath string
20+
fileType string
21+
fileFullPath string
2222
}
2323

2424
func NewConfig(fileName, filePath, fileType string) Config {
2525
viper.AddConfigPath("$HOME")
2626
viper.SetConfigName(fileName)
2727
viper.SetConfigType(fileType)
28-
return &config{fileName: fileName, filePath: filePath, fileType: fileType}
28+
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
29+
return &config{fileName: fileName, filePath: filePath, fileType: fileType, fileFullPath: ffp}
2930
}
3031

3132
func (c *config) GetApiUrl() string {
@@ -55,7 +56,7 @@ func (c *config) Set(key string, value string) {
5556
if err != nil {
5657
_, _ = fmt.Fprintln(os.Stderr, err)
5758
}
58-
err = viper.WriteConfigAs(filepath.Join(c.filePath, c.fileName))
59+
err = viper.WriteConfigAs(c.fileFullPath)
5960
if err != nil {
6061
_, _ = fmt.Fprintln(os.Stderr, err)
6162
}

0 commit comments

Comments
 (0)