-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
166 lines (135 loc) · 3.14 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"github.com/MichaelThessel/gomainr/app"
"github.com/MichaelThessel/gomainr/cache"
"github.com/MichaelThessel/gomainr/file"
"github.com/MichaelThessel/gomainr/search"
"github.com/MichaelThessel/gomainr/search/source"
"github.com/BurntSushi/toml"
)
type configPaths struct {
configFile string
baseDir string
dataDir string
}
type config struct {
DNS *source.DNSConfig
NameCheap *source.NameCheapConfig
GoDaddy *source.GoDaddyConfig
}
var c *config
var a *app.App
var cp *configPaths
func init() {
if err := initPaths(); err != nil {
log.Panic(err)
}
if err := generateConfig(); err != nil {
log.Panic(err)
}
if err := loadConfig(); err != nil {
log.Panic(err)
}
}
func main() {
s := initSearch()
a = app.New(s)
defer a.Close()
// Main loop
a.Loop()
}
// initPaths sets config and data storage paths
func initPaths() error {
configFile := "config"
baseDir := ".gomainr"
dataDir := "data"
usr, err := user.Current()
if err != nil {
return err
}
cp = new(configPaths)
cp.baseDir = usr.HomeDir + string(os.PathSeparator) + baseDir
cp.dataDir = cp.baseDir + string(os.PathSeparator) + dataDir
cp.configFile = cp.baseDir + string(os.PathSeparator) + configFile
return nil
}
// initSearch initializes the searcher
func initSearch() *search.Search {
var searchSource source.Source
if c.DNS != nil && c.DNS.Enabled {
searchSource = source.Get(c.DNS, source.DNSSource)
} else if c.NameCheap != nil && c.NameCheap.Enabled {
searchSource = source.Get(c.NameCheap, source.NameCheapSource)
} else if c.GoDaddy != nil && c.GoDaddy.Enabled {
searchSource = source.Get(c.GoDaddy, source.GoDaddySource)
} else {
fmt.Println("No search source enabled please update:", cp.configFile)
os.Exit(1)
}
cache := cache.New(cp.dataDir)
return search.New(searchSource, cache)
}
// generateConfig generates config files and directories
func generateConfig() error {
// Create base directory
if err := file.CreateDirectory(cp.baseDir, 0700); err != nil {
return err
}
// Create data directory
if err := file.CreateDirectory(cp.dataDir, 0700); err != nil {
return err
}
// Create config file
if err := createConfigFile(cp.configFile); err != nil {
return err
}
return nil
}
// loadConfig loads the configuration from the config file
func loadConfig() error {
configData, err := ioutil.ReadFile(cp.configFile)
if err != nil {
return err
}
if _, err := toml.Decode(string(configData), &c); err != nil {
return err
}
return nil
}
// createConfigFile creates the default config file
func createConfigFile(fileName string) error {
fd, created, err := file.CreateFile(fileName)
if err != nil {
return err
}
// Exit if no new file was created
if created == false {
return nil
}
if err := initConfig(fd); err != nil {
return err
}
return nil
}
// initConfig writes the default config to config file
func initConfig(fd *os.File) error {
defaultConfig := `[dns]
Enabled = true
[namecheap]
APIUser = ""
APIToken = ""
UserName = ""
Enabled = false
[godaddy]
Key = ""
Secret = ""
Enabled = false
`
_, err := fd.WriteString(defaultConfig)
return err
}