-
Notifications
You must be signed in to change notification settings - Fork 73
/
docs-operation.go
119 lines (94 loc) · 2.93 KB
/
docs-operation.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 operations
import (
"github.com/deviceinsight/kafkactl/output"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
type DocsFlags struct {
Directory string
DocType string
SinglePage bool
}
type DocsOperation struct {
}
func (operation *DocsOperation) GenerateDocs(rootCmd *cobra.Command, flags DocsFlags) error {
if _, err := os.Stat(flags.Directory); os.IsNotExist(err) {
if err := os.Mkdir(flags.Directory, os.ModePerm); err != nil {
return errors.Wrap(err, "unable to create directory")
}
}
switch flags.DocType {
case "markdown", "mdown", "md":
if err := doc.GenMarkdownTree(rootCmd, flags.Directory); err != nil {
return errors.Wrap(err, "unable to generate markdown")
}
if flags.SinglePage {
if err := generateSinglePage(flags); err != nil {
return err
}
}
case "man":
manHdr := &doc.GenManHeader{Title: "KAFKACTL", Section: "1"}
if err := doc.GenManTree(rootCmd, manHdr, flags.Directory); err != nil {
return errors.Wrap(err, "unable to generate markdown")
}
default:
return errors.Errorf("unknown doc type %q. Try 'markdown' or 'man'", flags.DocType)
}
return nil
}
func generateSinglePage(flags DocsFlags) error {
files, err := ioutil.ReadDir(flags.Directory)
if err != nil {
return errors.Wrap(err, "unable to read files in directory")
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() < files[j].Name()
})
singlePageMd := "kafkactl_docs.md"
// Open a new file for writing only
file, err := os.OpenFile(filepath.Join(flags.Directory, singlePageMd), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return errors.Wrap(err, "unable to open file")
}
defer file.Close()
for _, f := range files {
if f.Name() == singlePageMd || !strings.HasSuffix(f.Name(), ".md") {
continue
}
filename := filepath.Join(flags.Directory, f.Name())
bytes, err := ioutil.ReadFile(filename)
content := string(bytes)
content = adjustChapters(f.Name(), content)
content = removeFooter(content)
if err != nil {
return errors.Wrap(err, "unable to open file")
}
if _, err = file.WriteString(content); err != nil {
return errors.Wrap(err, "unable to write bytes")
}
if err := os.Remove(filename); err != nil {
return errors.Wrap(err, "unable to remove file")
}
}
output.Infof("File written: %s", filepath.Join(flags.Directory, singlePageMd))
return nil
}
func adjustChapters(filename string, content string) string {
separatorRegex := regexp.MustCompile("_")
matches := separatorRegex.FindAllStringIndex(filename, -1)
startLayer := len(matches)
chapterRegex := regexp.MustCompile("(?m)^(#+.*)$")
return chapterRegex.ReplaceAllString(content, strings.Repeat("#", startLayer)+"$1")
}
func removeFooter(content string) string {
footerRegex := regexp.MustCompile("(?m)^#+ Auto generated.*$")
return footerRegex.ReplaceAllLiteralString(content, "")
}