-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (118 loc) Β· 3.14 KB
/
main.go
File metadata and controls
131 lines (118 loc) Β· 3.14 KB
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
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"stash.appscode.dev/stash/pkg/cmds"
"github.com/spf13/cobra/doc"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gomodules.xyz/runtime"
"k8s.io/klog/v2"
)
var (
tplFrontMatter = template.Must(template.New("index").Parse(`---
title: Stash Operator
description: Stash Operator Reference
menu:
docs_{{ "{{ .version }}" }}:
identifier: operator
name: Stash Operator
parent: reference
weight: 20
menu_name: docs_{{ "{{ .version }}" }}
---
`))
_ = template.Must(tplFrontMatter.New("cmd").Parse(`---
title: {{ .Name }}
menu:
docs_{{ "{{ .version }}" }}:
identifier: {{ .ID }}
name: {{ .Name }}
parent: operator
{{- if .RootCmd }}
weight: 0
{{ end }}
product_name: stash
section_menu_id: reference
menu_name: docs_{{ "{{ .version }}" }}
{{- if .RootCmd }}
url: /docs/{{ "{{ .version }}" }}/reference/operator/
aliases:
- /docs/{{ "{{ .version }}" }}/reference/operator/operator/
{{ end }}
---
`))
)
// ref: https://github.com/spf13/cobra/blob/master/doc/md_docs.md
func main() {
rootCmd := cmds.NewRootCmd()
dir := runtime.GOPath() + "/src/stash.appscode.dev/docs/docs/reference/operator"
fmt.Printf("Generating cli markdown tree in: %v\n", dir)
err := os.RemoveAll(dir)
if err != nil {
klog.Fatalln(err)
}
err = os.MkdirAll(dir, 0o755)
if err != nil {
klog.Fatalln(err)
}
filePrepender := func(filename string) string {
filename = filepath.Base(filename)
base := strings.TrimSuffix(filename, path.Ext(filename))
name := cases.Title(language.English).String(strings.ReplaceAll(base, "_", " "))
parts := strings.Split(name, " ")
if len(parts) > 1 {
name = strings.Join(parts[1:], " ")
}
data := struct {
ID string
Name string
RootCmd bool
}{
strings.ReplaceAll(base, "_", "-"),
name,
!strings.ContainsRune(base, '_'),
}
var buf bytes.Buffer
if err := tplFrontMatter.ExecuteTemplate(&buf, "cmd", data); err != nil {
klog.Fatalln(err)
}
return buf.String()
}
linkHandler := func(name string) string {
return "/docs/reference/operator/" + name
}
err = doc.GenMarkdownTreeCustom(rootCmd, dir, filePrepender, linkHandler)
if err != nil {
klog.Fatalln(err)
}
index := filepath.Join(dir, "_index.md")
f, err := os.OpenFile(index, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
klog.Fatalln(err)
}
err = tplFrontMatter.ExecuteTemplate(f, "index", struct{}{})
if err != nil {
klog.Fatalln(err)
}
if err := f.Close(); err != nil {
klog.Fatalln(err)
}
}