clidocstool

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2021 License: Apache-2.0 Imports: 16 Imported by: 8

README

PkgGoDev Test Status Go Report Card

About

This is a library containing utilities to generate (reference) documentation for the docker CLI on docs.docker.com.

Disclaimer

This library is intended for use by Docker's CLIs, and is not intended to be a general-purpose utility. Various bits are hard-coded or make assumptions that are very specific to our use-case. Contributions are welcome, but we will not accept contributions to make this a general-purpose module.

Usage

To generate the documentation it's recommended to do so using a Go submodule in your repository.

We will use the example of docker/buildx and create a Go submodule in a docs folder (recommended):

$ mkdir docs
$ cd ./docs
$ go mod init github.com/docker/buildx/docs
$ go get github.com/docker/cli-docs-tool

Your go.mod should look like this:

module github.com/docker/buildx/docs

go 1.16

require (
	github.com/docker/cli-docs-tool v0.0.0
)

Next, create a file named docgen.go inside that directory containing the following Go code:

package main

import (
  "log"
  "os"
  "path/filepath"

  "github.com/docker/buildx/commands"
  "github.com/docker/cli/cli/command"
  clidocstool "github.com/docker/cli-docs-tool"
  "github.com/spf13/cobra"
)

const sourcePath = "docs/"

func main() {
  log.SetFlags(0)

  dockerCLI, err := command.NewDockerCli()
  if err != nil {
    log.Printf("ERROR: %+v", err)
  }

  cmd := &cobra.Command{
    Use:               "docker [OPTIONS] COMMAND [ARG...]",
    Short:             "The base command for the Docker CLI.",
    DisableAutoGenTag: true,
  }

  cmd.AddCommand(commands.NewRootCmd("buildx", true, dockerCLI))
  clidocstool.DisableFlagsInUseLine(cmd)

  cwd, _ := os.Getwd()
  source := filepath.Join(cwd, sourcePath)

  // Make sure "source" folder is created first
  if err = os.MkdirAll(source, 0755); err != nil {
    log.Printf("ERROR: %+v", err)
  }
  
  // Generate Markdown and YAML documentation to "source" folder
  if err = clidocstool.GenTree(cmd, source); err != nil {
    log.Printf("ERROR: %+v", err)
  }
}

Here we create a new instance of Docker CLI with command.NewDockerCli and a subcommand commands.NewRootCmd for buildx.

Finally, we generate Markdown and YAML documentation with clidocstool.GenTree.

$ go run main.go
INFO: Generating Markdown for "docker buildx bake"
INFO: Generating Markdown for "docker buildx build"
INFO: Generating Markdown for "docker buildx create"
INFO: Generating Markdown for "docker buildx du"
...
INFO: Generating YAML for "docker buildx uninstall"
INFO: Generating YAML for "docker buildx use"
INFO: Generating YAML for "docker buildx version"
INFO: Generating YAML for "docker buildx"

Generated docs will be available in the ./docs folder of the project.

Contributing

Want to contribute? Awesome! You can find information about contributing to this project in the CONTRIBUTING.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableFlagsInUseLine

func DisableFlagsInUseLine(cmd *cobra.Command)

DisableFlagsInUseLine sets the DisableFlagsInUseLine flag on all commands within the tree rooted at cmd.

func GenMarkdownTree

func GenMarkdownTree(cmd *cobra.Command, dir string) error

GenMarkdownTree will generate a markdown page for this command and all descendants in the directory given.

func GenTree

func GenTree(cmd *cobra.Command, dir string) error

GenTree creates yaml and markdown structured ref files for this command and all descendants in the directory given. This function will just call GenMarkdownTree and GenYamlTree functions successively.

func GenYamlCustom

func GenYamlCustom(cmd *cobra.Command, w io.Writer) error

GenYamlCustom creates custom yaml output. nolint: gocyclo

func GenYamlTree

func GenYamlTree(cmd *cobra.Command, dir string) error

GenYamlTree creates yaml structured ref files for this command and all descendants in the directory given. This function may not work correctly if your command names have `-` in them. If you have `cmd` with two subcmds, `sub` and `sub-third`, and `sub` has a subcommand called `third` it is undefined which help output will be in the file `cmd-sub-third.1`.

func GenYamlTreeCustom

func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string) string) error

GenYamlTreeCustom creates yaml structured ref files.

func VisitAll

func VisitAll(root *cobra.Command, fn func(*cobra.Command))

VisitAll will traverse all commands from the root. This is different from the VisitAll of cobra.Command where only parents are checked.

Types

This section is empty.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL