跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

gendoc.go

165 行 · 3.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
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
20func 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
34func 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
74func 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
80lima - ` + cmd.Root().Short + `
81# SYNOPSIS
82**lima** [_COMMAND_...]
83# DESCRIPTION
84lima is an alias for "limactl shell default".
85The instance name ("default") can be changed by specifying $LIMA_INSTANCE.
86
87The shell and initial workdir inside the instance can be specified via $LIMA_SHELL
88and $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
104func escapeMarkdown(text string) string {
105 lines := strings.Split(text, "\n")
106 for i := range lines {
107 // Need to escape backticks first, before adding more
108 for c := range strings.SplitSeq("\\`*_[]()#+-.|", "") {
109 lines[i] = strings.ReplaceAll(lines[i], c, "\\"+c)
110 }
111 if i < len(lines)-1 {
112 if lines[i] != "" && lines[i+1] != "" {
113 lines[i] += " " // line break
114 }
115 }
116 }
117 return strings.Join(lines, "\n")
118}
119
120func genDocsy(cmd *cobra.Command, dir string) error {
121 for _, c := range cmd.Root().Commands() {
122 c.Long = escapeMarkdown(c.Long)
123 }
124 return doc.GenMarkdownTreeCustom(cmd.Root(), dir, func(s string) string {
125 // Replace limactl_completion_bash to completion bash for docsy title
126 name := filepath.Base(s)
127 name = strings.ReplaceAll(name, "limactl_", "")
128 name = strings.ReplaceAll(name, "_", " ")
129 name = strings.TrimSuffix(name, filepath.Ext(name))
130 return fmt.Sprintf(`---
131title: %s
132weight: 3
133---
134`, name)
135 }, func(s string) string {
136 // Use ../ for move one folder up for docsy
137 return "../" + strings.TrimSuffix(s, filepath.Ext(s))
138 })
139}
140
141// replaceAll replaces all occurrences of text with replacement, for all files in dir.
142func replaceAll(dir, text, replacement string) error {
143 logrus.Infof("Replacing %#q with %#q", text, replacement)
144 return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
145 if err != nil {
146 return err
147 }
148 if path == dir {
149 return nil
150 }
151 if info.IsDir() {
152 return filepath.SkipDir
153 }
154 in, err := os.ReadFile(path)
155 if err != nil {
156 return err
157 }
158 out := bytes.ReplaceAll(in, []byte(text), []byte(replacement))
159 err = os.WriteFile(path, out, 0o644)
160 if err != nil {
161 return err
162 }
163 return nil
164 })
165}