You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

docs.go 1.3KB

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