cli: fix builder persistent flag

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2021-11-04 18:52:11 +01:00
parent 4c938c77ba
commit eab0e6a8fe
34 changed files with 277 additions and 172 deletions

View File

@@ -15,38 +15,90 @@
package clidocstool
import (
"errors"
"io"
"os"
"github.com/spf13/cobra"
)
// 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 GenTree(cmd *cobra.Command, dir string) error {
const (
// AnnotationExternalUrl specifies an external link annotation
AnnotationExternalUrl = "docs.external.url"
)
// Options defines options for cli-docs-tool
type Options struct {
Root *cobra.Command
SourceDir string
TargetDir string
Plugin bool
}
// Client represents an active cli-docs-tool object
type Client struct {
root *cobra.Command
source string
target string
plugin bool
}
// New initializes a new cli-docs-tool client
func New(opts Options) (*Client, error) {
if opts.Root == nil {
return nil, errors.New("root cmd required")
}
if len(opts.SourceDir) == 0 {
return nil, errors.New("source dir required")
}
c := &Client{
root: opts.Root,
source: opts.SourceDir,
plugin: opts.Plugin,
}
if len(opts.TargetDir) == 0 {
c.target = c.source
} else {
c.target = opts.TargetDir
}
if err := os.MkdirAll(c.target, 0755); err != nil {
return nil, err
}
return c, nil
}
// GenAllTree creates all structured ref files for this command and
// all descendants in the directory given.
func (c *Client) GenAllTree() error {
var err error
if err = GenMarkdownTree(cmd, dir); err != nil {
if err = c.GenMarkdownTree(c.root); err != nil {
return err
}
if err = GenYamlTree(cmd, dir); err != nil {
if err = c.GenYamlTree(c.root); err != nil {
return err
}
return nil
}
// VisitAll will traverse all commands from the root.
// This is different from the VisitAll of cobra.Command where only parents
// are checked.
func VisitAll(root *cobra.Command, fn func(*cobra.Command)) {
for _, cmd := range root.Commands() {
VisitAll(cmd, fn)
func fileExists(f string) bool {
info, err := os.Stat(f)
if os.IsNotExist(err) {
return false
}
fn(root)
return !info.IsDir()
}
// DisableFlagsInUseLine sets the DisableFlagsInUseLine flag on all
// commands within the tree rooted at cmd.
func DisableFlagsInUseLine(cmd *cobra.Command) {
VisitAll(cmd, func(ccmd *cobra.Command) {
// do not add a `[flags]` to the end of the usage line.
ccmd.DisableFlagsInUseLine = true
})
func copyFile(src string, dst string) error {
sf, err := os.Open(src)
if err != nil {
return err
}
defer sf.Close()
df, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
return err
}