forked from kool-dev/kool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs.go
108 lines (76 loc) · 2.02 KB
/
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
//go:build ignore
// +build ignore
package main
import (
"bytes"
"fmt"
"kool-dev/kool/commands"
"kool-dev/kool/core/shell"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/cobra/doc"
)
func main() {
var (
err error
koolOutput *bytes.Buffer
cmdFile *os.File
koolFile *os.File
)
linkHandler := func(filename string) string {
base := strings.TrimSuffix(filename, filepath.Ext(filename))
return strings.ToLower(base)
}
fmt.Println("Going to generate cobra docs in markdown...")
koolOutput = new(bytes.Buffer)
err = doc.GenMarkdownCustom(commands.RootCmd(), koolOutput, linkHandler)
if err != nil {
log.Fatal(err)
}
koolMarkdown := koolOutput.String()
for _, childCmd := range commands.RootCmd().Commands() {
var cmdName string
if cmdName = strings.Replace(childCmd.CommandPath(), " ", "_", -1); cmdName == "kool_deploy" || cmdName == "kool_help" {
continue
}
newName := strings.Replace(childCmd.CommandPath(), " ", "-", -1)
koolMarkdown = strings.Replace(koolMarkdown, cmdName, newName, -1)
cmdOutput := new(bytes.Buffer)
err = doc.GenMarkdownCustom(childCmd, cmdOutput, linkHandler)
if err != nil {
log.Fatal(err)
}
cmdFile, err = CreateFile(newName, "docs/4-Commands")
if err != nil {
log.Fatal(err)
}
defer cmdFile.Close()
_, err = cmdOutput.WriteTo(cmdFile)
if err != nil {
log.Fatal(err)
}
}
re := regexp.MustCompile("(?m)[\r\n]+^.*kool_deploy.*$")
koolMarkdown = re.ReplaceAllString(koolMarkdown, "")
koolFile, err = CreateFile("0-kool", "docs/4-Commands")
if err != nil {
log.Fatal(err)
}
defer koolFile.Close()
koolOutput = new(bytes.Buffer)
koolOutput.WriteString(koolMarkdown)
_, err = koolOutput.WriteTo(koolFile)
if err != nil {
log.Fatal(err)
}
shell.Success("Success!")
}
// CreateFile Create file to write markdown content
func CreateFile(filename string, dir string) (file *os.File, err error) {
basename := fmt.Sprintf("%s.md", filename)
file, err = os.Create(filepath.Join(dir, basename))
return
}