Skip to content

Commit

Permalink
config: refactored constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 8, 2019
1 parent c3f182e commit 8af0e69
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/spf13/viper"
"os"
"runtime"
)

type Config interface {
Expand Down Expand Up @@ -31,10 +32,31 @@ func NewConfig(fileName, filePath, fileType string) Config {
viper.AddConfigPath("$HOME")
viper.SetConfigName(fileName)
viper.SetConfigType(fileType)
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
ffp := fullConfigPath(filePath, fileName, fileType)
createConfigFileIfNonExistent(ffp)

return &config{fileName: fileName, filePath: filePath, fileType: fileType, fileFullPath: ffp}
}

func fullConfigPath(filePath string, fileName string, fileType string) string {
ffp := fmt.Sprintf("%s/%s.%s", filePath, fileName, fileType)
if runtime.GOOS == "windows" {
ffp = fmt.Sprintf("%s\\%s.%s", filePath, fileName, fileType)
}
return ffp
}

func createConfigFileIfNonExistent(ffp string) {
if _, err := os.Stat(ffp); os.IsExist(err) {
return
}

_, err := os.Create(ffp)
if err != nil {
panic(fmt.Sprintf("config file does not exists and wasn't able to create it: %s\n", err))
}
}

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

0 comments on commit 8af0e69

Please sign in to comment.