-
Notifications
You must be signed in to change notification settings - Fork 24
/
generate-docs.go
141 lines (123 loc) · 3.53 KB
/
generate-docs.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/fluxninja/aperture/v2/cmd/aperturectl/cmd"
"github.com/integralist/go-findroot/find"
"github.com/spf13/cobra/doc"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// GenerateDocs generated the reference docs for the CLI.
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
root, err := find.Repo()
if err != nil {
log.Fatal(err)
}
docsDir := filepath.Join(root.Path, "docs/content/reference/aperturectl")
// remove all subdirectories within the docsDir except docsDir itself
err = filepath.Walk(docsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() && path != docsDir {
err = os.RemoveAll(path)
if err != nil {
return nil
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
err = doc.GenMarkdownTreeCustom(cmd.RootCmd, docsDir, filePrepender, linkHandler)
if err != nil {
log.Fatal(err)
}
// walk the generated docs and move them to subdirectories based on their filename where _ separates the subdirectory
err = filepath.Walk(docsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".md") {
// inside this file, replace instances of "sub-command" with "sub command"
content, err := os.ReadFile(path)
if err != nil {
return err
}
content = []byte(strings.ReplaceAll(string(content), "sub-command", "sub command"))
content = []byte(strings.ReplaceAll(string(content), "via your OS's", "using your operating system's"))
// write the file back
err = os.WriteFile(path, content, 0o600)
if err != nil {
return err
}
subdir := transform(path)
subdir = filepath.Join(docsDir, subdir)
// create the subdirectory and move the file to it.
err = os.MkdirAll(filepath.Dir(subdir), 0o755)
if err != nil {
return err
}
err = os.Rename(path, subdir)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
func transform(path string) string {
filename := filepath.Base(path)
ext := filepath.Ext(filename)
// remove aperturectl prefix and replace _ with /
link := strings.ReplaceAll(strings.TrimPrefix(filename, "aperturectl"), "_", "/")
// remove the extension
link = strings.TrimSuffix(link, ext)
// tokenize filename by _ and pick up the last token
tokens := strings.Split(filename, "_")
filename = tokens[len(tokens)-1]
link = filepath.Join(link, filename)
return link
}
func filePrepender(filename string) string {
name := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filepath.Base(filename)))
// sidebar label is the last token in name
tokens := strings.Split(name, "_")
sidebarLabel := tokens[len(tokens)-1]
sidebarLabel = cases.Title(language.English).String(sidebarLabel)
template := `---
sidebar_label: %s
hide_title: true
keywords:
- aperturectl
- %s
---
<!-- markdownlint-disable -->
`
log.Println(filename)
// filename ends with aperturectl.md then add a note about installation
if strings.HasSuffix(filename, "aperturectl.md") {
template += `:::info
For installation instructions, see [installing aperturectl](/get-started/installation/aperture-cli/aperture-cli.md).
:::
:::info
See also [aperturectl configuration file format reference](/reference/configuration/aperturectl.md).
:::`
}
return fmt.Sprintf(template, sidebarLabel, name)
}
func linkHandler(name string) string {
return filepath.Join("/reference/aperturectl", transform(name))
}