-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (63 loc) · 1.36 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
)
var configPath = os.Getenv("HOME") + "/.config/vasle/"
var configFile = configPath + "config.toml"
type configSchema struct {
Key string `toml:"key"`
Port int `toml:"port"`
Client struct {
Url string `toml:"url"`
} `toml:"client"`
}
var c configSchema
var status bool
var serverTicker *time.Ticker
func init() {
if _, err := os.Stat(configFile); err == nil {
file, err := os.Open(filepath.Clean(configFile))
if err != nil {
log.Fatal(err)
}
defer file.Close()
if _, err = toml.NewDecoder(file).Decode(&c); err != nil {
log.Fatal(err)
}
} else {
err := os.MkdirAll(configPath, 0755)
if err != nil {
log.Fatal(err)
}
file, err := os.Create(filepath.Clean(configFile))
if err != nil {
log.Fatal(err)
}
c.Key = "0123456789abcdef"
c.Port = 9090
c.Client.Url = "http://localhost:9090/update"
if err := toml.NewEncoder(file).Encode(c); err != nil {
log.Fatal(err)
}
if err := file.Close(); err != nil {
log.Fatal(err)
}
}
}
func main() {
command := os.Args[1]
switch command {
case "server":
serverTicker = time.NewTicker(15 * time.Second)
runServer(c.Port, c.Key)
case "client":
runClient(c.Client.Url, c.Key)
case "init":
fmt.Println("Config file created at: ~/.config/vasle/config.toml")
}
}