-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.go
46 lines (36 loc) · 1.17 KB
/
configuration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
var conf Args
func loadConfig() error {
// Set the file name of the configurations file
viper.SetConfigName(configFileName)
// Set the path to look for the configurations file
viper.AddConfigPath(confFilePath)
// Set config type to yaml
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
if strings.Contains(err.Error(), "Not Found") {
fmt.Println("Configuration file not found. Creating configuration file...")
defaultConfig := []byte(defaultConfig())
if err := os.WriteFile(filepath.Join(confFilePath, configFileName), defaultConfig, 0644); err != nil {
checkForFatalErr(fmt.Errorf("error creating configuration file: %v", err))
} else {
Log.Info("Configuration file \"%v\" created.", filepath.Join(confFilePath, configFileName))
fmt.Println("Please edit default values!")
exit(0, nil)
}
} else {
checkForFatalErr(fmt.Errorf("error reading configuration file: %v", err))
}
}
if err := viper.Unmarshal(&conf); err != nil {
checkForFatalErr(fmt.Errorf("unable to decode configuration file structure: %v", err))
}
return nil
}