Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
const igniteBinaryName = "ignite"
|
|
|
|
// Path returns the absolute command path including the binary name as prefix.
|
|
func (c *Command) Path() string {
|
|
return ensureFullCommandPath(c.PlaceCommandUnder)
|
|
}
|
|
|
|
// ToCobraCommand returns a new Cobra command that matches the current command.
|
|
func (c *Command) ToCobraCommand() (*cobra.Command, error) {
|
|
cmd := &cobra.Command{
|
|
Use: c.Use,
|
|
Aliases: c.Aliases,
|
|
Short: c.Short,
|
|
Long: c.Long,
|
|
Hidden: c.Hidden,
|
|
}
|
|
|
|
for _, f := range c.Flags {
|
|
var fs *pflag.FlagSet
|
|
if f.Persistent {
|
|
fs = cmd.PersistentFlags()
|
|
} else {
|
|
fs = cmd.Flags()
|
|
}
|
|
|
|
if err := f.ExportToFlagSet(fs); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return cmd, nil
|
|
}
|
|
|
|
// ImportFlags imports flags from a Cobra command.
|
|
func (c *ExecutedCommand) ImportFlags(cmd *cobra.Command) {
|
|
c.Flags = extractCobraFlags(cmd)
|
|
}
|
|
|
|
// NewFlags creates a new flags set initialized with the executed command's flags.
|
|
func (c *ExecutedCommand) NewFlags() (*pflag.FlagSet, error) {
|
|
fs := pflag.NewFlagSet(igniteBinaryName, pflag.ContinueOnError)
|
|
|
|
for _, f := range c.Flags {
|
|
if f.Persistent {
|
|
continue
|
|
}
|
|
|
|
if err := f.ExportToFlagSet(fs); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return fs, nil
|
|
}
|
|
|
|
// NewPersistentFlags creates a new flags set initialized with the executed command's persistent flags.
|
|
func (c *ExecutedCommand) NewPersistentFlags() (*pflag.FlagSet, error) {
|
|
fs := pflag.NewFlagSet(igniteBinaryName, pflag.ContinueOnError)
|
|
|
|
for _, f := range c.Flags {
|
|
if !f.Persistent {
|
|
continue
|
|
}
|
|
|
|
if err := f.ExportToFlagSet(fs); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return fs, nil
|
|
}
|
|
|
|
func ensureFullCommandPath(path string) string {
|
|
path = strings.TrimSpace(path)
|
|
if path == "" {
|
|
return igniteBinaryName
|
|
}
|
|
|
|
if !strings.HasPrefix(path, igniteBinaryName) {
|
|
path = igniteBinaryName + " " + path
|
|
}
|
|
return path
|
|
}
|