1
0

genman.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "errors"
  17. "fmt"
  18. "io/fs"
  19. "os"
  20. "github.com/rs/zerolog"
  21. "github.com/spf13/cobra"
  22. "github.com/spf13/cobra/doc"
  23. "github.com/drakkan/sftpgo/v2/internal/logger"
  24. "github.com/drakkan/sftpgo/v2/internal/version"
  25. )
  26. var (
  27. manDir string
  28. genManCmd = &cobra.Command{
  29. Use: "man",
  30. Short: "Generate man pages for sftpgo",
  31. Long: `This command automatically generates up-to-date man pages of SFTPGo's
  32. command-line interface.
  33. By default, it creates the man page files in the "man" directory under the
  34. current directory.
  35. `,
  36. Run: func(cmd *cobra.Command, _ []string) {
  37. logger.DisableLogger()
  38. logger.EnableConsoleLogger(zerolog.DebugLevel)
  39. if _, err := os.Stat(manDir); errors.Is(err, fs.ErrNotExist) {
  40. err = os.MkdirAll(manDir, os.ModePerm)
  41. if err != nil {
  42. logger.WarnToConsole("Unable to generate man page files: %v", err)
  43. os.Exit(1)
  44. }
  45. }
  46. header := &doc.GenManHeader{
  47. Section: "1",
  48. Manual: "SFTPGo Manual",
  49. Source: fmt.Sprintf("SFTPGo %v", version.Get().Version),
  50. }
  51. cmd.Root().DisableAutoGenTag = true
  52. err := doc.GenManTree(cmd.Root(), header, manDir)
  53. if err != nil {
  54. logger.WarnToConsole("Unable to generate man page files: %v", err)
  55. os.Exit(1)
  56. }
  57. },
  58. }
  59. )
  60. func init() {
  61. genManCmd.Flags().StringVarP(&manDir, "dir", "d", "man", "The directory to write the man pages")
  62. genCmd.AddCommand(genManCmd)
  63. }