generate.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "github.com/spf13/cobra"
  22. "github.com/spf13/pflag"
  23. "github.com/docker/compose-cli/cli/cmd/compose"
  24. . "github.com/docker/compose-cli/docs/yaml"
  25. )
  26. const descriptionSourcePath = "docs/reference/"
  27. func generateCliYaml(opts *options) error {
  28. cmd := &cobra.Command{Use: "docker"}
  29. cmd.AddCommand(compose.Command("local"))
  30. disableFlagsInUseLine(cmd)
  31. source := filepath.Join(opts.source, descriptionSourcePath)
  32. if err := loadLongDescription(cmd, source); err != nil {
  33. return err
  34. }
  35. cmd.DisableAutoGenTag = true
  36. return GenYamlTree(cmd, opts.target)
  37. }
  38. func disableFlagsInUseLine(cmd *cobra.Command) {
  39. visitAll(cmd, func(ccmd *cobra.Command) {
  40. // do not add a `[flags]` to the end of the usage line.
  41. ccmd.DisableFlagsInUseLine = true
  42. })
  43. }
  44. // visitAll will traverse all commands from the root.
  45. // This is different from the VisitAll of cobra.Command where only parents
  46. // are checked.
  47. func visitAll(root *cobra.Command, fn func(*cobra.Command)) {
  48. for _, cmd := range root.Commands() {
  49. visitAll(cmd, fn)
  50. }
  51. fn(root)
  52. }
  53. func loadLongDescription(cmd *cobra.Command, path ...string) error {
  54. for _, cmd := range cmd.Commands() {
  55. if cmd.Name() == "" {
  56. continue
  57. }
  58. fullpath := filepath.Join(path[0], strings.Join(append(path[1:], cmd.Name()), "_")+".md")
  59. if cmd.HasSubCommands() {
  60. if err := loadLongDescription(cmd, path[0], cmd.Name()); err != nil {
  61. return err
  62. }
  63. }
  64. if _, err := os.Stat(fullpath); err != nil {
  65. log.Printf("WARN: %s does not exist, skipping\n", fullpath)
  66. continue
  67. }
  68. content, err := ioutil.ReadFile(fullpath)
  69. if err != nil {
  70. return err
  71. }
  72. description, examples := ParseMDContent(string(content))
  73. cmd.Long = description
  74. cmd.Example = examples
  75. }
  76. return nil
  77. }
  78. type options struct {
  79. source string
  80. target string
  81. }
  82. func parseArgs() (*options, error) {
  83. opts := &options{}
  84. cwd, _ := os.Getwd()
  85. flags := pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
  86. flags.StringVar(&opts.source, "root", cwd, "Path to project root")
  87. flags.StringVar(&opts.target, "target", filepath.Join(cwd, "docs", "reference"), "Target path for generated yaml files")
  88. err := flags.Parse(os.Args[1:])
  89. return opts, err
  90. }
  91. func main() {
  92. opts, err := parseArgs()
  93. if err != nil {
  94. fmt.Fprintln(os.Stderr, err.Error())
  95. }
  96. fmt.Printf("Project root: %s\n", opts.source)
  97. fmt.Printf("Generating yaml files into %s\n", opts.target)
  98. if err := generateCliYaml(opts); err != nil {
  99. fmt.Fprintf(os.Stderr, "Failed to generate yaml files: %s\n", err.Error())
  100. }
  101. }