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 (
|
2024-10-09 20:10:45 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-02-25 11:11:04 -05:00
|
|
|
"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"
|
2024-10-09 20:10:45 -04:00
|
|
|
|
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.",
|
|
|
|
}
|
2024-01-11 12:25:23 -05:00
|
|
|
|
2023-06-28 10:06:19 -04:00
|
|
|
clientOpts, _ := cli.SetupRootCommand(cmd)
|
2023-01-06 13:04:05 -05:00
|
|
|
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:
|
2024-10-09 20:10:45 -04:00
|
|
|
return fmt.Errorf("unknown format %q", format)
|
2023-01-06 13:04:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|