File: man.go

package info (click to toggle)
tea-cli 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,648 kB
  • sloc: makefile: 116; sh: 17
file content (62 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download
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
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package cmd

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	docs "github.com/urfave/cli-docs/v3"
	"github.com/urfave/cli/v3"
)

// DocRenderFlags are the flags for documentation generation, used by `./docs/docs.go` and the `generate-man-page` sub command
var DocRenderFlags = []cli.Flag{
	&cli.StringFlag{
		Name:    "out",
		Usage:   "Path to output docs to, otherwise prints to stdout",
		Aliases: []string{"o"},
	},
}

// CmdGenerateManPage is the sub command to generate the `tea` man page
var CmdGenerateManPage = cli.Command{
	Name:   "man",
	Usage:  "Generate man page",
	Hidden: true,
	Flags:  DocRenderFlags,
	Action: func(ctx context.Context, cmd *cli.Command) error {
		return RenderDocs(cmd, cmd.Root(), docs.ToMan)
	},
}

// RenderDocs renders the documentation for `target` using the supplied `render` function
func RenderDocs(cmd, target *cli.Command, render func(*cli.Command) (string, error)) error {
	out, err := render(target)
	if err != nil {
		return err
	}
	outPath := cmd.String("out")
	if outPath == "" {
		fmt.Print(out)
		return nil
	}

	if err = os.MkdirAll(filepath.Dir(outPath), os.ModePerm); err != nil {
		return err
	}

	fi, err := os.Create(outPath)
	if err != nil {
		return err
	}
	defer fi.Close()
	if _, err = fi.WriteString(out); err != nil {
		return err
	}

	return nil
}