-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
161 lines (134 loc) · 3.77 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
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
// +build windows
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"text/tabwriter"
"github.com/StackExchange/wmi"
"github.com/pkg/errors"
)
type config struct {
PNPDeviceID string `json:"pnp_device_id"`
BaudRate int `json:"baud_rate"`
LogDir string `json:"log_dir"`
}
var configData *config
func loadConfig(path string) {
_, err := os.Stat(path)
if err != nil {
panic("config.json not found")
}
if configData == nil {
configData = new(config)
buff, err := ioutil.ReadFile(path)
if err != nil {
panic(errors.Wrap(err, "Problem reading config file"))
}
err = json.Unmarshal(buff, configData)
if err != nil {
panic(errors.Wrap(err, "Problem unmarshaling config structure"))
}
}
}
func configPath() string {
exepath, err := exePath()
if err != nil {
panic("Couldn't find executable path: " + err.Error())
}
return filepath.FromSlash(filepath.Dir(exepath) + "/config.json")
}
// InteractiveConfig prompts the user for the necessary config parameters
// and writes them to config.json
func interactiveConfig() error {
var dst []serialPort
client := &wmi.Client{AllowMissingFields: true}
err := client.Query("SELECT DeviceID, MaxBaudRate, PNPDeviceID, Description FROM Win32_SerialPort", &dst)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "#\tDevice ID\tMax Baud Rate\tDescription")
for i, device := range dst {
fmt.Fprintf(w, "%d)\t%s\t%d\t%s\n", i+1, device.DeviceID, device.MaxBaudRate, device.Description)
}
w.Flush()
fmt.Printf("\nEnter selection or c to cancel: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
deviceSelection := scanner.Text()
deviceIndex, err := strconv.ParseInt(deviceSelection, 10, 64)
if err != nil {
fmt.Println("Cancelled")
return nil
}
if int(deviceIndex) > len(dst) || deviceIndex < 1 {
return fmt.Errorf("Invalid selection: %s", deviceSelection)
}
pnp := dst[deviceIndex-1].PNPDeviceID
fmt.Printf("Enter baud rate: ")
scanner.Scan()
baudRateSelection := scanner.Text()
baudRate, err := strconv.ParseInt(baudRateSelection, 10, 32)
if err != nil {
return fmt.Errorf("Invalid selection: %s", baudRateSelection)
}
currUser, _ := user.Current()
path := filepath.ToSlash(currUser.HomeDir) + "/Saved Games/Frontier Developments/Elite Dangerous"
pathInfo, err := os.Stat(filepath.FromSlash(path))
isPathValid := err == nil && pathInfo.IsDir()
if !isPathValid {
fmt.Printf("Enter the Elite Dangerous log path: ")
} else {
fmt.Printf("\nDefault logs folder: %s\n", path)
fmt.Printf("Enter a different one, or press enter to use the above: ")
}
scanner.Scan()
logDirSelection := scanner.Text()
logDir := ""
if len(logDirSelection) > 2 {
logDirInfo, err := os.Stat(filepath.FromSlash(logDirSelection))
if err != nil || !logDirInfo.IsDir() {
return fmt.Errorf("Couldn't find the selected log path: " + err.Error())
}
logDir = logDirSelection
} else if isPathValid {
logDir = path
} else {
return fmt.Errorf("Must enter a valid path")
}
conf := config{pnp, int(baudRate), logDir}
confBytes, err := json.Marshal(conf)
if err != nil {
return fmt.Errorf("Couldn't marshal config into JSON: %s", err.Error())
}
err = ioutil.WriteFile(configPath(), confBytes, 0777)
if err != nil {
return fmt.Errorf("Couldn't write config.json file: %s", err.Error())
}
fmt.Println("Wrote config to " + filepath.ToSlash(configPath()))
return nil
}
func getPNPDeviceID() string {
if configData == nil {
loadConfig(configPath())
}
return configData.PNPDeviceID
}
func getBaudRate() int {
if configData == nil {
loadConfig(configPath())
}
return configData.BaudRate
}
func getLogDir() string {
if configData == nil {
loadConfig(configPath())
}
return configData.LogDir
}