-
Notifications
You must be signed in to change notification settings - Fork 33
/
show.go
293 lines (260 loc) · 6.37 KB
/
show.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"strings"
"sync"
"text/template"
"github.com/nokia/ntt/internal/env"
"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/internal/log"
"github.com/nokia/ntt/internal/yaml"
"github.com/nokia/ntt/project"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/syntax"
"github.com/spf13/cobra"
)
var (
ShowCommand = &cobra.Command{
Use: "show [ <file>...] [-- var...]",
Short: "Show test suite configuration.",
RunE: show,
}
)
func show(cmd *cobra.Command, args []string) error {
_, keys := splitArgs(args, cmd.ArgsLenAtDash())
r := ConfigReport{Config: Project}
r.Environ = Project.Variables.Slice()
r.Files, r.err = project.Files(Project)
switch {
case outputJSON:
return printJSON(&r, keys)
case ShSetup:
return printShellScript(&r, keys)
case len(keys) != 0:
return printValues(Project, keys)
default:
keys := []string{
"name",
"root",
"source_dir",
"sources",
"imports",
"parameters_file",
"hooks_file",
"lint_file",
}
return printKeyValues(Project, keys)
}
}
func printJSON(report *ConfigReport, keys []string) error {
if len(keys) != 0 {
return fmt.Errorf("command line option --json does not accept additional command line arguments")
}
var presets []string
if s := env.Getenv("NTT_PRESETS"); s != "" {
presets = strings.Split(s, string(os.PathListSeparator))
gc, err := report.GlobalConfig(presets...)
if err != nil {
return err
}
report.Config.Parameters.TestConfig = gc
}
files, err := fs.TTCN3Files(report.Config.Sources...)
if err != nil {
return err
}
if !dumb {
mu := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(len(files))
glist := make([]project.TestConfig, 0, len(files))
for _, file := range files {
file := file
go func() {
defer wg.Done()
tree := ttcn3.ParseFile(file)
if tree.Err != nil {
report.err = errors.Join(report.err, tree.Err)
}
list := make([]project.TestConfig, 0, len(tree.Names))
tree.Inspect(func(n syntax.Node) bool {
switch n := n.(type) {
case *syntax.FuncDecl:
if n.IsTest() || n.IsControl() {
break
}
return false
case *syntax.ControlPart:
break
default:
return true
}
name := tree.QualifiedName(n)
tc, err := report.TestConfigs(name, presets...)
if err != nil {
log.Debugf("implementation error: %s\n", err)
}
if len(tc) > 0 {
list = append(list, tc...)
}
return false
})
mu.Lock()
glist = append(glist, list...)
mu.Unlock()
}()
}
wg.Wait()
report.Execute = glist
}
b, err := yaml.MarshalJSON(report)
if err != nil {
return fmt.Errorf("failed to marshal report: %w", err)
}
fmt.Println(string(b))
return report.err
}
func printShellScript(report *ConfigReport, keys []string) error {
const shellTemplate = `# This is a generated output of ntt show. Args: {{ .Args }}
# k3-hook calls the K3 test hook (if defined) with action passed by $1.
function k3-hook()
{
if [ -n "$K3_HOOKS_FILE" ]; then
K3_SOURCES="${K3_SOURCES[*]}" \
K3_IMPORTS="${K3_IMPORTS[*]}" \
K3_TTCN3_FILES="${K3_TTCN3_FILES[*]}" \
"$K3_HOOKS_FILE" "$@" 1>&2
fi
}
{{ if .Name -}} export K3_NAME='{{ .Name }}' {{- end }}
{{ if .HooksFile -}} export K3_HOOKS_FILE='{{ .HooksFile }}' {{- end }}
{{ if .Root -}} export K3_SOURCE_DIR='{{ .Root }}' {{- end }}
{{ range .Environ }}export '{{.}}'
{{end}}
K3_SOURCES=(
{{ range .Sources }} {{.}}
{{end}})
K3_IMPORTS=(
{{ range .Imports }} {{.}}
{{end}})
K3_TTCN3_FILES=(
{{ range .Files }} {{.}}
{{end}})
K3_BUILTINS=(
{{ range .K3.Includes }} {{.}}
{{end}})
{{ if .Err }}
# ERROR
#
# Output might not be complete, because some errors have occurred during
# execution. We return "false", to give you the chance to detect this
# situation
read -r -d '' K3_ERROR <<'EOF'
{{.Err}}
EOF
false
{{ end }}
`
if len(keys) != 0 {
return fmt.Errorf("command line option --sh does not accept additional command line arguments")
}
t := template.Must(template.New("k3-sh-setup").Parse(shellTemplate))
if err := t.Execute(os.Stdout, report); err != nil {
fmt.Printf(`
# ERROR: Internal template did not compile: %s
#
# Output might not be complete, because some errors have occurred during
# execution. We return "false", to give you the chance to detect this
# situation
false
`, err.Error())
return err
}
if err := report.Err(); err != "" {
return fmt.Errorf("%s", err)
}
return nil
}
func get(c *project.Config, key string) ([]string, error) {
b, err := yaml.MarshalJSON(c)
if err != nil {
return nil, err
}
conf := make(map[string]interface{})
if err := json.Unmarshal(b, &conf); err != nil {
return nil, err
}
for _, k := range strings.Split(key, ".") {
v, ok := conf[k]
if !ok {
return nil, fmt.Errorf("key %q not found", k)
}
switch v := v.(type) {
case []string:
return v, nil
case map[string]string:
s := make([]string, 0, len(v))
for key, val := range v {
s = append(s, fmt.Sprintf("'%s=%s'", key, val))
}
sort.Strings(s)
return s, nil
case map[string]interface{}:
conf = v
default:
return []string{fmt.Sprintf("%v", v)}, nil
}
}
return nil, fmt.Errorf("value of key %q is not of type string or list of strings", key)
}
func printValues(c *project.Config, keys []string) error {
for _, key := range keys {
s, err := get(c, key)
if err != nil {
return err
}
for _, v := range s {
fmt.Println(v)
}
}
return nil
}
func printKeyValues(c *project.Config, keys []string) error {
for _, key := range keys {
s, err := get(c, key)
if err != nil {
return err
}
if len(s) > 0 {
fmt.Printf("NTT_%s=\"%s\"\n", strings.ToUpper(key), strings.Join(s, " "))
}
}
return nil
}
// splitArgs splits an argument list at pos. Pos is usually the position of '--'
// (see cobra.Command.ArgsLenAtDash).
//
// Is pos < 0, the second list will be empty
func splitArgs(args []string, pos int) ([]string, []string) {
if pos < 0 {
return args, []string{}
}
return args[:pos], args[pos:]
}
type ConfigReport struct {
Args []string `json:"args"`
*project.Config `json:",inline"`
Environ []string `json:"env"`
Files []string `json:"files"`
err error
}
func (r *ConfigReport) Err() string {
if r.err != nil {
return r.err.Error()
}
return ""
}