-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
96 lines (77 loc) · 2.21 KB
/
main.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
// help is a simple example illustrating how to build a baker command with the ability
// to show an help output for common usage and component-specific instructions.
// A wrong invocation (no toml argument) or the -h/-help argument shows the main usage help.
// Using -help together with an available component, shows its help. `-help *` shows the
// help for all available components
package main
import (
"flag"
"fmt"
"os"
"strings"
"text/template"
"github.com/AdRoll/baker"
"github.com/AdRoll/baker/filter"
"github.com/AdRoll/baker/input"
"github.com/AdRoll/baker/output"
)
var flagHelpConfig = flag.String("help", "", "show help for a `component` (use '*' to dump all)")
func main() {
flag.Usage = displayProgramUsage
flag.Parse()
if *flagHelpConfig != "" {
baker.RenderHelpMarkdown(os.Stderr, *flagHelpConfig, components)
return
}
// If the toml configuration file isn't provided as argument, print usage and exit
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
// Do stuff, then...
fmt.Println("Bye!")
}
var components = baker.Components{
Inputs: input.All,
Filters: filter.All,
Outputs: output.All,
}
var programUsageTemplate = template.Must(template.New("Program usage").Parse(`
Usage: {{ .ExecName }} [options] TOPOLOGY
TOPOLOGY must be a pathname to a TOML file describing the topology to create.
Options:
{{ .Defaults }}
Available inputs:
{{ range .Components.Inputs }}
{{ .Name }}{{ end }}
Available filters:
{{ range .Components.Filters }}
{{ .Name }}{{ end }}
Available outputs:
{{ range .Components.Outputs }}
{{ .Name }}{{ end }}
Available uploads:
{{ range .Components.Uploads }}
{{ .Name }}{{ end }}
`))
func displayProgramUsage() {
// Structure program usage sections
type programUsage struct {
Build string
ExecName string
Defaults string
Components baker.Components
}
// Capture command argument defaults
var defaultsBuilder strings.Builder
flag.CommandLine.SetOutput(&defaultsBuilder)
flag.PrintDefaults()
// Inject program usage data into message template
if err := programUsageTemplate.Execute(os.Stderr, &programUsage{
ExecName: os.Args[0],
Defaults: defaultsBuilder.String(),
Components: components,
}); err != nil {
panic(err)
}
}