code.gitea.io/gitea@v1.22.3/cmd/docs.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/urfave/cli/v2"
    12  )
    13  
    14  // CmdDocs represents the available docs sub-command.
    15  var CmdDocs = &cli.Command{
    16  	Name:        "docs",
    17  	Usage:       "Output CLI documentation",
    18  	Description: "A command to output Gitea's CLI documentation, optionally to a file.",
    19  	Action:      runDocs,
    20  	Flags: []cli.Flag{
    21  		&cli.BoolFlag{
    22  			Name:  "man",
    23  			Usage: "Output man pages instead",
    24  		},
    25  		&cli.StringFlag{
    26  			Name:    "output",
    27  			Aliases: []string{"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  		firstHashtagIndex := strings.Index(docs, "#")
    47  
    48  		if firstHashtagIndex > 0 {
    49  			docs = docs[firstHashtagIndex:]
    50  		}
    51  	}
    52  
    53  	out := os.Stdout
    54  	if ctx.String("output") != "" {
    55  		fi, err := os.Create(ctx.String("output"))
    56  		if err != nil {
    57  			return err
    58  		}
    59  		defer fi.Close()
    60  		out = fi
    61  	}
    62  
    63  	_, err = fmt.Fprintln(out, docs)
    64  	return err
    65  }