Skip to content

Commit b1c2495

Browse files
committed
Review fixes: archive id var, JSON validation, locations whitespace
- archive.go: use validated 'id' variable instead of raw args[0] - client.go: validate response is JSON (guard against HTML 200s); use bytes.TrimSpace before first-byte check in Locations - root.go: revert os.Exit(2) (no-arg help path); drop unreachable UsageError that was printing spurious JSON to stderr
1 parent 9b70464 commit b1c2495

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

pkg/api/client.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -78,6 +79,12 @@ func (c *Client) doGet(endpoint string, params map[string]string) ([]byte, error
7879
}
7980
}
8081

82+
// Validate response is JSON (guard against HTML error pages with 200 status).
83+
trimmed := bytes.TrimSpace(body)
84+
if len(trimmed) == 0 || (trimmed[0] != '{' && trimmed[0] != '[') {
85+
return nil, &clierrors.ApiError{Message: "Unexpected non-JSON response from server"}
86+
}
87+
8188
return body, nil
8289
}
8390

@@ -133,7 +140,7 @@ func (c *Client) Locations(params map[string]string) (json.RawMessage, error) {
133140
return nil, err
134141
}
135142
// Locations returns a JSON array; check for an error object.
136-
if len(body) > 0 && body[0] == '{' {
143+
if trimmed := bytes.TrimSpace(body); len(trimmed) > 0 && trimmed[0] == '{' {
137144
if err := checkAPIError(body); err != nil {
138145
return nil, err
139146
}

pkg/cmd/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func runArchive(cmd *cobra.Command, args []string) error {
3636
sp := newSpinner("Fetching archive...")
3737
sp.Start()
3838
client := api.New(apiKey)
39-
result, err := client.Archive(args[0])
39+
result, err := client.Archive(id)
4040
sp.Stop()
4141
if err != nil {
4242
return err

pkg/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var rootCmd = &cobra.Command{
2929
SilenceUsage: true,
3030
SilenceErrors: true,
3131
RunE: func(cmd *cobra.Command, args []string) error {
32-
cmd.Help()
32+
_ = cmd.Help()
3333
os.Exit(2)
3434
return nil
3535
},

0 commit comments

Comments
 (0)