-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
85 lines (73 loc) · 1.76 KB
/
config.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package config
import (
"fmt"
"os"
"os/user"
"path"
"github.com/spf13/viper"
)
const (
configDir = ".config/"
brewDir = "brew/"
portDir = "port/"
nixDir = "nix/"
gitDir = ".git/"
)
func Init() {
// brew bundle dir
if _, err := os.Stat(getDirPath(brewDir)); os.IsNotExist(err) {
err = os.MkdirAll(getDirPath(brewDir), os.ModeDir|0755)
if err != nil {
panic(err)
}
}
// port bundle dir
if _, err := os.Stat(getDirPath(portDir)); os.IsNotExist(err) {
err = os.MkdirAll(getDirPath(portDir), os.ModeDir|0755)
if err != nil {
panic(err)
}
}
// nix bundle dir
if _, err := os.Stat(getDirPath(nixDir)); os.IsNotExist(err) {
err = os.MkdirAll(getDirPath(nixDir), os.ModeDir|0755)
if err != nil {
panic(err)
}
}
// config file dir
if _, err := os.Stat(getDirPath(configDir)); os.IsNotExist(err) {
err = os.Mkdir(getDirPath(configDir), os.ModeDir|0755)
if err != nil {
panic(err)
}
}
if _, err := os.Stat(getConfigPath()); os.IsNotExist(err) {
err = os.WriteFile(getConfigPath(), []byte("{}"), 0600)
if err != nil {
panic(err)
}
}
viper.SetConfigFile(getConfigPath())
viper.SetDefault("BASE_URL", "https://api.github.com")
viper.SetDefault("BREW_DIR", getDirPath(brewDir))
viper.SetDefault("PORT_DIR", getDirPath(portDir))
viper.SetDefault("PORT_DIR", getDirPath(portDir))
viper.SetDefault("NIX_DIR", getDirPath(nixDir))
viper.SetDefault("REPO_NAME", "prp-backup-repo")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
}
func getConfigPath() string {
return path.Join(getDirPath(configDir), "prp.json")
}
func getDirPath(dirName string) string {
usr, err := user.Current()
if err != nil {
fmt.Println("can't get your home directory.")
os.Exit(1)
}
return path.Join(usr.HomeDir, "/.prp/"+dirName)
}