-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
87 lines (72 loc) · 1.58 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
package main
import (
"encoding/json"
"io"
"os"
"runtime"
)
type Config struct {
SyntaxHighlightingStyle string `json:"syntax_highlighting_style"`
LineNumberColor string `json:"line_number_color"`
DisallowedFileTypes []string `json:"disallowed_file_types"`
MaxFileSize int64 `json:"max_file_size"`
}
func (c *Config) JSON() ([]byte, error) {
return json.MarshalIndent(c, " ", " ")
}
func (c *Config) Write(w io.Writer) (int, error) {
b, err := c.JSON()
if err != nil {
return 0, err
}
return w.Write(b)
}
func init() {
home := homeDir()
if _, err := os.Stat(home + "/.config/xcat"); os.IsNotExist(err) {
os.Mkdir(home+"/.config/xcat", 0755)
}
c := &Config{
SyntaxHighlightingStyle: "monokai",
LineNumberColor: "#677d8a",
DisallowedFileTypes: []string{"exe", "dll", "so", "dylib", "bin", "o", "a", "lib"},
MaxFileSize: 0,
}
f, err := os.Create(home + "/.config/xcat/config.json")
if err != nil {
panic(err)
}
defer f.Close()
_, err = c.Write(f)
if err != nil {
panic(err)
}
}
func homeDir() string {
home, _ := os.UserHomeDir()
if home == "" {
switch goos := runtime.GOOS; goos {
case "windows":
home = os.Getenv("USERPROFILE")
case "darwin", "linux":
home = os.Getenv("HOME")
default:
home = "."
}
}
return home
}
func loadConfig() (*Config, error) {
home := homeDir()
f, err := os.Open(home + "/.config/xcat/config.json")
if err != nil {
return nil, err
}
defer f.Close()
c := &Config{}
err = json.NewDecoder(f).Decode(c)
if err != nil {
return nil, err
}
return c, nil
}