-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_test.go
117 lines (96 loc) · 2.29 KB
/
main_test.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
package main
import (
"encoding/json"
"os"
"testing"
flags "github.com/jessevdk/go-flags"
"github.com/mitchellh/cli"
)
//Documented can be implemented by command we want to have documented
type Documented interface {
Usage() string
Description() string
Synopsis() string
Options() *flags.Parser
}
func TestDocGeneration(t *testing.T) {
cli := create()
type opt struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name,omitempty"`
Description string `json:"description"`
DefaultValue []string `json:"default_value"`
Choices []string `json:"choices"`
}
type entry struct {
Usage string `json:"usage"`
Synopsis string `json:"synopsis"`
Description string `json:"description"`
Options map[string][]opt `json:"options"`
}
type docs struct {
Commands map[string]*entry `json:"commands"`
}
d := docs{
Commands: map[string]*entry{},
}
for name, cmdFn := range cli.Commands {
if !isNotSysCmd(cli, name) {
continue
}
cmd, err := cmdFn()
if err != nil {
t.Fatalf("failed to create command for documentation purposes: %v", err)
}
var (
ok bool
doc Documented
)
if doc, ok = cmd.(Documented); !ok {
t.Logf("command '%s' doesn't implement documented interface, skipping", name)
continue
}
opts := map[string][]opt{}
d.Commands[name] = &entry{
Usage: doc.Usage(),
Synopsis: doc.Synopsis(),
Description: doc.Description(),
Options: opts,
}
for _, g := range doc.Options().Groups() {
gopts := []opt{}
for _, o := range g.Options() {
op := opt{
Description: o.Description,
LongName: o.LongName,
DefaultValue: o.Default,
Choices: o.Choices,
}
if o.ShortName != rune(0) {
op.ShortName = string(o.ShortName)
}
gopts = append(gopts, op)
}
opts[g.LongDescription] = gopts
}
}
f, err := os.Create("spec.json")
if err != nil {
t.Fatalf("failed to write docs: %v", err)
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", "\t")
err = enc.Encode(d)
if err != nil {
t.Fatalf("failed to encode: %+v", err)
}
}
func isNotSysCmd(cli *cli.CLI, name string) bool {
for _, cmd := range cli.HiddenCommands {
if name == cmd {
return false
}
}
return true
}