-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.go
126 lines (115 loc) · 3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
)
type Config struct {
TLD string `json:"tld"`
Addr string `json:"addr"`
TLSAddr string `json:"tls_addr"`
CAKeyFile string `json:"ca_key_file"`
CACertFile string `json:"ca_cert_file"`
UseMkcert bool `json:"mkcert"`
Entries []*Entry `json:"entries"`
}
var defaultConfig = &Config{
Addr: "localhost:7999",
TLSAddr: "localhost:7998",
TLD: "wip",
Entries: []*Entry{{
Source: "example.wip",
DestHost: "localhost:8000",
}},
}
type configManager struct {
searchPaths []string
}
// newConfigManager sets up all the search paths and configPath
func newConfigManager() (*configManager, error) {
cm := &configManager{}
c := os.Getenv("XDG_CONFIG_HOME")
if c != "" {
cm.searchPaths = append(cm.searchPaths, path.Join(c, "lightproxy"))
}
cm.searchPaths = append(cm.searchPaths, getHomeConfigDir())
return cm, nil
}
// configPath() returns the active config file path, config file dir
// and whether it exists or not.
// This checks all search paths for an existing config file
// XDG_CONFIG_HOME is the preferred path, but also fallback
// gracefully to $HOME/.config
func (cm *configManager) configPath() (string, string, bool) {
// default config path is the first search path
configDir := cm.searchPaths[0]
for _, dir := range cm.searchPaths {
configPath := path.Join(dir, "config.json")
fi, err := os.Stat(configPath)
if fi != nil && err == nil {
return configPath, dir, true
}
if !os.IsNotExist(err) {
// fmt.Printf("unknown error: %s\n", err)
}
}
return path.Join(configDir, "config.json"), configDir, false
}
func (cm *configManager) ensureAndLoad() (*Config, error) {
err := cm.ensure()
if err != nil {
return nil, err
}
configPath, _, exists := cm.configPath()
config := &Config{}
if exists {
f, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
err = json.Unmarshal(f, config)
if err != nil {
return nil, err
}
}
return config, nil
}
// writeConfig writes to the existing config file, or the first search path
func (cm *configManager) writeConfig(config *Config) error {
configPath, _, _ := cm.configPath()
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(configPath, b, os.ModePerm)
}
func (cm *configManager) ensure() error {
configPath, configDir, exists := cm.configPath()
if exists {
return nil
} else {
err := os.MkdirAll(configDir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create dir %s: %s", configDir, err)
}
}
err := cm.writeConfig(defaultConfig)
if err != nil {
return fmt.Errorf("failed to to create config.json file: %s", err)
}
fmt.Printf("created config.json file: %s\n", configPath)
return nil
}
func getHomeConfigDir() string {
u, err := user.Current()
if uid := os.Getenv("SUDO_UID"); uid != "" {
u, err = user.LookupId(uid)
}
if err != nil {
panic(err)
}
return path.Join(u.HomeDir, ".config", "lightproxy")
}