2020-11-07 01:32:57 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-11-07 01:32:57 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-11-07 01:32:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdDocs represents the available docs sub-command.
|
2023-07-21 11:28:19 +02:00
|
|
|
var CmdDocs = &cli.Command{
|
2020-11-07 01:32:57 +01:00
|
|
|
Name: "docs",
|
|
|
|
Usage: "Output CLI documentation",
|
2023-01-09 14:01:00 +01:00
|
|
|
Description: "A command to output Forgejo's CLI documentation, optionally to a file.",
|
2020-11-07 01:32:57 +01:00
|
|
|
Action: runDocs,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "man",
|
|
|
|
Usage: "Output man pages instead",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2023-07-21 11:28:19 +02:00
|
|
|
Name: "output",
|
|
|
|
Aliases: []string{"o"},
|
|
|
|
Usage: "Path to output to instead of stdout (will overwrite if exists)",
|
2020-11-07 01:32:57 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDocs(ctx *cli.Context) error {
|
|
|
|
docs, err := ctx.App.ToMarkdown()
|
|
|
|
if ctx.Bool("man") {
|
|
|
|
docs, err = ctx.App.ToMan()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Bool("man") {
|
|
|
|
// Clean up markdown. The following bug was fixed in v2, but is present in v1.
|
|
|
|
// It affects markdown output (even though the issue is referring to man pages)
|
|
|
|
// https://github.com/urfave/cli/issues/1040
|
2021-11-14 09:11:49 +01:00
|
|
|
firstHashtagIndex := strings.Index(docs, "#")
|
|
|
|
|
|
|
|
if firstHashtagIndex > 0 {
|
|
|
|
docs = docs[firstHashtagIndex:]
|
|
|
|
}
|
2020-11-07 01:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
out := os.Stdout
|
|
|
|
if ctx.String("output") != "" {
|
|
|
|
fi, err := os.Create(ctx.String("output"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
out = fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Fprintln(out, docs)
|
|
|
|
return err
|
|
|
|
}
|