-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
82 lines (67 loc) · 1.56 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
// Configuration for rdevcon
package main
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
//go:embed config.json
var config_json string
type Config struct {
DevicesPath string
TunnelKeyPath string
TunnelNameAddr string
SelfUpdatePath string
PortOffset int
Forwards string
AnonUser string
Verbose bool
SshOptionList []string
UseLoopbackAddrs bool
}
var config *Config
func ConfigLoad() *Config {
config = &Config{}
// Embedded config.json is loaded first.
err := json.Unmarshal([]byte(config_json), config)
if err != nil {
fmt.Println("embedded config error:", err)
}
// If a local config.json is found, it can override
// any or all values
if _, err := os.Stat("config.json"); err == nil {
if data, err := os.ReadFile("config.json"); err == nil {
if err := json.Unmarshal(data, config); err != nil {
fmt.Println("local config error:", err)
}
} else {
fmt.Println("local config read:", err)
}
}
// Replace placeholders in SelfUpdatePath
config.SelfUpdatePath = strings.Replace(config.SelfUpdatePath,
"$platform",
runtime.GOOS+"-"+runtime.GOARCH,
1)
executable, _ := os.Executable()
config.SelfUpdatePath = strings.Replace(config.SelfUpdatePath,
"$argv0",
filepath.Base(executable),
1)
setLoopback(config.UseLoopbackAddrs)
return config
}
func (config *Config) sshOptions() string {
options := ""
if config.Verbose {
options += " -v"
}
for _, option := range config.SshOptionList {
options += " " + option
}
return options
}