Fix panic in non-generated API calls when printing as JSON (#1095)

Fixes #992
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-04-09 13:09:51 -04:00
1 parent 26c0c4479d
commit f05f37d0fa
8 files changed
+21 -18

No files matched your search

+3
View File
@@ -47,6 +47,9 @@ Canonical reference for changes, improvements, and bugfixes for Boundary.
* server: Roles for auto generated scopes are now generated at database init.
[PR](https://github.com/hashicorp/boundary/pull/996)
* cli: Don't panic on certain commands when outputting in `json` format
([Issue](https://github.com/hashicorp/boundary/pull/992),
[PR](https://github.com/hashicorp/boundary/pull/1095))
## 0.1.8 (2021/03/10)
+1 -2
View File
@@ -57,10 +57,9 @@ func (c *Client) ChangePassword(ctx context.Context, accountId, currentPassword,
if err != nil {
return nil, fmt.Errorf("error decoding ChangePassword response: %w", err)
}
if apiErr != nil {
return nil, apiErr
}
target.response = resp
return target, nil
}
+1 -1
View File
@@ -59,6 +59,6 @@ func (c *Client) SetPassword(ctx context.Context, accountId, password string, ve
if apiErr != nil {
return nil, apiErr
}
target.response = resp
return target, nil
}
-1
View File
@@ -96,6 +96,5 @@ func (c *Client) Authenticate(ctx context.Context, authMethodId, command string,
return nil, apiErr
}
target.response = resp
return target, nil
}
+1 -2
View File
@@ -59,10 +59,9 @@ func (c *Client) ChangeState(ctx context.Context, authMethodId string, version u
if err != nil {
return nil, fmt.Errorf("error decoding ChangeState response: %w", err)
}
if apiErr != nil {
return nil, apiErr
}
target.response = resp
return target, nil
}
+5 -4
View File
@@ -63,14 +63,15 @@ func (c *Client) AuthorizeSession(ctx context.Context, targetId string, opt ...O
return nil, fmt.Errorf("error performing client request during AuthorizeSession call: %w", err)
}
sar := new(SessionAuthorizationResult)
sar.Item = new(SessionAuthorization)
apiErr, err := resp.Decode(sar.Item)
target := new(SessionAuthorizationResult)
target.Item = new(SessionAuthorization)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, fmt.Errorf("error decoding AuthorizeSession response: %w", err)
}
if apiErr != nil {
return nil, apiErr
}
return sar, nil
target.response = resp
return target, nil
}
+8 -4
View File
@@ -234,8 +234,10 @@ func (c *Command) PrintJsonItem(result api.GenericResult, item interface{}) bool
StatusCode int `json:"status_code"`
Item interface{} `json:"item"`
}{
StatusCode: result.GetResponse().HttpResponse().StatusCode,
Item: item,
Item: item,
}
if result.GetResponse() != nil && result.GetResponse().HttpResponse() != nil {
output.StatusCode = result.GetResponse().HttpResponse().StatusCode
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
@@ -252,8 +254,10 @@ func (c *Command) PrintJsonItems(result api.GenericListResult, items []interface
StatusCode int `json:"status_code"`
Items []interface{} `json:"items"`
}{
StatusCode: result.GetResponse().HttpResponse().StatusCode,
Items: items,
Items: items,
}
if result.GetResponse() != nil && result.GetResponse().HttpResponse() != nil {
output.StatusCode = result.GetResponse().HttpResponse().StatusCode
}
b, err := JsonFormatter{}.Format(output)
if err != nil {
+2 -4
View File
@@ -444,11 +444,9 @@ func printCustomActionOutputImpl(c *Command) (bool, error) {
return true, nil
case "json":
b, err := base.JsonFormatter{}.Format(item)
if err != nil {
return false, fmt.Errorf("Error formatting as JSON: %w", err)
if ok := c.PrintJsonItem(c.sar, item); !ok {
return false, fmt.Errorf("Error formatting as JSON")
}
c.UI.Output(string(b))
return true, nil
}
}