gitea源码

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