CLI Generation (#953)
This commit is contained in:
1 parent
282bbe619e
commit
341e7aff0d
77 files changed
+10303
-6861
No files matched your search
@@ -0,0 +1,255 @@
|
||||
package userscmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/users"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/mitchellh/go-wordwrap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
extraActionsFlagsMapFunc = extraActionsFlagsMapFuncImpl
|
||||
extraSynopsisFunc = extraSynopsisFuncImpl
|
||||
extraFlagsFunc = extraFlagsFuncImpl
|
||||
extraFlagsHandlingFunc = extraFlagsHandlingFuncImpl
|
||||
executeExtraActions = executeExtraActionsImpl
|
||||
}
|
||||
|
||||
type extraCmdVars struct {
|
||||
flagAccounts []string
|
||||
}
|
||||
|
||||
func extraActionsFlagsMapFuncImpl() map[string][]string {
|
||||
return map[string][]string{
|
||||
"add-accounts": {"id", "account", "version"},
|
||||
"set-accounts": {"id", "account", "version"},
|
||||
"remove-accounts": {"id", "account", "version"},
|
||||
}
|
||||
}
|
||||
|
||||
func extraSynopsisFuncImpl(c *Command) string {
|
||||
switch c.Func {
|
||||
case "add-accounts", "set-accounts", "remove-accounts":
|
||||
var in string
|
||||
switch {
|
||||
case strings.HasPrefix(c.Func, "add"):
|
||||
in = "Add accounts to"
|
||||
case strings.HasPrefix(c.Func, "set"):
|
||||
in = "Set the full contents of the accounts on"
|
||||
case strings.HasPrefix(c.Func, "remove"):
|
||||
in = "Remove accounts from"
|
||||
}
|
||||
return wordwrap.WrapString(fmt.Sprintf("%s a user within Boundary", in), base.TermWidth)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *Command) extraHelpFunc(helpMap map[string]func() string) string {
|
||||
var helpStr string
|
||||
switch c.Func {
|
||||
case "add-accounts":
|
||||
helpStr = base.WrapForHelpText([]string{
|
||||
"Usage: boundary users add-accounts [options] [args]",
|
||||
"",
|
||||
` Associates accounts to a user given its ID. The "account" flag can be specified multiple times. Example:`,
|
||||
"",
|
||||
` $ boundary users add-accounts -id u_1234567890 -account a_1234567890`,
|
||||
"",
|
||||
"",
|
||||
})
|
||||
|
||||
case "set-accounts":
|
||||
helpStr = base.WrapForHelpText([]string{
|
||||
"Usage: boundary users set-accounts [options] [args]",
|
||||
"",
|
||||
` Sets the complete set of associated accounts on a user given its ID. The "account" flag can be specified multiple times. Example:`,
|
||||
"",
|
||||
` $ boundary users set-principals -id u_1234567890 -account a_0987654321 -account a_1234567890`,
|
||||
"",
|
||||
"",
|
||||
})
|
||||
|
||||
case "remove-accounts":
|
||||
helpStr = base.WrapForHelpText([]string{
|
||||
"Usage: boundary users remove-accounts [options] [args]",
|
||||
"",
|
||||
` Disassociates accounts from a user given its ID. The "account" flag can be specified multiple times. Example:`,
|
||||
"",
|
||||
` $ boundary users remove-accounts -id u_1234567890 -account a_1234567890`,
|
||||
"",
|
||||
"",
|
||||
})
|
||||
|
||||
default:
|
||||
helpStr = helpMap["base"]()
|
||||
}
|
||||
return helpStr + c.Flags().Help()
|
||||
}
|
||||
|
||||
func extraFlagsFuncImpl(c *Command, _ *base.FlagSets, f *base.FlagSet) {
|
||||
for _, name := range flagsMap[c.Func] {
|
||||
switch name {
|
||||
case "account":
|
||||
f.StringSliceVar(&base.StringSliceVar{
|
||||
Name: "account",
|
||||
Target: &c.flagAccounts,
|
||||
Usage: "The accounts to add, remove, or set. May be specified multiple times.",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func extraFlagsHandlingFuncImpl(c *Command, opts *[]users.Option) int {
|
||||
switch c.Func {
|
||||
case "add-accounts", "remove-accounts":
|
||||
if len(c.flagAccounts) == 0 {
|
||||
c.UI.Error("No accounts supplied via -account")
|
||||
return 1
|
||||
}
|
||||
|
||||
case "set-accounts":
|
||||
switch len(c.flagAccounts) {
|
||||
case 0:
|
||||
c.UI.Error("No accounts supplied via -account")
|
||||
return 1
|
||||
case 1:
|
||||
if c.flagAccounts[0] == "null" {
|
||||
c.flagAccounts = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func executeExtraActionsImpl(c *Command, origResult api.GenericResult, origError error, userClient *users.Client, version uint32, opts []users.Option) (api.GenericResult, error) {
|
||||
switch c.Func {
|
||||
case "add-accounts":
|
||||
return userClient.AddAccounts(c.Context, c.FlagId, version, c.flagAccounts, opts...)
|
||||
case "set-accounts":
|
||||
return userClient.SetAccounts(c.Context, c.FlagId, version, c.flagAccounts, opts...)
|
||||
case "remove-accounts":
|
||||
return userClient.RemoveAccounts(c.Context, c.FlagId, version, c.flagAccounts, opts...)
|
||||
}
|
||||
return origResult, origError
|
||||
}
|
||||
|
||||
func (c *Command) printListTable(items []*users.User) string {
|
||||
if len(items) == 0 {
|
||||
return "No users found"
|
||||
}
|
||||
|
||||
var output []string
|
||||
output = []string{
|
||||
"",
|
||||
"User information:",
|
||||
}
|
||||
|
||||
for i, u := range items {
|
||||
if i > 0 {
|
||||
output = append(output, "")
|
||||
}
|
||||
if true {
|
||||
output = append(output,
|
||||
fmt.Sprintf(" ID: %s", u.Id),
|
||||
)
|
||||
}
|
||||
if c.FlagRecursive {
|
||||
output = append(output,
|
||||
fmt.Sprintf(" Scope ID: %s", u.Scope.Id),
|
||||
)
|
||||
}
|
||||
if true {
|
||||
output = append(output,
|
||||
fmt.Sprintf(" Version: %d", u.Version),
|
||||
)
|
||||
}
|
||||
if u.Name != "" {
|
||||
output = append(output,
|
||||
fmt.Sprintf(" Name: %s", u.Name),
|
||||
)
|
||||
}
|
||||
if u.Description != "" {
|
||||
output = append(output,
|
||||
fmt.Sprintf(" Description: %s", u.Description),
|
||||
)
|
||||
}
|
||||
if len(u.AuthorizedActions) > 0 {
|
||||
output = append(output,
|
||||
" Authorized Actions:",
|
||||
base.WrapSlice(6, u.AuthorizedActions),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return base.WrapForHelpText(output)
|
||||
}
|
||||
|
||||
func printItemTable(in *users.User) string {
|
||||
nonAttributeMap := map[string]interface{}{
|
||||
"ID": in.Id,
|
||||
"Version": in.Version,
|
||||
"Created Time": in.CreatedTime.Local().Format(time.RFC1123),
|
||||
"Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
|
||||
}
|
||||
|
||||
if in.Name != "" {
|
||||
nonAttributeMap["Name"] = in.Name
|
||||
}
|
||||
if in.Description != "" {
|
||||
nonAttributeMap["Description"] = in.Description
|
||||
}
|
||||
|
||||
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
|
||||
|
||||
var userMaps []map[string]interface{}
|
||||
if len(in.Accounts) > 0 {
|
||||
for _, account := range in.Accounts {
|
||||
a := map[string]interface{}{
|
||||
"ID": account.Id,
|
||||
"Scope ID": account.ScopeId,
|
||||
}
|
||||
userMaps = append(userMaps, a)
|
||||
}
|
||||
if l := len("Scope ID"); l > maxLength {
|
||||
maxLength = l
|
||||
}
|
||||
}
|
||||
|
||||
ret := []string{
|
||||
"",
|
||||
"User information:",
|
||||
base.WrapMap(2, maxLength+2, nonAttributeMap),
|
||||
"",
|
||||
" Scope:",
|
||||
base.ScopeInfoForOutput(in.Scope, maxLength),
|
||||
}
|
||||
|
||||
if len(in.AuthorizedActions) > 0 {
|
||||
ret = append(ret,
|
||||
"",
|
||||
" Authorized Actions:",
|
||||
base.WrapSlice(4, in.AuthorizedActions),
|
||||
)
|
||||
}
|
||||
|
||||
if len(in.Accounts) > 0 {
|
||||
ret = append(ret,
|
||||
"",
|
||||
" Accounts:",
|
||||
)
|
||||
for _, m := range userMaps {
|
||||
ret = append(ret,
|
||||
base.WrapMap(4, maxLength, m),
|
||||
"",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return base.WrapForHelpText(ret)
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package userscmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/users"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/common"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
func initFlags() {
|
||||
flagsOnce.Do(func() {
|
||||
extraFlags := extraActionsFlagsMapFunc()
|
||||
for k, v := range extraFlags {
|
||||
flagsMap[k] = append(flagsMap[k], v...)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
_ cli.Command = (*Command)(nil)
|
||||
_ cli.CommandAutocomplete = (*Command)(nil)
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
*base.Command
|
||||
|
||||
Func string
|
||||
|
||||
plural string
|
||||
|
||||
extraCmdVars
|
||||
}
|
||||
|
||||
func (c *Command) AutocompleteArgs() complete.Predictor {
|
||||
initFlags()
|
||||
return complete.PredictAnything
|
||||
}
|
||||
|
||||
func (c *Command) AutocompleteFlags() complete.Flags {
|
||||
initFlags()
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *Command) Synopsis() string {
|
||||
if extra := extraSynopsisFunc(c); extra != "" {
|
||||
return extra
|
||||
}
|
||||
|
||||
synopsisStr := "user"
|
||||
|
||||
return common.SynopsisFunc(c.Func, synopsisStr)
|
||||
}
|
||||
|
||||
func (c *Command) Help() string {
|
||||
initFlags()
|
||||
|
||||
var helpStr string
|
||||
helpMap := common.HelpMap("user")
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
||||
|
||||
case "read":
|
||||
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
||||
|
||||
case "update":
|
||||
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
||||
|
||||
case "delete":
|
||||
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
||||
|
||||
case "list":
|
||||
helpStr = helpMap[c.Func]() + c.Flags().Help()
|
||||
|
||||
default:
|
||||
|
||||
helpStr = c.extraHelpFunc(helpMap)
|
||||
|
||||
}
|
||||
|
||||
// Keep linter from complaining if we don't actually generate code using it
|
||||
_ = helpMap
|
||||
return helpStr
|
||||
}
|
||||
|
||||
var flagsMap = map[string][]string{
|
||||
|
||||
"create": {"scope-id", "name", "description"},
|
||||
|
||||
"read": {"id"},
|
||||
|
||||
"update": {"id", "name", "description", "version"},
|
||||
|
||||
"delete": {"id"},
|
||||
|
||||
"list": {"scope-id", "recursive"},
|
||||
}
|
||||
|
||||
func (c *Command) Flags() *base.FlagSets {
|
||||
if len(flagsMap[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, "user", flagsMap[c.Func])
|
||||
|
||||
extraFlagsFunc(c, set, f)
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (c *Command) Run(args []string) int {
|
||||
initFlags()
|
||||
|
||||
switch c.Func {
|
||||
case "":
|
||||
return cli.RunResultHelp
|
||||
}
|
||||
|
||||
c.plural = "user"
|
||||
switch c.Func {
|
||||
case "list":
|
||||
c.plural = "users"
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
|
||||
c.UI.Error("ID is required but not passed in via -id")
|
||||
return 1
|
||||
}
|
||||
|
||||
var opts []users.Option
|
||||
|
||||
if strutil.StrListContains(flagsMap[c.Func], "scope-id") {
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
if c.FlagScopeId == "" {
|
||||
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
|
||||
return 1
|
||||
}
|
||||
|
||||
case "list":
|
||||
if c.FlagScopeId == "" {
|
||||
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
|
||||
return 1
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
|
||||
return 2
|
||||
}
|
||||
usersClient := users.NewClient(client)
|
||||
|
||||
switch c.FlagName {
|
||||
case "":
|
||||
case "null":
|
||||
opts = append(opts, users.DefaultName())
|
||||
default:
|
||||
opts = append(opts, users.WithName(c.FlagName))
|
||||
}
|
||||
|
||||
switch c.FlagDescription {
|
||||
case "":
|
||||
case "null":
|
||||
opts = append(opts, users.DefaultDescription())
|
||||
default:
|
||||
opts = append(opts, users.WithDescription(c.FlagDescription))
|
||||
}
|
||||
|
||||
switch c.FlagRecursive {
|
||||
case true:
|
||||
opts = append(opts, users.WithRecursive(true))
|
||||
}
|
||||
|
||||
var version uint32
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "update":
|
||||
switch c.FlagVersion {
|
||||
case 0:
|
||||
opts = append(opts, users.WithAutomaticVersioning(true))
|
||||
default:
|
||||
version = uint32(c.FlagVersion)
|
||||
}
|
||||
|
||||
case "add-accounts":
|
||||
switch c.FlagVersion {
|
||||
case 0:
|
||||
opts = append(opts, users.WithAutomaticVersioning(true))
|
||||
default:
|
||||
version = uint32(c.FlagVersion)
|
||||
}
|
||||
|
||||
case "remove-accounts":
|
||||
switch c.FlagVersion {
|
||||
case 0:
|
||||
opts = append(opts, users.WithAutomaticVersioning(true))
|
||||
default:
|
||||
version = uint32(c.FlagVersion)
|
||||
}
|
||||
|
||||
case "set-accounts":
|
||||
switch c.FlagVersion {
|
||||
case 0:
|
||||
opts = append(opts, users.WithAutomaticVersioning(true))
|
||||
default:
|
||||
version = uint32(c.FlagVersion)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ret := extraFlagsHandlingFunc(c, &opts); ret != 0 {
|
||||
return ret
|
||||
}
|
||||
|
||||
existed := true
|
||||
|
||||
var result api.GenericResult
|
||||
|
||||
var listResult api.GenericListResult
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "create":
|
||||
result, err = usersClient.Create(c.Context, c.FlagScopeId, opts...)
|
||||
|
||||
case "read":
|
||||
result, err = usersClient.Read(c.Context, c.FlagId, opts...)
|
||||
|
||||
case "update":
|
||||
result, err = usersClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
|
||||
case "delete":
|
||||
_, err = usersClient.Delete(c.Context, c.FlagId, opts...)
|
||||
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
|
||||
existed = false
|
||||
err = nil
|
||||
}
|
||||
|
||||
case "list":
|
||||
listResult, err = usersClient.List(c.Context, c.FlagScopeId, opts...)
|
||||
|
||||
}
|
||||
|
||||
result, err = executeExtraActions(c, result, err, usersClient, version, opts)
|
||||
|
||||
if err != nil {
|
||||
if apiErr := api.AsServerError(err); apiErr != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, c.plural, base.PrintApiError(apiErr)))
|
||||
return 1
|
||||
}
|
||||
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
output, err := printCustomActionOutput(c)
|
||||
if err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
if output {
|
||||
return 0
|
||||
}
|
||||
|
||||
switch c.Func {
|
||||
|
||||
case "delete":
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
c.UI.Output(fmt.Sprintf("{ \"existed\": %t }", existed))
|
||||
|
||||
case "table":
|
||||
output := "The delete operation completed successfully"
|
||||
switch existed {
|
||||
case true:
|
||||
output += "."
|
||||
default:
|
||||
output += ", however the resource did not exist at the time."
|
||||
}
|
||||
c.UI.Output(output)
|
||||
}
|
||||
|
||||
return 0
|
||||
|
||||
case "list":
|
||||
listedItems := listResult.GetItems().([]*users.User)
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
switch {
|
||||
|
||||
case len(listedItems) == 0:
|
||||
c.UI.Output("null")
|
||||
|
||||
default:
|
||||
b, err := base.JsonFormatter{}.Format(listedItems)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
c.UI.Output(string(b))
|
||||
}
|
||||
|
||||
case "table":
|
||||
c.UI.Output(c.printListTable(listedItems))
|
||||
}
|
||||
|
||||
return 0
|
||||
|
||||
}
|
||||
|
||||
item := result.GetItem().(*users.User)
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(printItemTable(item))
|
||||
|
||||
case "json":
|
||||
b, err := base.JsonFormatter{}.Format(item)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
c.UI.Output(string(b))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
var (
|
||||
flagsOnce = new(sync.Once)
|
||||
|
||||
extraActionsFlagsMapFunc = func() map[string][]string { return nil }
|
||||
extraSynopsisFunc = func(*Command) string { return "" }
|
||||
extraFlagsFunc = func(*Command, *base.FlagSets, *base.FlagSet) {}
|
||||
extraFlagsHandlingFunc = func(*Command, *[]users.Option) int { return 0 }
|
||||
executeExtraActions = func(_ *Command, inResult api.GenericResult, inErr error, _ *users.Client, _ uint32, _ []users.Option) (api.GenericResult, error) {
|
||||
return inResult, inErr
|
||||
}
|
||||
printCustomActionOutput = func(*Command) (bool, error) { return false, nil }
|
||||
)
|
||||
Reference in new issue
Block a user