-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.go
More file actions
171 lines (148 loc) · 3.42 KB
/
config.go
File metadata and controls
171 lines (148 loc) · 3.42 KB
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
162
163
164
165
166
167
168
169
170
171
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <zhaojh329@gmail.com>
*/
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/kylelemons/go-gypsy/yaml"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v3"
"github.com/zhaojh329/rtty-go/proto"
)
type Config struct {
group string
id string
host string
port uint16
description string
token string
heartbeat uint8
username string
reconnect bool
ssl bool
cacert string
sslcert string
sslkey string
insecure bool
}
func (cfg *Config) Parse(c *cli.Command) error {
var yamlCfg *yaml.File
var err error
conf := c.String("conf")
if conf != "" {
yamlCfg, err = yaml.ReadFile(conf)
if err != nil {
return fmt.Errorf(`read config file: %s`, err.Error())
}
}
fields := map[string]any{
"group": &cfg.group,
"id": &cfg.id,
"host": &cfg.host,
"port": &cfg.port,
"description": &cfg.description,
"token": &cfg.token,
"heartbeat": &cfg.heartbeat,
"username": &cfg.username,
"reconnect": &cfg.reconnect,
"ssl": &cfg.ssl,
"cacert": &cfg.cacert,
"cert": &cfg.sslcert,
"key": &cfg.sslkey,
"insecure": &cfg.insecure,
}
for name, opt := range fields {
if yamlCfg != nil {
if err := getConfigOpt(yamlCfg, name, opt); err != nil {
return err
}
}
getFlagOpt(c, name, opt)
}
getFlagOpt(c, "f", &cfg.username)
getFlagOpt(c, "a", &cfg.reconnect)
if cfg.id == "" {
return fmt.Errorf("you must specify an id for your device")
}
if strings.ContainsAny(cfg.id, " ") || len(cfg.id) > proto.MaximumDevIDLen {
return fmt.Errorf("invalid device id: must be 1-32 characters and cannot contain spaces")
}
if strings.ContainsAny(cfg.group, " ") || len(cfg.group) > proto.MaximumGroupLen {
return fmt.Errorf("invalid group: must be 1-16 characters and cannot contain spaces")
}
if len(cfg.description) > proto.MaximumDescLen {
return fmt.Errorf("description too long: must be 1-126 characters")
}
if cfg.heartbeat < 5 {
cfg.heartbeat = 5
log.Warn().Msgf("heartbeat interval too low, setting to minimum 5 seconds")
}
if runtime.GOOS != "windows" && os.Getuid() != 0 {
return fmt.Errorf("operation not permitted, must be run as root")
}
return nil
}
func getConfigOpt(yamlCfg *yaml.File, name string, opt any) error {
var num int64
var err error
switch opt := opt.(type) {
case *string:
var val string
val, err = yamlCfg.Get(name)
if err == nil {
*opt = val
}
case *bool:
var val bool
val, err = yamlCfg.GetBool(name)
if err == nil {
*opt = val
}
case *int, *uint, *uint8, *uint16:
num, err = yamlCfg.GetInt(name)
if err == nil {
switch opt := opt.(type) {
case *int:
*opt = int(num)
case *uint:
*opt = uint(num)
case *uint8:
*opt = uint8(num)
case *uint16:
*opt = uint16(num)
}
}
default:
return fmt.Errorf("unsupported type for option %s", name)
}
if err != nil {
if _, ok := err.(*yaml.NodeNotFound); ok {
return nil
}
return fmt.Errorf(`invalud "%s": %w`, name, err)
}
return nil
}
func getFlagOpt(c *cli.Command, name string, opt any) {
if !c.IsSet(name) {
return
}
switch opt := opt.(type) {
case *string:
*opt = c.String(name)
case *int:
*opt = c.Int(name)
case *uint:
*opt = c.Uint(name)
case *uint8:
*opt = c.Uint8(name)
case *uint16:
*opt = c.Uint16(name)
case *bool:
*opt = c.Bool(name)
}
}