Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/cmd/limactl/gendoc.go
1645 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package main
5
6
import (
7
"bytes"
8
"fmt"
9
"io/fs"
10
"os"
11
"path/filepath"
12
"strings"
13
14
"github.com/cpuguy83/go-md2man/v2/md2man"
15
"github.com/sirupsen/logrus"
16
"github.com/spf13/cobra"
17
"github.com/spf13/cobra/doc"
18
)
19
20
func newGenDocCommand() *cobra.Command {
21
genmanCommand := &cobra.Command{
22
Use: "generate-doc DIR",
23
Short: "Generate cli-reference pages",
24
Args: WrapArgsError(cobra.MinimumNArgs(1)),
25
RunE: gendocAction,
26
Hidden: true,
27
}
28
genmanCommand.Flags().String("type", "man", "Output type (man, docsy)")
29
genmanCommand.Flags().String("output", "", "Output directory")
30
genmanCommand.Flags().String("prefix", "", "Install prefix")
31
return genmanCommand
32
}
33
34
func gendocAction(cmd *cobra.Command, args []string) error {
35
output, err := cmd.Flags().GetString("output")
36
if err != nil {
37
return err
38
}
39
output, err = filepath.Abs(output)
40
if err != nil {
41
return err
42
}
43
prefix, err := cmd.Flags().GetString("prefix")
44
if err != nil {
45
return err
46
}
47
outputType, err := cmd.Flags().GetString("type")
48
if err != nil {
49
return err
50
}
51
homeDir, err := os.UserHomeDir()
52
if err != nil {
53
return err
54
}
55
dir := args[0]
56
switch outputType {
57
case "man":
58
if err := genMan(cmd, dir); err != nil {
59
return err
60
}
61
case "docsy":
62
if err := genDocsy(cmd, dir); err != nil {
63
return err
64
}
65
}
66
if output != "" && prefix != "" {
67
if err := replaceAll(dir, output, prefix); err != nil {
68
return err
69
}
70
}
71
return replaceAll(dir, homeDir, "~")
72
}
73
74
func genMan(cmd *cobra.Command, dir string) error {
75
logrus.Infof("Generating man %q", dir)
76
// lima(1)
77
filePath := filepath.Join(dir, "lima.1")
78
md := "LIMA 1\n======" + `
79
# NAME
80
lima - ` + cmd.Root().Short + `
81
# SYNOPSIS
82
**lima** [_COMMAND_...]
83
# DESCRIPTION
84
lima is an alias for "limactl shell default".
85
The instance name ("default") can be changed by specifying $LIMA_INSTANCE.
86
87
The shell and initial workdir inside the instance can be specified via $LIMA_SHELL
88
and $LIMA_WORKDIR.
89
# SEE ALSO
90
**limactl**(1)
91
`
92
out := md2man.Render([]byte(md))
93
if err := os.WriteFile(filePath, out, 0o644); err != nil {
94
return err
95
}
96
// limactl(1)
97
header := &doc.GenManHeader{
98
Title: "LIMACTL",
99
Section: "1",
100
}
101
return doc.GenManTree(cmd.Root(), header, dir)
102
}
103
104
func genDocsy(cmd *cobra.Command, dir string) error {
105
return doc.GenMarkdownTreeCustom(cmd.Root(), dir, func(s string) string {
106
// Replace limactl_completion_bash to completion bash for docsy title
107
name := filepath.Base(s)
108
name = strings.ReplaceAll(name, "limactl_", "")
109
name = strings.ReplaceAll(name, "_", " ")
110
name = strings.TrimSuffix(name, filepath.Ext(name))
111
return fmt.Sprintf(`---
112
title: %s
113
weight: 3
114
---
115
`, name)
116
}, func(s string) string {
117
// Use ../ for move one folder up for docsy
118
return "../" + strings.TrimSuffix(s, filepath.Ext(s))
119
})
120
}
121
122
// replaceAll replaces all occurrences of text with replacement, for all files in dir.
123
func replaceAll(dir, text, replacement string) error {
124
logrus.Infof("Replacing %q with %q", text, replacement)
125
return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
126
if err != nil {
127
return err
128
}
129
if path == dir {
130
return nil
131
}
132
if info.IsDir() {
133
return filepath.SkipDir
134
}
135
in, err := os.ReadFile(path)
136
if err != nil {
137
return err
138
}
139
out := bytes.ReplaceAll(in, []byte(text), []byte(replacement))
140
err = os.WriteFile(path, out, 0o644)
141
if err != nil {
142
return err
143
}
144
return nil
145
})
146
}
147
148