-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcmd_examples.go
110 lines (90 loc) · 2 KB
/
cmd_examples.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
// Examples
//
// Show information about our protocols.
package main
import (
"context"
"flag"
"fmt"
"regexp"
"sort"
"github.com/google/subcommands"
"github.com/skx/overseer/protocols"
)
type examplesCmd struct {
}
//
// Glue
//
func (*examplesCmd) Name() string { return "examples" }
func (*examplesCmd) Synopsis() string { return "Show example protocol-tests." }
func (*examplesCmd) Usage() string {
return `examples :
Provide sample usage of each of our protocol-tests.
`
}
//
// Flag setup.
//
func (p *examplesCmd) SetFlags(f *flag.FlagSet) {
}
//
// Show example output for any protocol-handler matching the
// pattern specified.
//
// If the filter is empty then show all.
//
func showExamples(filter string) {
re := regexp.MustCompile(filter)
// For each (sorted) protocol-handler
handlers := protocols.Handlers()
sort.Strings(handlers)
// Get the name
for _, name := range handlers {
// Skip unless this handler matches the filter.
match := re.FindAllStringSubmatch(name, -1)
if len(match) < 1 {
continue
}
// Create an instance of it
x := protocols.ProtocolHandler(name)
// Show the output of that function
out := x.Example()
fmt.Printf("%s\n", out)
fmt.Printf("Arguments which are supported are now shown:\n\n")
fmt.Printf(" %10s|%s\n", "Name", "Valid Value")
fmt.Printf(" ----------------------------------\n")
//
// The arguments this test supports
//
m := x.Arguments()
//
// Temporary structure to store the keys.
//
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
//
// Now show the keys + values in sorted order
//
for _, k := range keys {
fmt.Printf(" %10s|%s\n", k, m[k])
}
fmt.Printf("\n\n")
}
}
//
// Entry-point.
//
func (p *examplesCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if len(f.Args()) > 0 {
for _, name := range f.Args() {
showExamples(name)
}
} else {
showExamples(".*")
}
return subcommands.ExitSuccess
}