-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
66 lines (56 loc) · 1.88 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"os"
"os/user"
"github.com/jurocknsail/gojira/cmd"
"github.com/spf13/viper"
)
func main() {
initConfigFolder()
cmd.Execute()
}
func initConfigFolder() {
usr, usrErr := user.Current()
if usrErr != nil {
log.Fatal(usrErr)
}
configPath := usr.HomeDir + "/.gojira"
configFile := "gojira"
configFilePath := configPath + "/gojira.yaml"
dodFilePath := configPath + "/dod.yaml"
fileMode := os.ModePerm
//Create config folder if not exists
if _, statErr := os.Stat(configPath); os.IsNotExist(statErr) {
direrr := os.Mkdir(configPath, fileMode)
if direrr != nil {
panic(direrr)
}
//Create the config file if not exists
if _, stateFileErr := os.Stat(configFilePath); os.IsNotExist(stateFileErr) {
os.OpenFile(configFilePath, os.O_RDONLY|os.O_CREATE, 0666)
}
//Create the config file if not exists
if _, stateDoDFileErr := os.Stat(dodFilePath); os.IsNotExist(stateDoDFileErr) {
os.OpenFile(dodFilePath, os.O_RDONLY|os.O_CREATE, 0666)
}
}
//Create the config file if not exists, in case the folder was there but empty
if _, stateFileErr := os.Stat(configFilePath); os.IsNotExist(stateFileErr) {
os.OpenFile(configFilePath, os.O_RDONLY|os.O_CREATE, 0666)
}
//Create the config file if not exists, in case the folder was there but empty
if _, stateDoDFileErr := os.Stat(dodFilePath); os.IsNotExist(stateDoDFileErr) {
os.OpenFile(dodFilePath, os.O_RDONLY|os.O_CREATE, 0666)
}
// Init the actual config using viper
viper.SetConfigName(configFile) // name of config file (without extension)
viper.AddConfigPath(configPath) // call multiple times to add many search paths
viper.AddConfigPath(dodFilePath)
viper.SetConfigType("yaml")
readerr := viper.ReadInConfig() // Find and read the config file
if readerr != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file %s", readerr))
}
}