CLI JSON updates (#962)

See Changelog for details.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-02-25 12:43:17 -05:00
1 parent b967ff7f05
commit 72aa110f72
50 files changed
+489 -540

No files matched your search

+120 -44
View File
@@ -134,58 +134,134 @@ func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]interface{})
return strings.Join(ret, "\n")
}
func PrintApiError(in *api.Error) string {
nonAttributeMap := map[string]interface{}{
"Status": in.ResponseStatus(),
"Kind": in.Kind,
"Message": in.Message,
}
// PrintApiError prints the given API error, optionally with context
// information, to the UI in the appropriate format
func (c *Command) PrintApiError(in *api.Error, contextStr string) {
switch Format(c.UI) {
case "json":
output := struct {
Context string `json:"context,omitempty"`
Status int `json:"status"`
ApiError json.RawMessage `json:"api_error"`
}{
Context: contextStr,
Status: in.Response().StatusCode(),
ApiError: in.Response().Body.Bytes(),
}
b, _ := JsonFormatter{}.Format(output)
c.UI.Error(string(b))
if in.Op != "" {
nonAttributeMap["Operation"] = in.Op
}
maxLength := MaxAttributesLength(nonAttributeMap, nil, nil)
ret := []string{
"",
"Error information:",
WrapMap(2, maxLength+2, nonAttributeMap),
}
if in.Details != nil {
if len(in.Details.WrappedErrors) > 0 {
ret = append(ret,
"",
" Wrapped Errors:",
)
for _, we := range in.Details.WrappedErrors {
ret = append(ret,
fmt.Sprintf(" Message: %s", we.Message),
fmt.Sprintf(" Operation: %s", we.Op),
)
}
default:
nonAttributeMap := map[string]interface{}{
"Status": in.Response().StatusCode(),
"Kind": in.Kind,
"Message": in.Message,
}
if contextStr != "" {
nonAttributeMap["context"] = contextStr
}
if in.Op != "" {
nonAttributeMap["Operation"] = in.Op
}
if len(in.Details.RequestFields) > 0 {
ret = append(ret,
"",
" Field-specific Errors:",
)
for _, field := range in.Details.RequestFields {
if field.Name == "update_mask" {
// TODO: Report useful error messages related to "update_mask".
continue
maxLength := MaxAttributesLength(nonAttributeMap, nil, nil)
var output []string
if contextStr != "" {
output = append(output, contextStr)
}
output = append(output,
"",
"Error information:",
WrapMap(2, maxLength+2, nonAttributeMap),
)
if in.Details != nil {
if len(in.Details.WrappedErrors) > 0 {
output = append(output,
"",
" Wrapped Errors:",
)
for _, we := range in.Details.WrappedErrors {
output = append(output,
fmt.Sprintf(" Message: %s", we.Message),
fmt.Sprintf(" Operation: %s", we.Op),
)
}
ret = append(ret,
fmt.Sprintf(" Name: -%s", strings.ReplaceAll(field.Name, "_", "-")),
fmt.Sprintf(" Error: %s", field.Description),
}
if len(in.Details.RequestFields) > 0 {
output = append(output,
"",
" Field-specific Errors:",
)
for _, field := range in.Details.RequestFields {
if field.Name == "update_mask" {
// TODO: Report useful error messages related to "update_mask".
continue
}
output = append(output,
fmt.Sprintf(" Name: -%s", strings.ReplaceAll(field.Name, "_", "-")),
fmt.Sprintf(" Error: %s", field.Description),
)
}
}
}
}
return WrapForHelpText(ret)
c.UI.Error(WrapForHelpText(output))
}
}
// PrintCliError prints the given CLI error to the UI in the appropriate format
func (c *Command) PrintCliError(err error) {
switch Format(c.UI) {
case "table":
c.UI.Error(err.Error())
case "json":
output := struct {
Error string `json:"error"`
}{
Error: err.Error(),
}
b, _ := JsonFormatter{}.Format(output)
c.UI.Error(string(b))
}
}
// PrintJsonItem prints the given item to the UI in JSON format
func (c *Command) PrintJsonItem(result api.GenericResult, item interface{}) int {
output := struct {
StatusCode int `json:"status_code"`
Item interface{} `json:"item"`
}{
StatusCode: result.GetResponse().HttpResponse().StatusCode,
Item: item,
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
c.PrintCliError(err)
return 1
}
c.UI.Output(string(b))
return 0
}
// PrintJsonItems prints the given items to the UI in JSON format
func (c *Command) PrintJsonItems(result api.GenericListResult, items []interface{}) int {
output := struct {
StatusCode int `json:"status_code"`
Items []interface{} `json:"items"`
}{
StatusCode: result.GetResponse().HttpResponse().StatusCode,
Items: items,
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
c.PrintCliError(err)
return 1
}
c.UI.Output(string(b))
return 0
}
// An output formatter for json output of an object
@@ -2,6 +2,7 @@
package accountscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -131,12 +132,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -146,7 +147,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagAuthMethodId == "" {
c.UI.Error("AuthMethod ID must be passed in via -auth-method-id or BOUNDARY_AUTH_METHOD_ID")
c.PrintCliError(errors.New("AuthMethod ID must be passed in via -auth-method-id or BOUNDARY_AUTH_METHOD_ID"))
return 1
}
}
@@ -154,7 +155,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
accountsClient := accounts.NewClient(client)
@@ -214,7 +215,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = accountsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -228,16 +229,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -274,12 +275,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -296,12 +296,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package accountscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
@@ -114,12 +115,12 @@ func (c *PasswordCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsPasswordMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -129,7 +130,7 @@ func (c *PasswordCommand) Run(args []string) int {
switch c.Func {
case "create":
if c.FlagAuthMethodId == "" {
c.UI.Error("AuthMethod ID must be passed in via -auth-method-id or BOUNDARY_AUTH_METHOD_ID")
c.PrintCliError(errors.New("AuthMethod ID must be passed in via -auth-method-id or BOUNDARY_AUTH_METHOD_ID"))
return 1
}
}
@@ -137,7 +138,7 @@ func (c *PasswordCommand) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
accountsClient := accounts.NewClient(client)
@@ -190,16 +191,16 @@ func (c *PasswordCommand) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustomPasswordActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -215,12 +216,7 @@ func (c *PasswordCommand) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+8 -12
View File
@@ -3,6 +3,7 @@ package authenticate
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
@@ -94,16 +95,16 @@ func (c *PasswordCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
switch {
case c.flagLoginName == "":
c.UI.Error("Login name must be provided via -login-name")
c.PrintCliError(errors.New("Login name must be provided via -login-name"))
return 1
case c.FlagAuthMethodId == "":
c.UI.Error("Auth method ID must be provided via -auth-method-id")
c.PrintCliError(errors.New("Auth method ID must be provided via -auth-method-id"))
return 1
}
@@ -120,7 +121,7 @@ func (c *PasswordCommand) Run(args []string) int {
client, err := c.Client(base.WithNoTokenScope(), base.WithNoTokenValue())
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %w", err))
return 2
}
@@ -134,10 +135,10 @@ func (c *PasswordCommand) Run(args []string) int {
})
if err != nil {
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authentication: %s", base.PrintApiError(apiErr)))
c.PrintApiError(apiErr, "Error from controller when performing authentication")
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to perform authentication: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to perform authentication: %w", err))
return 2
}
@@ -155,12 +156,7 @@ func (c *PasswordCommand) Run(args []string) int {
}))
case "json":
jsonOut, err := base.JsonFormatter{}.Format(token)
if err != nil {
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
return 1
}
c.UI.Output(string(jsonOut))
return c.PrintJsonItem(result, token)
}
var gotErr bool
@@ -2,6 +2,7 @@
package authmethodscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -129,12 +130,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -144,7 +145,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -152,7 +153,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
authmethodsClient := authmethods.NewClient(client)
@@ -181,7 +182,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = authmethodsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -195,16 +196,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -241,12 +242,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -263,12 +263,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package authmethodscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
@@ -114,12 +115,12 @@ func (c *PasswordCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsPasswordMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -129,7 +130,7 @@ func (c *PasswordCommand) Run(args []string) int {
switch c.Func {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -137,7 +138,7 @@ func (c *PasswordCommand) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
authmethodsClient := authmethods.NewClient(client)
@@ -195,16 +196,16 @@ func (c *PasswordCommand) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustomPasswordActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -220,12 +221,7 @@ func (c *PasswordCommand) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package authtokenscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -125,12 +126,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -140,7 +141,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -148,7 +149,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
authtokensClient := authtokens.NewClient(client)
@@ -177,7 +178,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = authtokensClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -191,16 +192,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -237,12 +238,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -259,12 +259,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+32 -55
View File
@@ -208,15 +208,6 @@ func (c *Command) Flags() *base.FlagSets {
Usage: "Target scope name, if authorizing the session via scope parameters and target name. Mutually exclusive with -scope-id.",
})
f.BoolVar(&base.BoolVar{
Name: "output-json-errors",
Target: &c.outputJsonErrors,
EnvVar: "BOUNDARY_CONNECT_OUTPUT_JSON_ERRORS",
Completion: complete.PredictNothing,
Usage: "Cause errors coming from this command to be output as JSON. This is experimental only and currently only meant for internal purposes. The format may change at any time and this flag/env var may be removed or modified at any time.",
Hidden: true,
})
switch c.Func {
case "connect":
f.StringVar(&base.StringVar{
@@ -274,7 +265,7 @@ func (c *Command) Run(args []string) (retCode int) {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.Error(err.Error())
c.PrintCliError(err)
return 3
}
@@ -282,22 +273,22 @@ func (c *Command) Run(args []string) (retCode int) {
case c.flagAuthzToken != "":
switch {
case c.flagTargetId != "":
c.Error(`-target-id and -authz-token cannot both be specified`)
c.PrintCliError(errors.New(`-target-id and -authz-token cannot both be specified`))
return 3
case c.flagTargetName != "":
c.Error(`-target-name and -authz-token cannot both be specified`)
c.PrintCliError(errors.New(`-target-name and -authz-token cannot both be specified`))
return 3
}
default:
if c.flagTargetId == "" &&
(c.flagTargetName == "" ||
(c.FlagScopeId == "" && c.FlagScopeName == "")) {
c.Error("Target ID was not passed in, but no combination of target name and scope ID/name was passed in either")
c.PrintCliError(errors.New("Target ID was not passed in, but no combination of target name and scope ID/name was passed in either"))
return 3
}
if c.flagTargetId != "" &&
(c.flagTargetName != "" || c.FlagScopeId != "" || c.FlagScopeName != "") {
c.Error("Cannot specify a target ID and also other lookup parameters")
c.PrintCliError(errors.New("Cannot specify a target ID and also other lookup parameters"))
return 3
}
}
@@ -319,7 +310,7 @@ func (c *Command) Run(args []string) (retCode int) {
tofuToken, err := base62.Random(20)
if err != nil {
c.Error(fmt.Errorf("Could not derive random bytes for tofu token: %w", err).Error())
c.PrintCliError(fmt.Errorf("Could not derive random bytes for tofu token: %w", err))
return 2
}
@@ -330,7 +321,7 @@ func (c *Command) Run(args []string) (retCode int) {
}
listenAddr := net.ParseIP(c.flagListenAddr)
if listenAddr == nil {
c.Error(fmt.Sprintf("Could not successfully parse listen address of %s", c.flagListenAddr))
c.PrintCliError(fmt.Errorf("Could not successfully parse listen address of %s", c.flagListenAddr))
return 3
}
@@ -340,18 +331,18 @@ func (c *Command) Run(args []string) (retCode int) {
if authzString == "-" {
authBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
c.Error(fmt.Errorf("No authorization string was provided and encountered the following error attempting to read it from stdin: %w", err).Error())
c.PrintCliError(fmt.Errorf("No authorization string was provided and encountered the following error attempting to read it from stdin: %w", err))
return 3
}
if len(authBytes) == 0 {
c.Error("No authorization data read from stdin")
c.PrintCliError(errors.New("No authorization data read from stdin"))
return 3
}
authzString = string(authBytes)
}
if authzString == "" {
c.Error("Authorization data was empty")
c.PrintCliError(errors.New("Authorization data was empty"))
return 3
}
@@ -367,7 +358,7 @@ func (c *Command) Run(args []string) (retCode int) {
default:
client, err := c.Client()
if err != nil {
c.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err))
return 2
}
targetClient := targets.NewClient(client)
@@ -389,15 +380,10 @@ func (c *Command) Run(args []string) (retCode int) {
sar, err := targetClient.AuthorizeSession(c.Context, c.flagTargetId, opts...)
if err != nil {
if apiErr := api.AsServerError(err); apiErr != nil {
switch c.outputJsonErrors {
case true:
c.Error(apiErr.ResponseBody().String())
default:
c.Error(fmt.Sprintf("Error from controller when performing authorize-session against target: %s", base.PrintApiError(apiErr)))
}
c.PrintApiError(apiErr, "Error from controller when performing authorize-session action against given target")
return 1
}
c.Error(fmt.Sprintf("Error trying to authorize a session against target: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to authorize a session against target: %w", err))
return 2
}
authzString = sar.GetItem().(*targets.SessionAuthorization).AuthorizationToken
@@ -405,22 +391,22 @@ func (c *Command) Run(args []string) (retCode int) {
marshaled, err := base58.FastBase58Decoding(authzString)
if err != nil {
c.Error(fmt.Errorf("Unable to base58-decode authorization data: %w", err).Error())
c.PrintCliError(fmt.Errorf("Unable to base58-decode authorization data: %w", err))
return 3
}
if len(marshaled) == 0 {
c.Error("Zero length authorization information after decoding")
c.PrintCliError(errors.New("Zero length authorization information after decoding"))
return 3
}
c.sessionAuthzData = new(targetspb.SessionAuthorizationData)
if err := proto.Unmarshal(marshaled, c.sessionAuthzData); err != nil {
c.Error(fmt.Errorf("Unable to proto-decode authorization data: %w", err).Error())
c.PrintCliError(fmt.Errorf("Unable to proto-decode authorization data: %w", err))
return 3
}
if len(c.sessionAuthzData.GetWorkerInfo()) == 0 {
c.Error("No workers found in authorization string")
c.PrintCliError(errors.New("No workers found in authorization string"))
return 3
}
@@ -429,12 +415,12 @@ func (c *Command) Run(args []string) (retCode int) {
parsedCert, err := x509.ParseCertificate(c.sessionAuthzData.Certificate)
if err != nil {
c.Error(fmt.Errorf("Unable to decode mTLS certificate: %w", err).Error())
c.PrintCliError(fmt.Errorf("Unable to decode mTLS certificate: %w", err))
return 3
}
if len(parsedCert.DNSNames) != 1 {
c.Error(fmt.Errorf("mTLS certificate has invalid parameters: %w", err).Error())
c.PrintCliError(fmt.Errorf("mTLS certificate has invalid parameters: %w", err))
return 3
}
@@ -474,7 +460,7 @@ func (c *Command) Run(args []string) (retCode int) {
Port: c.flagListenPort,
})
if err != nil {
c.Error(fmt.Errorf("Error starting listening port: %w", err).Error())
c.PrintCliError(fmt.Errorf("Error starting listening port: %w", err))
return 2
}
@@ -482,7 +468,7 @@ func (c *Command) Run(args []string) (retCode int) {
// Forces the for loop to exist instead of spinning on errors
c.connectionsLeft.Store(0)
if err := c.listener.Close(); err != nil {
c.Error(fmt.Errorf("Error closing listener on shutdown: %w", err).Error())
c.PrintCliError(fmt.Errorf("Error closing listener on shutdown: %w", err))
retCode = 2
}
}
@@ -510,7 +496,7 @@ func (c *Command) Run(args []string) (retCode int) {
case "json":
out, err := json.Marshal(&sessInfo)
if err != nil {
c.Error(fmt.Errorf("error marshaling session information: %w", err).Error())
c.PrintCliError(fmt.Errorf("error marshaling session information: %w", err))
return 2
}
c.UI.Output(string(out))
@@ -536,7 +522,7 @@ func (c *Command) Run(args []string) (retCode int) {
if c.connectionsLeft.Load() == 0 {
return
}
c.Error(fmt.Errorf("Error accepting connection: %w", err).Error())
c.PrintCliError(fmt.Errorf("Error accepting connection: %w", err))
continue
}
}
@@ -549,10 +535,10 @@ func (c *Command) Run(args []string) (retCode int) {
workerAddr,
transport)
if err != nil {
c.Error(err.Error())
c.PrintCliError(err)
} else {
if err := c.runTcpProxyV1(wsConn, listeningConn, tofuToken); err != nil {
c.Error(err.Error())
c.PrintCliError(err)
}
}
}()
@@ -620,10 +606,10 @@ func (c *Command) Run(args []string) (retCode int) {
ctx, cancel := context.WithTimeout(context.Background(), sessionCancelTimeout)
wsConn, err := c.getWsConn(ctx, workerAddr, transport)
if err != nil {
c.Error(fmt.Errorf("error fetching connection to send session teardown request to worker: %w", err).Error())
c.PrintCliError(fmt.Errorf("error fetching connection to send session teardown request to worker: %w", err))
} else {
if err := c.sendSessionTeardown(ctx, wsConn, tofuToken); err != nil {
c.Error(fmt.Errorf("error sending session teardown request to worker: %w", err).Error())
c.PrintCliError(fmt.Errorf("error sending session teardown request to worker: %w", err))
}
}
cancel()
@@ -636,7 +622,7 @@ func (c *Command) Run(args []string) (retCode int) {
case "json":
out, err := json.Marshal(&termInfo)
if err != nil {
c.Error(fmt.Errorf("error marshaling termination information: %w", err).Error())
c.PrintCliError(fmt.Errorf("error marshaling termination information: %w", err))
return 2
}
c.UI.Output(string(out))
@@ -767,7 +753,7 @@ func (c *Command) updateConnsLeft(connsLeft int32) {
case "json":
out, err := json.Marshal(&connInfo)
if err != nil {
c.Error(fmt.Errorf("error marshaling connection information: %w", err).Error())
c.PrintCliError(fmt.Errorf("error marshaling connection information: %w", err))
}
c.UI.Output(string(out))
}
@@ -788,7 +774,7 @@ func (c *Command) handleExec(passthroughArgs []string) {
case "http":
httpArgs, err := c.httpFlags.buildArgs(c, port, ip, addr)
if err != nil {
c.Error(fmt.Sprintf("Error parsing session args: %s", err))
c.PrintCliError(fmt.Errorf("Error parsing session args: %w", err))
c.execCmdReturnValue.Store(int32(3))
return
}
@@ -806,7 +792,7 @@ func (c *Command) handleExec(passthroughArgs []string) {
case "kube":
kubeArgs, err := c.kubeFlags.buildArgs(c, port, ip, addr)
if err != nil {
c.Error(fmt.Sprintf("Error parsing session args: %s", err))
c.PrintCliError(fmt.Errorf("Error parsing session args: %w", err))
c.execCmdReturnValue.Store(int32(3))
return
}
@@ -860,18 +846,9 @@ func (c *Command) handleExec(passthroughArgs []string) {
}
}
c.Error(fmt.Sprintf("Failed to run command: %s", err))
c.PrintCliError(fmt.Errorf("Failed to run command: %w", err))
c.execCmdReturnValue.Store(int32(exitCode))
return
}
c.execCmdReturnValue.Store(0)
}
func (c *Command) Error(err string) {
switch c.outputJsonErrors {
case true:
c.UI.Error(fmt.Sprintf(`{"error": %q}`, err))
default:
c.UI.Error(err)
}
}
+15 -20
View File
@@ -2,6 +2,7 @@
package groupscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -137,12 +138,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -153,13 +154,13 @@ func (c *Command) Run(args []string) int {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("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")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
@@ -168,7 +169,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
groupsClient := groups.NewClient(client)
@@ -255,7 +256,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = groupsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -269,16 +270,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -315,12 +316,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -337,12 +337,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package hostcatalogscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -129,12 +130,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -144,7 +145,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -152,7 +153,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
hostcatalogsClient := hostcatalogs.NewClient(client)
@@ -181,7 +182,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = hostcatalogsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -195,16 +196,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -241,12 +242,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -263,12 +263,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package hostcatalogscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
@@ -112,12 +113,12 @@ func (c *StaticCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsStaticMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -127,7 +128,7 @@ func (c *StaticCommand) Run(args []string) int {
switch c.Func {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -135,7 +136,7 @@ func (c *StaticCommand) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
hostcatalogsClient := hostcatalogs.NewClient(client)
@@ -193,16 +194,16 @@ func (c *StaticCommand) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustomStaticActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -218,12 +219,7 @@ func (c *StaticCommand) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+14 -19
View File
@@ -2,6 +2,7 @@
package hostscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -129,12 +130,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -144,7 +145,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagHostCatalogId == "" {
c.UI.Error("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID")
c.PrintCliError(errors.New("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID"))
return 1
}
}
@@ -152,7 +153,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
hostsClient := hosts.NewClient(client)
@@ -192,7 +193,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = hostsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -206,16 +207,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -252,12 +253,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -274,12 +274,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package hostscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
@@ -114,12 +115,12 @@ func (c *StaticCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsStaticMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -129,7 +130,7 @@ func (c *StaticCommand) Run(args []string) int {
switch c.Func {
case "create":
if c.FlagHostCatalogId == "" {
c.UI.Error("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID")
c.PrintCliError(errors.New("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID"))
return 1
}
}
@@ -137,7 +138,7 @@ func (c *StaticCommand) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
hostsClient := hosts.NewClient(client)
@@ -190,16 +191,16 @@ func (c *StaticCommand) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustomStaticActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -215,12 +216,7 @@ func (c *StaticCommand) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package hostsetscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -131,12 +132,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -146,7 +147,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagHostCatalogId == "" {
c.UI.Error("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID")
c.PrintCliError(errors.New("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID"))
return 1
}
}
@@ -154,7 +155,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
hostsetsClient := hostsets.NewClient(client)
@@ -222,7 +223,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = hostsetsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -236,16 +237,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -282,12 +283,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -304,12 +304,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package hostsetscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
@@ -112,12 +113,12 @@ func (c *StaticCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsStaticMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -127,7 +128,7 @@ func (c *StaticCommand) Run(args []string) int {
switch c.Func {
case "create":
if c.FlagHostCatalogId == "" {
c.UI.Error("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID")
c.PrintCliError(errors.New("HostCatalog ID must be passed in via -host-catalog-id or BOUNDARY_HOST_CATALOG_ID"))
return 1
}
}
@@ -135,7 +136,7 @@ func (c *StaticCommand) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
hostsetsClient := hostsets.NewClient(client)
@@ -188,16 +189,16 @@ func (c *StaticCommand) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustomStaticActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -213,12 +214,7 @@ func (c *StaticCommand) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+15 -20
View File
@@ -2,6 +2,7 @@
package rolescmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -137,12 +138,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -153,13 +154,13 @@ func (c *Command) Run(args []string) int {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("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")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
@@ -168,7 +169,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
rolesClient := roles.NewClient(client)
@@ -279,7 +280,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = rolesClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -293,16 +294,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -339,12 +340,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -361,12 +361,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+15 -20
View File
@@ -2,6 +2,7 @@
package scopescmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -137,12 +138,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -153,13 +154,13 @@ func (c *Command) Run(args []string) int {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("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")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
@@ -168,7 +169,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
scopesClient := scopes.NewClient(client)
@@ -229,7 +230,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = scopesClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -243,16 +244,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -289,12 +290,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -311,12 +311,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package sessionscmd
import (
"errors"
"fmt"
"sync"
@@ -119,12 +120,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -134,7 +135,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -142,7 +143,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
sessionsClient := sessions.NewClient(client)
@@ -186,16 +187,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -213,12 +214,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -234,12 +234,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+13 -18
View File
@@ -2,6 +2,7 @@
package targetscmd
import (
"errors"
"fmt"
"net/http"
"os"
@@ -137,7 +138,7 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
@@ -147,7 +148,7 @@ func (c *Command) Run(args []string) int {
switch c.Func {
case "list":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -155,7 +156,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
targetsClient := targets.NewClient(client)
@@ -228,7 +229,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = targetsClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -242,16 +243,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -288,12 +289,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -310,12 +310,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
@@ -2,6 +2,7 @@
package targetscmd
import (
"errors"
"fmt"
"github.com/hashicorp/boundary/api"
@@ -114,12 +115,12 @@ func (c *TcpCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsTcpMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -129,7 +130,7 @@ func (c *TcpCommand) Run(args []string) int {
switch c.Func {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
}
@@ -137,7 +138,7 @@ func (c *TcpCommand) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
targetsClient := targets.NewClient(client)
@@ -195,16 +196,16 @@ func (c *TcpCommand) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustomTcpActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -220,12 +221,7 @@ func (c *TcpCommand) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+15 -20
View File
@@ -2,6 +2,7 @@
package userscmd
import (
"errors"
"fmt"
"net/http"
"sync"
@@ -137,12 +138,12 @@ func (c *Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
@@ -153,13 +154,13 @@ func (c *Command) Run(args []string) int {
case "create":
if c.FlagScopeId == "" {
c.UI.Error("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID")
c.PrintCliError(errors.New("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")
c.PrintCliError(errors.New("Scope ID must be passed in via -scope-id or BOUNDARY_SCOPE_ID"))
return 1
}
@@ -168,7 +169,7 @@ func (c *Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
usersClient := users.NewClient(client)
@@ -255,7 +256,7 @@ func (c *Command) Run(args []string) int {
case "delete":
_, err = usersClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -269,16 +270,16 @@ func (c *Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("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())
c.PrintCliError(err)
return 1
}
if output {
@@ -315,12 +316,11 @@ func (c *Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -337,12 +337,7 @@ func (c *Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0
+15 -22
View File
@@ -81,16 +81,15 @@ var cmdTemplate = template.Must(template.New("").Funcs(
package {{ .Pkg }}cmd
import (
"errors"
"fmt"
"net/http"
"os"
"sync"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/{{ .Pkg }}"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/mitchellh/cli"
"github.com/posener/complete"
@@ -231,13 +230,13 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
{{ if .HasId }}
if strutil.StrListContains(flags{{ camelCase .SubActionPrefix }}Map[c.Func], "id") && c.FlagId == "" {
c.UI.Error("ID is required but not passed in via -id")
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return 1
}
{{ end }}
@@ -249,14 +248,14 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
{{ if hasAction .StdActions "create" }}
case "create":
if c.Flag{{ .Container }}Id == "" {
c.UI.Error("{{ .Container }} ID must be passed in via -{{ kebabCase .Container }}-id or BOUNDARY_{{ envCase .Container }}_ID")
c.PrintCliError(errors.New("{{ .Container }} ID must be passed in via -{{ kebabCase .Container }}-id or BOUNDARY_{{ envCase .Container }}_ID"))
return 1
}
{{ end }}
{{ if hasAction .StdActions "list" }}
case "list":
if c.Flag{{ .Container }}Id == "" {
c.UI.Error("{{ .Container }} ID must be passed in via -{{ kebabCase .Container }}-id or BOUNDARY_{{ envCase .Container }}_ID")
c.PrintCliError(errors.New("{{ .Container }} ID must be passed in via -{{ kebabCase .Container }}-id or BOUNDARY_{{ envCase .Container }}_ID"))
return 1
}
{{ end }}
@@ -265,7 +264,7 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
client, err := c.Client()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
c.PrintCliError(fmt.Errorf("Error creating API client: %s", err.Error()))
return 2
}
{{ .Pkg }}Client := {{ .Pkg }}.NewClient(client)
@@ -349,7 +348,7 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
{{ if eq $action "delete" }}
case "delete":
_, err = {{ $input.Pkg}}Client.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.ResponseStatus() == http.StatusNotFound {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
existed = false
err = nil
}
@@ -365,16 +364,16 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
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)))
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return 2
}
output, err := printCustom{{ camelCase .SubActionPrefix }}ActionOutput(c)
if err != nil {
c.UI.Error(err.Error())
c.PrintCliError(err)
return 1
}
if output {
@@ -413,12 +412,11 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
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
items := make([]interface{}, len(listedItems))
for i, v := range listedItems {
items[i] = v
}
c.UI.Output(string(b))
return c.PrintJsonItems(listResult, items)
}
case "table":
@@ -436,12 +434,7 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Run(args []string) int {
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 c.PrintJsonItem(result, item)
}
return 0