Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random-love: when maxxing out quota, continue sending love to others #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions briq/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

var (
ErrNotFound error = errors.New("not found")
ErrNotFound error = errors.New("not found")
ErrBadRequest = fmt.Errorf("%d %s", http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
)

// Client is a Briq API client
Expand Down Expand Up @@ -64,7 +65,10 @@ func (client *Client) do(ctx context.Context, method, targetUrl string, request,
if err != nil {
return err
}
if res.StatusCode < 200 || res.StatusCode >= 400 {
if res.StatusCode == 400 {
return ErrBadRequest
}
if res.StatusCode < 200 || res.StatusCode > 400 {
return errors.New(res.Status)
}
rdata, err := io.ReadAll(res.Body)
Expand Down
8 changes: 7 additions & 1 deletion cmdrandomlove.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"

"github.com/nlm/briq-cli/briq"
Expand Down Expand Up @@ -54,7 +55,7 @@ var RandomLoveCmd = cobra.Command{
// if a group is specified, only select users from this group
groupUsers := viper.GetStringSlice(fmt.Sprintf("groups.%s", argToGroup))
if len(groupUsers) == 0 {
cobra.CheckErr(fmt.Errorf("group not found or empty"))
cobra.CheckErr(errors.New("group not found or empty"))
}
targetUsers = utils.FilterSlice(res.Users, groupUsers, BriqUserKey)
}
Expand All @@ -67,6 +68,11 @@ var RandomLoveCmd = cobra.Command{
Comment: argMessage,
}
res, err := client.CreateTransaction(cmd.Context(), req)
if err == briq.ErrBadRequest {
fmt.Println(err, "=> too many briqs sent to", user.Username)
err = nil
continue
}
cobra.CheckErr(err)
cobra.CheckErr(Render(res))
}
Expand Down
2 changes: 1 addition & 1 deletion utils/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *Set[T]) Len() int {

// Items returns a slice with copies of the items of the Set.
func (s *Set[T]) Items() []T {
items := make([]T, len(s.m))
items := make([]T, 0, len(s.m))
for k := range s.m {
items = append(items, k)
}
Expand Down