-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
149 lines (128 loc) · 3.46 KB
/
parse.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
package socketcmd
/* Copyright 2017 Ryan Clarke
This file is part of Socketcmd.
Socketcmd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Socketcmd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Socketcmd. If not, see <http://www.gnu.org/licenses/>
*/
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
// Read unlimited lines until the default timeout
DefaultHeader = "-1:"
// Command not allowed (will not be sent to Wrapper)
ForbiddenHeader = "-:"
// Do not wait for any response
EmptyHeader = ":"
headerRegexp = regexp.MustCompile("^-?[0-9]*:[0-9]*$")
ErrMissingHeader = fmt.Errorf("missing or invalid socketcmd header")
ErrCommandForbidden = fmt.Errorf("the provided command is not allowed")
)
// A ParseFunc determines the proper header for a given command sequence.
type ParseFunc func(args []string) string
// Header representation of the given line count and timeout.
func Header(lines, timeout int) string {
if timeout <= 0 {
if lines == 0 {
return EmptyHeader
}
return fmt.Sprintf("%d:", lines)
}
if lines == 0 {
return fmt.Sprintf(":%d", timeout)
}
return fmt.Sprintf("%d:%d", lines, timeout)
}
// ParseHeader extracts the line count and timeout from the given header.
func ParseHeader(header string) (lines, timeout int, err error) {
if !headerRegexp.MatchString(header) {
return 0, 0, ErrMissingHeader
}
s := strings.Split(header, ":")
if len(s) != 2 {
return 0, 0, ErrMissingHeader
}
if s[0] != "" {
lines, err = strconv.Atoi(s[0])
if err != nil {
return
}
}
if s[1] != "" {
timeout, err = strconv.Atoi(s[1])
}
return
}
func DefaultParseFunc(_ []string) string {
return DefaultHeader
}
func NewArguments(table map[string]string, defaultHeader string) Argument {
a := Argument{make(map[string]Argument, len(table)), defaultHeader}
for cmd, header := range table {
a.Args[cmd] = Argument{nil, header}
}
return a
}
type Argument struct {
Args map[string]Argument
Header string
}
func (a *Argument) Match(args []string) string {
// Use default if header is omitted
if a.Header == "" {
a.Header = DefaultHeader
}
if len(args) < 1 {
return a.Header
}
if arg, ok := a.Args[args[0]]; ok {
// Propagate headers to child elements
if arg.Header == "" {
arg.Header = a.Header
}
if len(args) < 2 {
//return arg.Match([]string{})
}
return arg.Match(args[1:])
}
return a.Header
}
func (a *Argument) AddArguments(table map[string]string) {
if a.Args == nil {
a.Args = make(map[string]Argument, len(table))
}
for arg, header := range table {
if header == "" {
header = a.Header
}
a.Args[arg] = Argument{nil, header}
}
}
func (a *Argument) AddNestedArguments(table map[string]map[string]string) {
if a.Args == nil {
a.Args = make(map[string]Argument, len(table))
}
for cmd, args := range table {
if arg, ok := a.Args[cmd]; ok {
arg.AddArguments(args)
continue
}
a.Args[cmd] = NewArguments(args, a.Header)
}
}
func (a *Argument) ParseFunc() ParseFunc {
return func(args []string) string {
return a.Match(args)
}
}