-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
venom.go
166 lines (142 loc) · 4.57 KB
/
venom.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
package venom
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"strings"
)
type writerFn func() writer
var writers = make(map[Formats]writerFn)
type docCommandOptions struct {
outDir string
formats []string
showHidden bool
}
// Initialize a new documentation command with cmd as the parent, providing options for customization
// The provided command will always provide documentation from the *root*, which allows the caller to list this automated
// command under other administrative/tooling/hidden commands as needed.
func Initialize(cmd *cobra.Command, options *Options) error {
if options == nil {
options = NewOptions()
}
if err := options.validate(); err != nil {
return err
}
formats := make([]string, 0)
for _, format := range options.formats.defined() {
switch format {
case Markdown:
formats = append(formats, "markdown")
case Yaml:
formats = append(formats, "yaml")
case Json:
formats = append(formats, "json")
case ReST:
formats = append(formats, "rest")
}
}
o := docCommandOptions{
outDir: options.outDir,
showHidden: options.showHiddenCommands,
formats: formats,
}
docCommand := &cobra.Command{
Use: options.commandName,
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
opts := *options
root := cmd.Root()
root.InitDefaultHelpCmd()
root.InitDefaultHelpFlag()
opts.showHiddenCommands = o.showHidden
opts.outDir = o.outDir
definedFormats := getUserSelectedFormats(o, opts)
if !definedFormats.IsValid() {
return errors.New("invalid formats selected")
}
opts.formats = definedFormats
documentation := NewDocumentation(root, &opts)
if err := Write(documentation); err != nil {
return err
}
return nil
},
}
if !options.disableUserCommandOptions {
docCommand.Flags().StringVar(&o.outDir, "out-dir", o.outDir, "The target output directory")
docCommand.Flags().BoolVar(&o.showHidden, "show-hidden", o.showHidden, "Also show hidden commands")
docCommand.Flags().StringSliceVar(&o.formats, "formats", o.formats,
fmt.Sprintf("A comma-separated list of formats to output. Allowed: [%s]", strings.Join(formats, ",")))
}
cmd.AddCommand(docCommand)
return nil
}
func getUserSelectedFormats(o docCommandOptions, opts Options) Formats {
definedFormats := Formats(0)
for _, format := range o.formats {
switch strings.ToLower(format) {
case "markdown", "md":
if opts.formats.IsSet(Markdown) {
definedFormats.Set(Markdown)
} else {
opts.templateOptions.Logger.Printf("Skipping markdown documentation because the application maintainers have not enabled this output format.")
}
case "yaml", "yml":
if opts.formats.IsSet(Yaml) {
definedFormats.Set(Yaml)
} else {
opts.templateOptions.Logger.Printf("Skipping yaml documentation because the application maintainers have not enabled this output format.")
}
case "json":
if opts.formats.IsSet(Json) {
definedFormats.Set(Json)
} else {
opts.templateOptions.Logger.Printf("Skipping json documentation because the application maintainers have not enabled this output format.")
}
case "rest", "rst":
if opts.formats.IsSet(ReST) {
definedFormats.Set(ReST)
} else {
opts.templateOptions.Logger.Printf("Skipping rest documentation because the application maintainers have not enabled this output format.")
}
default:
opts.templateOptions.Logger.Printf("Skipping %s documentation because it is not currently supported.", format)
}
}
return definedFormats
}
// registerWriter for a given format to allow writing via the writer function
func registerWriter(format Formats, writer writerFn) {
writers[format] = writer
}
// Write to outDir the documentation for all given formats
func Write(documentation Documentation) error {
var err error
options := documentation.options
formats := options.formats
outDir := options.outDir
if !formats.IsValid() {
return errors.New("unexpected formats provided to Write")
}
// ensure proper initialization
documentation.init()
templateOptions := options.TemplateOptions()
for _, format := range []Formats{Yaml, Json, Markdown, Man, ReST} {
if formats.IsSet(format) {
templateOptions.Logger.Printf("Generating documentation for %s", strings.ToLower(format.String()))
if writeBuilder, ok := writers[format]; ok {
w := writeBuilder()
if wt, ok := w.(wantsTemplateOptions); ok {
wt.SetTemplateOptions(templateOptions)
}
err = w.Write(outDir, documentation)
if err != nil {
return err
}
} else {
return fmt.Errorf("missing output writer for format %v", format)
}
}
}
return nil
}