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. // 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. 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. }