-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
102 lines (90 loc) · 2.36 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
func configDefaults(config *viper.Viper, token string) {
defaults := map[string]interface{}{
"token": token,
"enterprise": map[string]string{
"url": "",
},
"gists": map[string]interface{}{
"backupdir": os.Getenv("HOME") + "/.ghb/backups/gists",
"backupregex": "",
"deleteregex": "",
"retention": 0,
"fileonly": true,
"prompt": false,
},
"starred": map[string]interface{}{
"backupdir": os.Getenv("HOME") + "/.ghb/backups/starred",
"shallow": true,
"prompt": false,
},
"repos": map[string]interface{}{
"backupdir": os.Getenv("HOME") + "/.ghb/backups/repos",
"shallow": true,
"prompt": false,
"owner": true,
"collaborator": true,
"orgmember": false,
},
}
for key, value := range defaults {
config.SetDefault(key, value)
}
}
func configSetup() {
resp := ""
dir := os.Getenv("HOME") + "/.ghb"
fmt.Printf("Full path of directory to save config to [ empty for %s ] : ", dir)
i, err := fmt.Scanln(&resp)
if i > 0 && err != nil {
Error.Printf("Failed to read input for config file path\n%s\n", err)
shutdown(1)
}
if resp == "" {
resp = dir
}
if _, err := os.Stat(resp); os.IsNotExist(err) {
err = os.MkdirAll(resp, 0755)
if err != nil {
Error.Printf("Failed to create directory %s\n%s\n", resp, err)
}
}
err = config.WriteConfigAs(resp + "/config.toml")
if err != nil {
Error.Printf("Failed to write config file to %s\n%s", resp, err)
shutdown(1)
}
Info.Printf("Saved default config file to %s/config.toml", resp)
if resp != (dir) {
Info.Printf("Make edits as desired then rerun gcb with '-c %s/config.toml' to use\n", resp)
} else {
Info.Printf("Make edits as desired then rerun gcb to use\n")
}
shutdown(0)
}
func configure(filename string, token string) {
config = viper.New()
config.AutomaticEnv()
configDefaults(config, token)
if filename == "" {
if _, err := os.Stat(os.Getenv("HOME") + "/.ghb/config.toml"); os.IsNotExist(err) {
if prompt("No config file found, would you like to create one?") {
configSetup()
}
return
}
config.SetConfigFile(os.Getenv("HOME") + "/.ghb/config.toml")
} else {
config.SetConfigFile(filename)
}
err := config.ReadInConfig()
if err != nil {
Error.Printf("Could not parse config file %s\n%s", filename, err)
shutdown(1)
}
}