Skip to content

Commit 8af0e69

Browse files
committed
config: refactored constructor
1 parent c3f182e commit 8af0e69

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

config/config.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/spf13/viper"
66
"os"
7+
"runtime"
78
)
89

910
type Config interface {
@@ -31,10 +32,31 @@ func NewConfig(fileName, filePath, fileType string) Config {
3132
viper.AddConfigPath("$HOME")
3233
viper.SetConfigName(fileName)
3334
viper.SetConfigType(fileType)
34-
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
35+
ffp := fullConfigPath(filePath, fileName, fileType)
36+
createConfigFileIfNonExistent(ffp)
37+
3538
return &config{fileName: fileName, filePath: filePath, fileType: fileType, fileFullPath: ffp}
3639
}
3740

41+
func fullConfigPath(filePath string, fileName string, fileType string) string {
42+
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
43+
if runtime.GOOS == "windows" {
44+
ffp = fmt.Sprintf("%s\\%s.%s", filePath, fileName, fileType)
45+
}
46+
return ffp
47+
}
48+
49+
func createConfigFileIfNonExistent(ffp string) {
50+
if _, err := os.Stat(ffp); os.IsExist(err) {
51+
return
52+
}
53+
54+
_, err := os.Create(ffp)
55+
if err != nil {
56+
panic(fmt.Sprintf("config file does not exists and wasn't able to create it: %s\n", err))
57+
}
58+
}
59+
3860
func (c *config) GetApiUrl() string {
3961
return c.Get("api.url")
4062
}

0 commit comments

Comments
 (0)