Add attribute flag building and parsing (#1562)

This adds a few command sketches but that's not the main point of the PR
and filling those out will come in a different PR. This adds a new flag
type that can combine inputs to a single target slice, retaining the
ability to distinguish where it came from but also keep ordering across
different flag names. It also implements a function that does the
heuristic parsing with fallback.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-09-26 11:48:43 -04:00
1 parent 14cc97b645
commit a834a14785
16 files changed
+1166 -36

No files matched your search

@@ -0,0 +1,33 @@
package hostcatalogscmd
import (
"github.com/hashicorp/boundary/internal/cmd/base"
)
func (c *PluginCommand) extraPluginHelpFunc(helpMap map[string]func() string) string {
var helpStr string
switch c.Func {
case "create":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary host-catalogs create plugin [options] [args]",
"",
" Create a plugin-type host catalog. Example:",
"",
` $ boundary host-catalogs create plugin -scope-id p_1234567890 -name prodops -description "Plugin host-catalog for ProdOps"`,
"",
"",
})
case "update":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary host-catalogs update plugin [options] [args]",
"",
" Update a plugin-type host catalog given its ID. Example:",
"",
` $ boundary host-catalogs update plugin -id hc_1234567890 -name "devops" -description "Plugin host-catalog for DevOps"`,
"",
"",
})
}
return helpStr + c.Flags().Help()
}
@@ -0,0 +1,260 @@
// Code generated by "make cli"; DO NOT EDIT.
package hostcatalogscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/hostcatalogs"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
func initPluginFlags() {
flagsOnce.Do(func() {
extraFlags := extraPluginActionsFlagsMapFunc()
for k, v := range extraFlags {
flagsPluginMap[k] = append(flagsPluginMap[k], v...)
}
})
}
var (
_ cli.Command = (*PluginCommand)(nil)
_ cli.CommandAutocomplete = (*PluginCommand)(nil)
)
type PluginCommand struct {
*base.Command
Func string
plural string
}
func (c *PluginCommand) AutocompleteArgs() complete.Predictor {
initPluginFlags()
return complete.PredictAnything
}
func (c *PluginCommand) AutocompleteFlags() complete.Flags {
initPluginFlags()
return c.Flags().Completions()
}
func (c *PluginCommand) Synopsis() string {
if extra := extraPluginSynopsisFunc(c); extra != "" {
return extra
}
synopsisStr := "host catalog"
synopsisStr = fmt.Sprintf("%s %s", "plugin-type", synopsisStr)
return common.SynopsisFunc(c.Func, synopsisStr)
}
func (c *PluginCommand) Help() string {
initPluginFlags()
var helpStr string
helpMap := common.HelpMap("host catalog")
switch c.Func {
default:
helpStr = c.extraPluginHelpFunc(helpMap)
}
// Keep linter from complaining if we don't actually generate code using it
_ = helpMap
return helpStr
}
var flagsPluginMap = map[string][]string{
"create": {"scope-id", "name", "description", "attributes", "attr", "string-attr", "bool-attr", "num-attr"},
"update": {"id", "name", "description", "version", "attributes", "attr", "string-attr", "bool-attr", "num-attr"},
}
func (c *PluginCommand) Flags() *base.FlagSets {
if len(flagsPluginMap[c.Func]) == 0 {
return c.FlagSet(base.FlagSetNone)
}
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
f := set.NewFlagSet("Command Options")
common.PopulateCommonFlags(c.Command, f, "plugin-type host catalog", flagsPluginMap, c.Func)
f = set.NewFlagSet("Attribute Options")
common.PopulateAttributeFlags(c.Command, f, flagsPluginMap, c.Func)
extraPluginFlagsFunc(c, set, f)
return set
}
func (c *PluginCommand) Run(args []string) int {
initPluginFlags()
switch c.Func {
case "":
return cli.RunResultHelp
}
c.plural = "plugin-type host catalog"
switch c.Func {
case "list":
c.plural = "plugin-type host catalogs"
}
f := c.Flags()
if err := f.Parse(args); err != nil {
c.PrintCliError(err)
return base.CommandUserError
}
if strutil.StrListContains(flagsPluginMap[c.Func], "id") && c.FlagId == "" {
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return base.CommandUserError
}
var opts []hostcatalogs.Option
if strutil.StrListContains(flagsPluginMap[c.Func], "scope-id") {
switch c.Func {
case "create":
if c.FlagScopeId == "" {
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return base.CommandUserError
}
}
}
client, err := c.Client()
if err != nil {
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return base.CommandCliError
}
hostcatalogsClient := hostcatalogs.NewClient(client)
switch c.FlagName {
case "":
case "null":
opts = append(opts, hostcatalogs.DefaultName())
default:
opts = append(opts, hostcatalogs.WithName(c.FlagName))
}
switch c.FlagDescription {
case "":
case "null":
opts = append(opts, hostcatalogs.DefaultDescription())
default:
opts = append(opts, hostcatalogs.WithDescription(c.FlagDescription))
}
switch c.FlagRecursive {
case true:
opts = append(opts, hostcatalogs.WithRecursive(true))
}
if c.FlagFilter != "" {
opts = append(opts, hostcatalogs.WithFilter(c.FlagFilter))
}
var version uint32
switch c.Func {
case "update":
switch c.FlagVersion {
case 0:
opts = append(opts, hostcatalogs.WithAutomaticVersioning(true))
default:
version = uint32(c.FlagVersion)
}
}
if err := common.HandleAttributeFlags(
c.Command,
"attr",
func() {
hostcatalogs.DefaultAttributes()
},
func(in map[string]interface{}) {
hostcatalogs.WithAttributes(in)
}); err != nil {
c.PrintCliError(fmt.Errorf("Error evaluating attribute flags to: %s", err.Error()))
return base.CommandCliError
}
if ok := extraPluginFlagsHandlingFunc(c, f, &opts); !ok {
return base.CommandUserError
}
var result api.GenericResult
switch c.Func {
case "create":
result, err = hostcatalogsClient.Create(c.Context, "plugin", c.FlagScopeId, opts...)
case "update":
result, err = hostcatalogsClient.Update(c.Context, c.FlagId, version, opts...)
}
result, err = executeExtraPluginActions(c, result, err, hostcatalogsClient, version, opts)
if err != nil {
if apiErr := api.AsServerError(err); apiErr != nil {
var opts []base.Option
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...)
return base.CommandApiError
}
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return base.CommandCliError
}
output, err := printCustomPluginActionOutput(c)
if err != nil {
c.PrintCliError(err)
return base.CommandUserError
}
if output {
return base.CommandSuccess
}
switch c.Func {
}
switch base.Format(c.UI) {
case "table":
c.UI.Output(printItemTable(result))
case "json":
if ok := c.PrintJsonItem(result); !ok {
return base.CommandCliError
}
}
return base.CommandSuccess
}
var (
extraPluginActionsFlagsMapFunc = func() map[string][]string { return nil }
extraPluginSynopsisFunc = func(*PluginCommand) string { return "" }
extraPluginFlagsFunc = func(*PluginCommand, *base.FlagSets, *base.FlagSet) {}
extraPluginFlagsHandlingFunc = func(*PluginCommand, *base.FlagSets, *[]hostcatalogs.Option) bool { return true }
executeExtraPluginActions = func(_ *PluginCommand, inResult api.GenericResult, inErr error, _ *hostcatalogs.Client, _ uint32, _ []hostcatalogs.Option) (api.GenericResult, error) {
return inResult, inErr
}
printCustomPluginActionOutput = func(*PluginCommand) (bool, error) { return false, nil }
)
@@ -13,7 +13,7 @@ func (c *StaticCommand) extraStaticHelpFunc(helpMap map[string]func() string) st
"",
" Create a static-type host catalog. Example:",
"",
` $ boundary host-catalogs create static -name prodops -description "Static host-catalog for ProdOps"`,
` $ boundary host-catalogs create static -scope-id p_1234567890 -name prodops -description "Static host-catalog for ProdOps"`,
"",
"",
})