Skip to content

Commit 938ea3e

Browse files
authored
Merge pull request #1778 from balajiv113/generate-doc
Support for generating cli reference with different output formats
2 parents 7dd1505 + ac46ad1 commit 938ea3e

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ _output/share/lima/lima-guestagent.Linux-riscv64:
148148
.PHONY: manpages
149149
manpages: _output/bin/limactl$(exe)
150150
@mkdir -p _output/share/man/man1
151-
$< generate-man _output/share/man/man1 \
151+
$< generate-doc _output/share/man/man1 \
152+
--output _output --prefix $(PREFIX)
153+
154+
.PHONY: docsy
155+
docsy: _output/bin/limactl$(exe)
156+
@mkdir -p website/_output/docsy
157+
$< generate-doc --type docsy website/_output/docsy \
152158
--output _output --prefix $(PREFIX)
153159

154160
.PHONY: diagrams

cmd/limactl/genman.go renamed to cmd/limactl/gendoc.go

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@ package main
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/fs"
67
"os"
78
"path/filepath"
9+
"strings"
810

911
"github.com/cpuguy83/go-md2man/v2/md2man"
1012
"github.com/sirupsen/logrus"
1113
"github.com/spf13/cobra"
1214
"github.com/spf13/cobra/doc"
1315
)
1416

15-
func newGenManCommand() *cobra.Command {
17+
func newGenDocCommand() *cobra.Command {
1618
genmanCommand := &cobra.Command{
17-
Use: "generate-man DIR",
18-
Short: "Generate manual pages",
19+
Use: "generate-doc DIR",
20+
Short: "Generate cli-reference pages",
1921
Args: WrapArgsError(cobra.MinimumNArgs(1)),
20-
RunE: genmanAction,
22+
RunE: gendocAction,
2123
Hidden: true,
2224
}
25+
genmanCommand.Flags().String("type", "man", "Output type (man, docsy)")
2326
genmanCommand.Flags().String("output", "", "Output directory")
2427
genmanCommand.Flags().String("prefix", "", "Install prefix")
2528
return genmanCommand
2629
}
2730

28-
func genmanAction(cmd *cobra.Command, args []string) error {
31+
func gendocAction(cmd *cobra.Command, args []string) error {
2932
output, err := cmd.Flags().GetString("output")
3033
if err != nil {
3134
return err
@@ -38,11 +41,33 @@ func genmanAction(cmd *cobra.Command, args []string) error {
3841
if err != nil {
3942
return err
4043
}
44+
outputType, err := cmd.Flags().GetString("type")
45+
if err != nil {
46+
return err
47+
}
4148
homeDir, err := os.UserHomeDir()
4249
if err != nil {
4350
return err
4451
}
4552
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 {
4671
logrus.Infof("Generating man %q", dir)
4772
// lima(1)
4873
filePath := filepath.Join(dir, "lima.1")
@@ -69,14 +94,25 @@ and $LIMA_WORKDIR.
6994
Title: "LIMACTL",
7095
Section: "1",
7196
}
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+
})
80116
}
81117

82118
// replaceAll replaces all occurrences of new with old, for all files in dir

cmd/limactl/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func newApp() *cobra.Command {
8383
formatter.ForceColors = true
8484
logrus.StandardLogger().SetFormatter(formatter)
8585
}
86-
if os.Geteuid() == 0 && cmd.Name() != "generate-man" {
86+
if os.Geteuid() == 0 && cmd.Name() != "generate-doc" {
8787
return errors.New("must not run as the root")
8888
}
8989
// Make sure either $HOME or $LIMA_HOME is defined, so we don't need
@@ -112,7 +112,7 @@ func newApp() *cobra.Command {
112112
newFactoryResetCommand(),
113113
newDiskCommand(),
114114
newUsernetCommand(),
115-
newGenManCommand(),
115+
newGenDocCommand(),
116116
newSnapshotCommand(),
117117
)
118118
return rootCmd

0 commit comments

Comments
 (0)