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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. "github.com/urfave/cli"
  10. )
  11. // CmdDocs represents the available docs sub-command.
  12. var CmdDocs = cli.Command{
  13. Name: "docs",
  14. Usage: "Output CLI documentation",
  15. Description: "A command to output Gitea's CLI documentation, optionally to a file.",
  16. Action: runDocs,
  17. Flags: []cli.Flag{
  18. &cli.BoolFlag{
  19. Name: "man",
  20. Usage: "Output man pages instead",
  21. },
  22. &cli.StringFlag{
  23. Name: "output, 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. docs = docs[strings.Index(docs, "#"):]
  41. }
  42. out := os.Stdout
  43. if ctx.String("output") != "" {
  44. fi, err := os.Create(ctx.String("output"))
  45. if err != nil {
  46. return err
  47. }
  48. defer fi.Close()
  49. out = fi
  50. }
  51. _, err = fmt.Fprintln(out, docs)
  52. return err
  53. }