-
Notifications
You must be signed in to change notification settings - Fork 64
/
executable.go
203 lines (163 loc) · 4.2 KB
/
executable.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package exec
import (
"fmt"
"reflect"
"strings"
)
type Executable struct {
bin string
args []string
}
// NewExecutable accepts an input argument bin which is the path or executable
// name to be ultimately executed. An optional positional argument args can be
// provided which is of an interface type. The interface can use the attribute
// annotation tags `flag:"--myarg"` to aid serialization and organization of the
// executable's command-line arguments. The type of the attribute will derive
// what is passed to the flag.
func NewExecutable(bin string, face interface{}, args ...string) (*Executable, error) {
if len(bin) == 0 {
return nil, fmt.Errorf("binary argument cannot be empty")
}
e := &Executable{}
if strings.Contains(bin, " ") {
args := strings.Split(bin, " ")
bin = args[0]
e.args = args[1:]
}
e.args = append(e.args, args...)
e.bin = bin
if face != nil {
ifaceArgs, err := ParseInterfaceArgs(face)
if err != nil {
return nil, err
}
e.args = append(e.args, ifaceArgs...)
}
return e, nil
}
func (e *Executable) Args() []string {
return e.args
}
type flag struct {
flag string
omitvalueif string
}
func parseFlag(tag reflect.StructTag) (*flag, error) {
parts := strings.Split(tag.Get("flag"), ",")
if len(parts) == 0 {
return nil, fmt.Errorf("could not parse flag without tag")
}
f := &flag{
flag: parts[0],
}
for _, part := range parts[1:] {
switch true {
case strings.HasPrefix(part, "omitvalueif"):
omit := strings.Split(part, "=")
if len(omit) == 1 {
return nil, fmt.Errorf("omitvalueif requires value")
}
f.omitvalueif = omit[1]
default:
continue
}
}
return f, nil
}
// ParseInterfaceArgs returns the array of arguments detected from an interface
// with tag annotations `flag`
func ParseInterfaceArgs(face interface{}, args ...string) ([]string, error) {
if face != nil && reflect.ValueOf(face).Kind() == reflect.Ptr {
return nil, fmt.Errorf("cannot derive interface arguments from pointer: passed by reference")
}
t := reflect.TypeOf(face)
v := reflect.ValueOf(face)
for i := 0; i < t.NumField(); i++ {
f, err := parseFlag(t.Field(i).Tag)
if err != nil {
continue
}
if len(f.flag) > 0 {
switch v.Field(i).Kind().String() {
case "ptr":
if v.Field(i).IsZero() { // if nil
continue
}
value := fmt.Sprintf("%d", reflect.Indirect(v.Field(i)).Int())
if value == f.omitvalueif {
args = append(args, f.flag)
} else {
args = append(args, f.flag)
args = append(args, value)
}
case "bool":
value := v.Field(i).Bool()
if !value {
continue
}
args = append(args, f.flag)
case "[]string":
n := v.Field(i).Len()
if n == 0 {
continue
}
for j := 0; j < n; j++ {
args = append(args, f.flag)
args = append(args, v.Field(i).Index(j).String())
}
case "string":
value := v.Field(i).String()
if len(value) == 0 {
continue
}
args = append(args, f.flag)
args = append(args, value)
case "slice": // array of structs or custom slice type
n := v.Field(i).Len()
if n == 0 {
continue
}
for j := 0; j < n; j++ {
value, ok := v.Field(i).Index(j).Interface().(fmt.Stringer)
if !ok {
continue
}
str := value.String()
if len(str) == 0 {
continue
}
args = append(args, f.flag)
args = append(args, str)
}
default:
if !v.Field(i).CanInterface() {
continue
}
value, ok := v.Field(i).Interface().(fmt.Stringer)
if !ok {
continue
}
str := value.String()
if len(str) == 0 {
continue
}
args = append(args, f.flag)
args = append(args, str)
}
// Recurisvely iterate through embedded structures
} else if v.Field(i).Kind() == reflect.Struct {
structArgs, err := ParseInterfaceArgs(v.Field(i).Interface())
if err != nil {
return []string{}, err
}
if len(structArgs) > 0 {
args = append(args, structArgs...)
}
}
}
return args, nil
}