-
Notifications
You must be signed in to change notification settings - Fork 3
/
args.go
67 lines (58 loc) · 1.89 KB
/
args.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
package main
import (
"errors"
"fmt"
"strings"
"gopkg.in/urfave/cli.v1"
)
type ArgConfig struct {
// FreeSWITCH
FreeswitchHost string
FreeswitchPort int
FreeswitchEslPassword string
FreeswitchSofiaProfiles []string
FreeswitchAdvertiseIp string
FreeswitchAdvertisePort int
// Key/Value Store
KvBackend string
KvHost string
KvPort int
KvPrefix string
//
SyncInterval uint32
}
func parseFlags(c *cli.Context) (*ArgConfig, error) {
var result ArgConfig
for _, v := range []string{"fshost", "fspassword", "fsprofiles", "fsadvertiseip", "kvhost", "kvprefix"} {
if len(c.String(v)) == 0 {
return new(ArgConfig), fmt.Errorf("Error: --%s must not be empty.", v)
}
}
for _, v := range []string{"fsport", "fsadvertiseport", "kvport"} {
if c.Int(v) <= 0 {
return new(ArgConfig), fmt.Errorf("Error: --%s must not be 0 (or empty).", v)
}
if c.Int(v) > 65536 {
return new(ArgConfig), fmt.Errorf("Error: --%s must be below 65536.", v)
}
}
result.FreeswitchHost = c.String("fshost")
result.FreeswitchPort = c.Int("fsport")
result.FreeswitchEslPassword = c.String("fspassword")
result.FreeswitchAdvertiseIp = c.String("fsadvertiseip")
result.FreeswitchAdvertisePort = c.Int("fsadvertiseport")
result.KvHost = c.String("kvhost")
result.KvPort = c.Int("kvport")
result.KvPrefix = c.String("kvprefix")
available_backends := availableKvBackends()
if stringInSlice(c.String("kvbackend"), available_backends) != true {
return new(ArgConfig), fmt.Errorf("Error: --kvbackend must be one of: %s", strings.Join(available_backends, ", "))
}
result.KvBackend = c.String("kvbackend")
if uint32(c.Int("syncinterval")) <= 0 {
return new(ArgConfig), errors.New("Error: --syncinterval must not be 0 (or empty).")
}
result.SyncInterval = uint32(c.Int("syncinterval"))
result.FreeswitchSofiaProfiles = strings.Split(c.String("fsprofiles"), ",")
return &result, nil
}