Skip to content

Commit

Permalink
fix: fix panic when too many statuses requested
Browse files Browse the repository at this point in the history
Closes #1.

Signed-off-by: Mark Cornick <mark@markcornick.com>
  • Loading branch information
mcornick committed Feb 18, 2024
1 parent 3adab5a commit 11ca899
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion cmd/crud_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ func Test_crudStatus(t *testing.T) {
}
statusID := createResult.Response.ID

_, err = listStatus(os.Getenv("CLILOL_ADDRESS"), 1)
listResult, err := listStatus(os.Getenv("CLILOL_ADDRESS"), 0)
if err != nil {
t.Errorf("listStatus() error = %v", err)
return
}
listResult2, err := listStatus(os.Getenv("CLILOL_ADDRESS"), len(listResult.Response.Statuses)+1)
if err != nil {
t.Errorf("listStatus() error = %v", err)
return
}
if len(listResult2.Response.Statuses) != len(listResult.Response.Statuses) {
t.Errorf("listStatus() = %v, wanted %v", len(listResult2.Response.Statuses), len(listResult.Response.Statuses))
}

getResult, err := getStatus(os.Getenv("CLILOL_ADDRESS"), statusID)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion cmd/list_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ The address can be specified with the --address flag. If not set,
it defaults to your own address.
The number of statuses returned can be specified with the --limit
flag. If not set, it will return all statuses for the user.
flag. If not set, it will return all statuses for the user. If
set to more statuses than exist, it will return all statuses.
See the statuslog commands to get statuses for all users.`,
Args: cobra.NoArgs,
Expand Down Expand Up @@ -98,6 +99,9 @@ func listStatus(address string, limit int) (listStatusOutput, error) {
false,
)
err := json.Unmarshal(body, &result)
if limit > len(result.Response.Statuses) {
limit = len(result.Response.Statuses)
}
if limit > 0 && err == nil {
result.Response.Statuses = result.Response.Statuses[:limit]
}
Expand Down
3 changes: 2 additions & 1 deletion docs/clilol_list_statuses.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The address can be specified with the --address flag. If not set,
it defaults to your own address.

The number of statuses returned can be specified with the --limit
flag. If not set, it will return all statuses for the user.
flag. If not set, it will return all statuses for the user. If
set to more statuses than exist, it will return all statuses.

See the statuslog commands to get statuses for all users.

Expand Down

0 comments on commit 11ca899

Please sign in to comment.