-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (70 loc) · 1.95 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
80
81
82
83
84
85
86
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
)
type Config struct {
PathDeviceFile string
PathPrometheusFile string
}
var devices = map[string]usbDevice{}
var pathRuleFile = "/etc/udev/rules.d/85-usb-device.rules"
var appDir = getAppDir()
var pathAppFile = appDir + "/prometheus-usb-detection"
var pathDevicesFile = appDir + "/devices.json"
var pathPrometheusFile = appDir + "/usb-device.prom"
func main() {
fmt.Println("--- USB-DETECTION ---")
// initialize default Config Values
myConfig := Config{pathDevicesFile, pathPrometheusFile}
// read config from file
if fileExists("./config.json") {
myConfig = readConfig(myConfig)
}
//read envvars
// fill map with udevrule devices
readRegisteredDevices()
// add subcommands
registerCmd := flag.NewFlagSet("register", flag.ExitOnError)
updateStateCmd := flag.NewFlagSet("updateState", flag.ExitOnError)
addID := updateStateCmd.String("add", "", "Tell Prometheus which device has been added")
removeID := updateStateCmd.String("remove", "", "Tell prometheus which device has been removed")
// parse here of when opening command?
//flag.Parse()
// if args < 1 default to register
if len(os.Args) <= 1 {
register(registerCmd)
} else {
switch os.Args[1] {
case "register":
register(registerCmd)
case "updateState":
updateStateCmd.Parse(os.Args[2:])
updateState(updateStateCmd, addID, removeID)
default:
register(registerCmd)
}
}
}
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func readConfig(myConfig Config) Config {
bytes, err := os.ReadFile("./config.json")
check(err)
defaultConfig := "{\"PathDeviceFile\":\"./devices.json\",\"PathPrometheusFile\":\"./usb-device.prom\"}"
err = json.Unmarshal(bytes, &myConfig)
if err != nil || string(bytes) == defaultConfig {
fmt.Println("true")
myConfig = Config{pathDevicesFile, pathPrometheusFile}
}
return myConfig
}