/
usage.go
226 lines (182 loc) · 4.98 KB
/
usage.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
package streamutil
import (
"bytes"
"io"
"os"
"strings"
"text/tabwriter"
"text/template"
"github.com/blendle/go-streamprocessor/streamconfig"
"github.com/kelseyhightower/envconfig"
)
// About provides details about the processor and its configuration.
type About struct {
Name,
URI,
Usage string
Consumers,
Producers []string
Config map[string]string
}
type name struct {
Upper, Lower, Usage, Line string
}
type usageData struct {
Name,
NameLine,
Usage,
URI string
Consumers,
Producers []name
Config map[string][]string
Count int
}
// TODO:
// - split up into multiple templates
// - colorize headers
// - colorize environment variables
// - properly split lines at 80 characters
// - indent anything other than headers by 2 spaces
// - underline documentation link
// - add configurable "EXAMPLE" header at the top
// - improve consumer/producer environment variable output
// - show default values
// - show descriptions
// - show required true/false
// - use same format as "PROCESSOR CONFIGURATION"
const usage = `
{{ .Name }}
{{ .NameLine }}
{{ .Usage }}
{{ if .URI }}
🐚 ONLINE DOCUMENTATION
-----------------------
For more details about this processor, see the following URL:
{{ .URI }}
{{ end }}
{{ if .Count }}
🐝 USAGE
--------
This stream processor is configured via environment variables. The list of
available variables is split between these {{ .Count }} sections:
{{ if .Config -}}
* PROCESSOR CONFIGURATION – how to configure the business logic of this
processor via environment variables.
{{ end }}
{{- range .Consumers }}
* {{ .Upper }} – how to configure the {{if eq .Lower "consumer"}}default {{end}}{{.Lower}}
{{ end }}
{{- range .Producers }}
* {{ .Upper }} – how to configure the {{if eq .Lower "producer"}}default {{end}}{{.Lower}}
{{ end }}
{{ if .Config -}}
⚙️ PROCESSOR CONFIGURATION
--------------------------
{{ range $k, $v := .Config }}
{{ $k }}
{{- range $v }}
{{ . }}
{{- end }}
{{ end }}
{{ end }}
{{- range .Consumers }}
⚙️ {{ .Upper }}
---{{ .Line }}
In order to use this consumer, you need to set the environment variable
"STREAMCLIENT_CONSUMER" to one of the following values:
* standardstream
* inmem
* kafka
* pubsub
Alternatively, if you pipe any input to this consumer over stdin, and don't set
the "STREAMCLIENT_CONSUMER" environment variable, the consumer mode will be set
to "standardstream".
In any other situation, the consumer will terminate.
{{ .Usage }}
{{ end }}
{{- range .Producers }}
⚙️ {{ .Upper }}
---{{ .Line }}
In order to use this producer, you need to set the environment variable
"STREAMCLIENT_PRODUCER" to one of the following values:
* standardstream
* inmem
* kafka
* pubsub
Alternatively, if you set the environment variable "DRY_RUN" to any value, and
don't set the "STREAMCLIENT_PRODUCER" environment variable, the producer mode
will be set to "standardstream".
In any other situation, the producer will terminate.
{{ .Usage }}
{{ end }}
{{ end }}
`
// Usage listens for `--help` and prints the relevant details on how to use the
// processor. A boolean is returned indicating if the usage documentation is
// printed, at which point the processor should probably exit.
func Usage(out io.Writer, about About) bool {
if len(os.Args) < 2 {
return false
}
if os.Args[1] != "--help" && os.Args[1] != "-h" {
return false
}
count := len(about.Consumers) + len(about.Producers)
if len(about.Config) > 0 {
count++
}
var consumers []name
for _, c := range about.Consumers {
b := &bytes.Buffer{}
tabs := tabwriter.NewWriter(b, 1, 0, 4, ' ', 0)
conf := &streamconfig.Consumer{}
_ = envconfig.Usagef(c, conf, tabs, envconfig.DefaultTableFormat) // nolint
_ = tabs.Flush() // nolint
cc := strings.Replace(c, "_", " ", -1)
consumers = append(consumers, name{
Lower: cc,
Upper: strings.ToUpper(cc),
Usage: b.String(),
Line: strings.Repeat("-", len(cc)),
})
}
var producers []name
for _, p := range about.Producers {
b := &bytes.Buffer{}
tabs := tabwriter.NewWriter(b, 1, 0, 4, ' ', 0)
conf := &streamconfig.Producer{}
_ = envconfig.Usagef(p, conf, tabs, envconfig.DefaultTableFormat) // nolint
_ = tabs.Flush() // nolint
pp := strings.Replace(p, "_", " ", -1)
producers = append(producers, name{
Lower: pp,
Upper: strings.ToUpper(pp),
Usage: b.String(),
Line: strings.Repeat("-", len(pp)),
})
}
config := map[string][]string{}
for k, v := range about.Config {
config[k] = strings.Split(v, "\n")
}
data := &usageData{
Name: about.Name,
NameLine: strings.Repeat("=", len(about.Name)),
Usage: about.Usage,
URI: about.URI,
Consumers: consumers,
Producers: producers,
Config: config,
Count: count,
}
t := template.Must(template.New("usage").Parse(usage))
buf := &bytes.Buffer{}
if err := t.Execute(buf, data); err != nil {
panic(err)
}
_, err := out.Write(buf.Bytes())
if err != nil {
panic(err)
}
return true
}