Skip to content

Commit 172eca1

Browse files
authored
Add docs command (#13429)
Signed-off-by: jolheiser <[email protected]>
1 parent 30ce373 commit 172eca1

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

cmd/docs.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"strings"
11+
12+
"github.com/urfave/cli"
13+
)
14+
15+
// CmdDocs represents the available docs sub-command.
16+
var CmdDocs = cli.Command{
17+
Name: "docs",
18+
Usage: "Output CLI documentation",
19+
Description: "A command to output Gitea's CLI documentation, optionally to a file.",
20+
Action: runDocs,
21+
Flags: []cli.Flag{
22+
&cli.BoolFlag{
23+
Name: "man",
24+
Usage: "Output man pages instead",
25+
},
26+
&cli.StringFlag{
27+
Name: "output, o",
28+
Usage: "Path to output to instead of stdout (will overwrite if exists)",
29+
},
30+
},
31+
}
32+
33+
func runDocs(ctx *cli.Context) error {
34+
docs, err := ctx.App.ToMarkdown()
35+
if ctx.Bool("man") {
36+
docs, err = ctx.App.ToMan()
37+
}
38+
if err != nil {
39+
return err
40+
}
41+
42+
if !ctx.Bool("man") {
43+
// Clean up markdown. The following bug was fixed in v2, but is present in v1.
44+
// It affects markdown output (even though the issue is referring to man pages)
45+
// https://github.com/urfave/cli/issues/1040
46+
docs = docs[strings.Index(docs, "#"):]
47+
}
48+
49+
out := os.Stdout
50+
if ctx.String("output") != "" {
51+
fi, err := os.Create(ctx.String("output"))
52+
if err != nil {
53+
return err
54+
}
55+
defer fi.Close()
56+
out = fi
57+
}
58+
59+
_, err = fmt.Fprintln(out, docs)
60+
return err
61+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ arguments - which can alternatively be run by running the subcommand web.`
6969
cmd.CmdManager,
7070
cmd.Cmdembedded,
7171
cmd.CmdMigrateStorage,
72+
cmd.CmdDocs,
7273
}
7374
// Now adjust these commands to add our global configuration options
7475

0 commit comments

Comments
 (0)