@@ -2,30 +2,33 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"io/fs"
6
7
"os"
7
8
"path/filepath"
9
+ "strings"
8
10
9
11
"github.com/cpuguy83/go-md2man/v2/md2man"
10
12
"github.com/sirupsen/logrus"
11
13
"github.com/spf13/cobra"
12
14
"github.com/spf13/cobra/doc"
13
15
)
14
16
15
- func newGenManCommand () * cobra.Command {
17
+ func newGenDocCommand () * cobra.Command {
16
18
genmanCommand := & cobra.Command {
17
- Use : "generate-man DIR" ,
18
- Short : "Generate manual pages" ,
19
+ Use : "generate-doc DIR" ,
20
+ Short : "Generate cli-reference pages" ,
19
21
Args : WrapArgsError (cobra .MinimumNArgs (1 )),
20
- RunE : genmanAction ,
22
+ RunE : gendocAction ,
21
23
Hidden : true ,
22
24
}
25
+ genmanCommand .Flags ().String ("type" , "man" , "Output type (man, docsy)" )
23
26
genmanCommand .Flags ().String ("output" , "" , "Output directory" )
24
27
genmanCommand .Flags ().String ("prefix" , "" , "Install prefix" )
25
28
return genmanCommand
26
29
}
27
30
28
- func genmanAction (cmd * cobra.Command , args []string ) error {
31
+ func gendocAction (cmd * cobra.Command , args []string ) error {
29
32
output , err := cmd .Flags ().GetString ("output" )
30
33
if err != nil {
31
34
return err
@@ -38,11 +41,33 @@ func genmanAction(cmd *cobra.Command, args []string) error {
38
41
if err != nil {
39
42
return err
40
43
}
44
+ outputType , err := cmd .Flags ().GetString ("type" )
45
+ if err != nil {
46
+ return err
47
+ }
41
48
homeDir , err := os .UserHomeDir ()
42
49
if err != nil {
43
50
return err
44
51
}
45
52
dir := args [0 ]
53
+ switch outputType {
54
+ case "man" :
55
+ if err := genMan (cmd , dir ); err != nil {
56
+ return err
57
+ }
58
+ case "docsy" :
59
+ if err := genDocsy (cmd , dir ); err != nil {
60
+ return err
61
+ }
62
+ }
63
+ if output != "" && prefix != "" {
64
+ replaceAll (dir , output , prefix )
65
+ }
66
+ replaceAll (dir , homeDir , "~" )
67
+ return nil
68
+ }
69
+
70
+ func genMan (cmd * cobra.Command , dir string ) error {
46
71
logrus .Infof ("Generating man %q" , dir )
47
72
// lima(1)
48
73
filePath := filepath .Join (dir , "lima.1" )
@@ -69,14 +94,25 @@ and $LIMA_WORKDIR.
69
94
Title : "LIMACTL" ,
70
95
Section : "1" ,
71
96
}
72
- if err := doc .GenManTree (cmd .Root (), header , dir ); err != nil {
73
- return err
74
- }
75
- if output != "" && prefix != "" {
76
- replaceAll (dir , output , prefix )
77
- }
78
- replaceAll (dir , homeDir , "~" )
79
- return nil
97
+ return doc .GenManTree (cmd .Root (), header , dir )
98
+ }
99
+
100
+ func genDocsy (cmd * cobra.Command , dir string ) error {
101
+ return doc .GenMarkdownTreeCustom (cmd .Root (), dir , func (s string ) string {
102
+ //Replace limactl_completion_bash to completion bash for docsy title
103
+ name := filepath .Base (s )
104
+ name = strings .ReplaceAll (name , "limactl_" , "" )
105
+ name = strings .ReplaceAll (name , "_" , " " )
106
+ name = strings .TrimSuffix (name , filepath .Ext (name ))
107
+ return fmt .Sprintf (`---
108
+ title: %s
109
+ weight: 3
110
+ ---
111
+ ` , name )
112
+ }, func (s string ) string {
113
+ //Use ../ for move one folder up for docsy
114
+ return "../" + strings .TrimSuffix (s , filepath .Ext (s ))
115
+ })
80
116
}
81
117
82
118
// replaceAll replaces all occurrences of new with old, for all files in dir
0 commit comments