|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + clidocstool "github.com/docker/cli-docs-tool" |
| 9 | + "github.com/docker/cli/cli/command" |
| 10 | + "github.com/docker/model-cli/commands" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/pflag" |
| 14 | +) |
| 15 | + |
| 16 | +const defaultSourcePath = "docs/reference/" |
| 17 | + |
| 18 | +type options struct { |
| 19 | + source string |
| 20 | + formats []string |
| 21 | +} |
| 22 | + |
| 23 | +func gen(opts *options) error { |
| 24 | + log.SetFlags(0) |
| 25 | + |
| 26 | + dockerCLI, err := command.NewDockerCli() |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "docker [OPTIONS] COMMAND [ARG...]", |
| 32 | + Short: "The base command for the Docker CLI.", |
| 33 | + DisableAutoGenTag: true, |
| 34 | + } |
| 35 | + |
| 36 | + cmd.AddCommand(commands.NewRootCmd(dockerCLI)) |
| 37 | + |
| 38 | + c, err := clidocstool.New(clidocstool.Options{ |
| 39 | + Root: cmd, |
| 40 | + SourceDir: opts.source, |
| 41 | + Plugin: true, |
| 42 | + }) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + for _, format := range opts.formats { |
| 48 | + switch format { |
| 49 | + case "md": |
| 50 | + if err = c.GenMarkdownTree(cmd); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + case "yaml": |
| 54 | + fixUpExperimentalCLI(cmd) |
| 55 | + if err = c.GenYamlTree(cmd); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + default: |
| 59 | + return errors.Errorf("unknown format %q", format) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func run() error { |
| 67 | + opts := &options{} |
| 68 | + flags := pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError) |
| 69 | + flags.StringVar(&opts.source, "source", defaultSourcePath, "Docs source folder") |
| 70 | + flags.StringSliceVar(&opts.formats, "formats", []string{}, "Format (md, yaml)") |
| 71 | + if err := flags.Parse(os.Args[1:]); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + if len(opts.formats) == 0 { |
| 75 | + return errors.New("Docs format required") |
| 76 | + } |
| 77 | + return gen(opts) |
| 78 | +} |
| 79 | + |
| 80 | +func main() { |
| 81 | + if err := run(); err != nil { |
| 82 | + log.Printf("ERROR: %+v", err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// fixUpExperimentalCLI trims the " (EXPERIMENTAL)" suffix from the CLI output, |
| 88 | +// as docs.docker.com already displays "experimental (CLI)", |
| 89 | +// |
| 90 | +// https://github.com/docker/buildx/pull/2188#issuecomment-1889487022 |
| 91 | +func fixUpExperimentalCLI(cmd *cobra.Command) { |
| 92 | + const ( |
| 93 | + annotationExperimentalCLI = "experimentalCLI" |
| 94 | + suffixExperimental = " (EXPERIMENTAL)" |
| 95 | + ) |
| 96 | + if _, ok := cmd.Annotations[annotationExperimentalCLI]; ok { |
| 97 | + cmd.Short = strings.TrimSuffix(cmd.Short, suffixExperimental) |
| 98 | + } |
| 99 | + cmd.Flags().VisitAll(func(f *pflag.Flag) { |
| 100 | + if _, ok := f.Annotations[annotationExperimentalCLI]; ok { |
| 101 | + f.Usage = strings.TrimSuffix(f.Usage, suffixExperimental) |
| 102 | + } |
| 103 | + }) |
| 104 | + for _, c := range cmd.Commands() { |
| 105 | + fixUpExperimentalCLI(c) |
| 106 | + } |
| 107 | +} |
0 commit comments