forked from kumahq/kuma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
119 lines (104 loc) 路 2.79 KB
/
generate.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
package main
import (
"fmt"
"log"
"os"
"path"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
kuma_cp "github.com/kumahq/kuma/app/kuma-cp/cmd"
kuma_dp "github.com/kumahq/kuma/app/kuma-dp/cmd"
kuma_prometheus_sd "github.com/kumahq/kuma/app/kuma-prometheus-sd/cmd"
kumactl "github.com/kumahq/kuma/app/kumactl/cmd"
"github.com/kumahq/kuma/pkg/version"
)
// GetenvOr returns the value of the environment variable named by env,
// or the value of alternate if the environment variable is not present.
//
// TODO(jpeach) move this to pkg/utils
func GetenvOr(env string, alternate string) string {
if value, ok := os.LookupEnv(env); ok {
return value
}
return alternate
}
func must(err error) {
if err != nil {
log.Fatal(err)
}
}
func disableAutogen(cmd *cobra.Command) *cobra.Command {
// Not that we don't want to advertise cobra, but this is the only way to
// suppress the timestamp.
cmd.DisableAutoGenTag = true
for _, c := range cmd.Commands() {
disableAutogen(c)
}
return cmd
}
func markdown(path string, cmd *cobra.Command) {
must(os.MkdirAll(path, 0755))
must(doc.GenMarkdownTree(cmd, path))
}
func man(path string, header *doc.GenManHeader, cmd *cobra.Command) {
must(os.MkdirAll(path, 0755))
must(doc.GenManTree(cmd, header, path))
}
type command struct {
command *cobra.Command
header *doc.GenManHeader
}
func main() {
prefix := GetenvOr("DESTDIR", ".")
format := GetenvOr("FORMAT", "markdown")
apps := map[string]command{
path.Join(prefix, "kuma-cp"): command{
command: kuma_cp.DefaultRootCmd(),
header: &doc.GenManHeader{
Title: "KUMA-CP",
Section: "8",
Source: fmt.Sprintf("%s %s", version.Product, version.Build.Version),
Manual: version.Product,
},
},
path.Join(prefix, "kuma-dp"): command{
command: kuma_dp.DefaultRootCmd(),
header: &doc.GenManHeader{
Title: "KUMA-DP",
Section: "8",
Source: fmt.Sprintf("%s %s", version.Product, version.Build.Version),
Manual: version.Product,
},
},
path.Join(prefix, "kuma-prometheus-sd"): command{
command: kuma_prometheus_sd.DefaultRootCmd(),
header: &doc.GenManHeader{
Title: "KUMA-PROMETHEUS-SD",
Section: "8",
Source: fmt.Sprintf("%s %s", version.Product, version.Build.Version),
Manual: version.Product,
},
},
path.Join(prefix, "kumactl"): command{
command: kumactl.DefaultRootCmd(),
header: &doc.GenManHeader{
Title: "KUMACTL",
Section: "1",
Source: fmt.Sprintf("%s %s", version.Product, version.Build.Version),
Manual: version.Product,
},
},
}
switch format {
case "markdown":
for p, c := range apps {
markdown(p, disableAutogen(c.command))
}
case "man":
for p, c := range apps {
man(p, c.header, disableAutogen(c.command))
}
default:
log.Fatalf("unsupported reference format %q", format)
}
}