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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/v2"
  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",
  23. Aliases: []string{"o"},
  24. Usage: "Path to output to instead of stdout (will overwrite if exists)",
  25. },
  26. },
  27. }
  28. func runDocs(ctx *cli.Context) error {
  29. docs, err := ctx.App.ToMarkdown()
  30. if ctx.Bool("man") {
  31. docs, err = ctx.App.ToMan()
  32. }
  33. if err != nil {
  34. return err
  35. }
  36. if !ctx.Bool("man") {
  37. // Clean up markdown. The following bug was fixed in v2, but is present in v1.
  38. // It affects markdown output (even though the issue is referring to man pages)
  39. // https://github.com/urfave/cli/issues/1040
  40. firstHashtagIndex := strings.Index(docs, "#")
  41. if firstHashtagIndex > 0 {
  42. docs = docs[firstHashtagIndex:]
  43. }
  44. }
  45. out := os.Stdout
  46. if ctx.String("output") != "" {
  47. fi, err := os.Create(ctx.String("output"))
  48. if err != nil {
  49. return err
  50. }
  51. defer fi.Close()
  52. out = fi
  53. }
  54. _, err = fmt.Fprintln(out, docs)
  55. return err
  56. }