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

+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