Skip to content

Commit 815d03a

Browse files
committed
fix: small improvements to generation and documentation
1 parent 3a974a8 commit 815d03a

File tree

5 files changed

+78
-46
lines changed

5 files changed

+78
-46
lines changed

internal/doc/GeneratorOptions.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package doc
2+
3+
import "github.com/spf13/cobra"
4+
5+
// GeneratorOptions all modifiers for the generator
6+
type GeneratorOptions struct {
7+
// Directory to write the documentation files
8+
Dir string
9+
10+
// FileNameGenerator - provides custom file name for each documentation file
11+
FileNameGenerator func(cmd *cobra.Command) string
12+
13+
// FilePrepender - Prepend content to the generated file (add header)
14+
FilePrepender func(string) string
15+
16+
// LinkHandler - function to handle links that lets you to transform them to different format
17+
LinkHandler func(string) string
18+
19+
// GenerateIndex - generate index file
20+
GenerateIndex bool
21+
22+
// IndexLocation - name and folder of the assembly file (typically ./README.adoc)
23+
IndexLocation string
24+
}

internal/doc/docs_index.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package doc
22

3-
// TODO remove
43
import (
54
"os"
65
"text/template"

internal/doc/modular_adoc_docs.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package doc
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"fmt"
2021
"io"
2122
"os"
@@ -198,12 +199,12 @@ func printOptions(buf *bytes.Buffer, cmd *cobra.Command) error {
198199
}
199200

200201
// GenAsciidoc creates asciidocs documentation
201-
func GenAsciidoc(cmd *cobra.Command, w io.Writer, options *GeneratorOptions) error {
202-
return GenAsciidocCustom(cmd, w, options)
202+
func GenAsciidoc(cmd *cobra.Command, w io.Writer) error {
203+
return GenAsciidocCustom(cmd, w, func(s string) string { return s })
203204
}
204205

205206
// GenAsciidocCustom creates custom asciidoc documentation
206-
func GenAsciidocCustom(cmd *cobra.Command, w io.Writer, options *GeneratorOptions) error {
207+
func GenAsciidocCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
207208
cmd.InitDefaultHelpCmd()
208209
cmd.InitDefaultHelpFlag()
209210

@@ -292,34 +293,17 @@ func GetShortCommandPath(c *cobra.Command) string {
292293
return strings.Join(commands[1:], " ")
293294
}
294295

295-
// GeneratorOptions options for the generator
296-
type GeneratorOptions struct {
297-
// Directory to write the file to.
298-
Dir string
299-
300-
// FileNameGenerator - provides custom file name for the file we use
301-
FileNameGenerator func(cmd *cobra.Command) string
302-
303-
// FilePrepender - Prepend content to the generated file.
304-
FilePrepender func(string) string
305-
306-
// LinkHandler - function to handle links
307-
LinkHandler func(string) string
308-
309-
// GenerateIndex - generate index file
310-
GenerateIndex bool
311-
312-
// IndexLocation - name and folder of the assembly file (typically ./README.adoc)
313-
IndexLocation string
314-
}
315-
316296
// GenAsciidocTree will generate a markdown page for this command and all
317297
// descendants in the directory given. The header may be nil.
318298
// This function may not work correctly if your command names have `-` in them.
319299
// If you have `cmd` with two subcmds, `sub` and `sub-third`,
320300
// and `sub` has a subcommand called `third`, it is undefined which
321301
// help output will be in the file `cmd-sub-third.1`.
322302
func GenAsciidocTree(cmd *cobra.Command, options *GeneratorOptions) error {
303+
if options.Dir == "" {
304+
return errors.New("dir must be specified")
305+
}
306+
323307
if options.LinkHandler == nil {
324308
options.LinkHandler = func(name string) string {
325309
return name
@@ -370,5 +354,5 @@ func GenAsciidocTreeCustom(cmd *cobra.Command, options *GeneratorOptions) error
370354
}
371355
}
372356

373-
return GenAsciidocCustom(cmd, f, options)
357+
return GenAsciidocCustom(cmd, f, options.LinkHandler)
374358
}

internal/doc/modular_adoc_docs.md

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func main() {
1717
Use: "test",
1818
Short: "my test program",
1919
}
20-
options := GeneratorOptions{
21-
Dir: "/tmp/test.adoc",
20+
options := doc.GeneratorOptions{
21+
Dir: "/tmp/",
2222
}
2323
err := doc.GenAsciidocTree(cmd, options)
2424
if err != nil {
@@ -29,9 +29,28 @@ func main() {
2929

3030
That will get you a AsciiDoc document `/tmp/test.adoc`
3131

32-
## Generating AsciiDoc TOC with support for chapters
32+
## Generation options
3333

34-
TODO
34+
35+
```golang
36+
// Directory to write the documentation files
37+
Dir string
38+
39+
// FileNameGenerator - provides custom file name for each documentation file
40+
FileNameGenerator func(cmd *cobra.Command) string
41+
42+
// FilePrepender - Prepend content to the generated file (add header)
43+
FilePrepender func(string) string
44+
45+
// LinkHandler - function to handle links that lets you to transform them to different format
46+
LinkHandler func(string) string
47+
48+
// GenerateIndex - generate index file
49+
GenerateIndex bool
50+
51+
// IndexLocation - name and folder of the assembly file (typically ./README.adoc)
52+
IndexLocation string
53+
```
3554

3655
## Generate AsciiDoc docs for the entire command tree
3756

@@ -53,7 +72,9 @@ import (
5372

5473
func main() {
5574
kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
56-
err := doc.GenAsciidocTree(kubectl, "./")
75+
err := doc.GenAsciidocTree(kubectl, doc.GeneratorOptions{
76+
Dir: "/.",
77+
})
5778
if err != nil {
5879
log.Fatal(err)
5980
}
@@ -78,21 +99,16 @@ This will write the AsciiDoc doc for ONLY "cmd" into the out, buffer.
7899

79100
## Customize the output
80101

81-
Both `GenAsciidoc` and `GenAsciidocTree` have alternate versions with callbacks to get some control of the output:
102+
Both `GenAsciidocTree` GeneratorOptions object provides number of customizations we can execute.
103+
By default only Dir folder is required to be present.
82104

83105
```go
84-
func GenAsciidocTreeCustom(cmd *Command, dir string, filePrepender, linkHandler func(string) string) error {
85-
//...
106+
options := doc.GeneratorOptions{
107+
Dir: "/.",
86108
}
87109
```
88-
89-
```go
90-
func GenAsciidocCustom(cmd *Command, out *bytes.Buffer, linkHandler func(string) string) error {
91-
//...
92-
}
93-
```
94-
95-
The `filePrepender` will prepend the return value given the full filepath to the rendered Asciidoc file. A common use case is to add front matter to use the generated documentation with [Hugo](http://gohugo.io/):
110+
111+
The `FilePrepender` field will prepend the return value given the full filepath to the rendered Asciidoc file. A common use case is to add front matter to use the generated documentation with [Hugo](http://gohugo.io/):
96112

97113
```go
98114
const fmTemplate = `---
@@ -103,7 +119,7 @@ url: %s
103119
---
104120
`
105121

106-
filePrepender := func(filename string) string {
122+
options.FilePrepender := func(filename string) string {
107123
now := time.Now().Format(time.RFC3339)
108124
name := filepath.Base(filename)
109125
base := strings.TrimSuffix(name, path.Ext(name))
@@ -112,11 +128,20 @@ filePrepender := func(filename string) string {
112128
}
113129
```
114130

115-
The `linkHandler` can be used to customize the rendered internal links to the commands, given a filename:
131+
The `LinkHandler` can be used to customize the rendered internal links to the commands, given a filename:
116132

117133
```go
118-
linkHandler := func(name string) string {
134+
options.LinkHandler := func(name string) string {
119135
base := strings.TrimSuffix(name, path.Ext(name))
120136
return "/commands/" + strings.ToLower(base) + "/"
121137
}
122138
```
139+
140+
The `FileNameGenerator` can be used to customize file names:
141+
142+
```go
143+
options.FileNameGenerator = func(c *cobra.Command) string {
144+
basename := c.CommandPath() + ".adoc"
145+
return filepath.Join(options.Dir, basename)
146+
}
147+
```

internal/doc/modular_adoc_docs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestGenMdTree(t *testing.T) {
8888
options := GeneratorOptions{
8989
Dir: tmpdir,
9090
}
91-
if err := GenAsciidocTree(c, options); err != nil {
91+
if err := GenAsciidocTree(c, &options); err != nil {
9292
t.Fatalf("GenAsciidocTree failed: %v", err)
9393
}
9494

0 commit comments

Comments
 (0)