2022-02-25 11:11:04 -05:00
|
|
|
// This file is intended for use with "go run"; it isn't really part of the package.
|
|
|
|
|
2022-03-26 13:34:10 -04:00
|
|
|
//go:build docsgen
|
2022-02-25 11:11:04 -05:00
|
|
|
// +build docsgen
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
clidocstool "github.com/docker/cli-docs-tool"
|
2023-01-06 13:04:05 -05:00
|
|
|
"github.com/docker/cli/cli"
|
2022-02-25 11:11:04 -05:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/commands"
|
2023-01-06 13:04:05 -05:00
|
|
|
"github.com/pkg/errors"
|
2022-02-25 11:11:04 -05:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultSourcePath = "docs/reference/commandline/"
|
|
|
|
|
|
|
|
type options struct {
|
2023-01-06 13:04:05 -05:00
|
|
|
source string
|
|
|
|
target string
|
|
|
|
formats []string
|
2022-02-25 11:11:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func gen(opts *options) error {
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
|
|
|
dockerCLI, err := command.NewDockerCli()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "docker [OPTIONS] COMMAND [ARG...]",
|
|
|
|
Short: "The base command for the Docker CLI.",
|
|
|
|
}
|
2023-01-06 13:04:05 -05:00
|
|
|
clientOpts, _, _ := cli.SetupRootCommand(cmd)
|
|
|
|
if err := dockerCLI.Initialize(clientOpts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-25 11:11:04 -05:00
|
|
|
commands.AddCommands(cmd, dockerCLI)
|
|
|
|
|
|
|
|
c, err := clidocstool.New(clidocstool.Options{
|
|
|
|
Root: cmd,
|
|
|
|
SourceDir: opts.source,
|
|
|
|
TargetDir: opts.target,
|
|
|
|
Plugin: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
for _, format := range opts.formats {
|
|
|
|
switch format {
|
|
|
|
case "md":
|
|
|
|
if err = c.GenMarkdownTree(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "yaml":
|
|
|
|
if err = c.GenYamlTree(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown format %q", format)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-02-25 11:11:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func run() error {
|
|
|
|
opts := &options{}
|
|
|
|
flags := pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
|
|
|
|
flags.StringVar(&opts.source, "source", defaultSourcePath, "Docs source folder")
|
|
|
|
flags.StringVar(&opts.target, "target", defaultSourcePath, "Docs target folder")
|
2023-01-06 13:04:05 -05:00
|
|
|
flags.StringSliceVar(&opts.formats, "formats", []string{}, "Format (md, yaml)")
|
2022-02-25 11:11:04 -05:00
|
|
|
if err := flags.Parse(os.Args[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-06 13:04:05 -05:00
|
|
|
if len(opts.formats) == 0 {
|
|
|
|
return errors.New("Docs format required")
|
|
|
|
}
|
2022-02-25 11:11:04 -05:00
|
|
|
return gen(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
log.Printf("ERROR: %+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|