summaryrefslogtreecommitdiffstats
path: root/cmd/docs.go
blob: 073c57497305f1a4a39a02240ac9ddd9f9651169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cmd

import (
	"fmt"
	"os"
	"strings"

	"github.com/urfave/cli"
)

// CmdDocs represents the available docs sub-command.
var CmdDocs = cli.Command{
	Name:        "docs",
	Usage:       "Output CLI documentation",
	Description: "A command to output Gitea's CLI documentation, optionally to a file.",
	Action:      runDocs,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:  "man",
			Usage: "Output man pages instead",
		},
		&cli.StringFlag{
			Name:  "output, o",
			Usage: "Path to output to instead of stdout (will overwrite if exists)",
		},
	},
}

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
		firstHashtagIndex := strings.Index(docs, "#")

		if firstHashtagIndex > 0 {
			docs = docs[firstHashtagIndex:]
		}
	}

	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
}