-
Notifications
You must be signed in to change notification settings - Fork 133
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
Misc. fixes #852
base: main
Are you sure you want to change the base?
Misc. fixes #852
Changes from 1 commit
e3f0652
37f50f5
c4befdb
4c9c614
d90abdf
09c3c35
9f8d020
d16ddc8
9ab4495
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,15 @@ package main | |
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/bluesky-social/indigo/atproto/data" | ||
"github.com/bluesky-social/indigo/atproto/identity" | ||
"github.com/bluesky-social/indigo/atproto/lexicon" | ||
"github.com/bluesky-social/indigo/atproto/syntax" | ||
|
||
"github.com/bobg/errors" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A humble suggestion. Please see https://github.com/bobg/errors#readme |
||
"github.com/urfave/cli/v2" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
) | ||
|
||
func runValidateRecord(cctx *cli.Context) error { | ||
|
@@ -29,12 +28,12 @@ func runValidateRecord(cctx *cli.Context) error { | |
cat := lexicon.NewBaseCatalog() | ||
err := cat.LoadDirectory(p) | ||
if err != nil { | ||
return err | ||
return errors.Wrap(err, "in LoadDirectory") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Especially when a function has multiple error return sites, it's useful to wrap them to know which site did the returning. |
||
} | ||
|
||
aturi, err := syntax.ParseATURI(args[1]) | ||
if err != nil { | ||
return err | ||
return errors.Wrap(err, "in ParseATURI") | ||
} | ||
if aturi.RecordKey() == "" { | ||
return fmt.Errorf("need a full, not partial, AT-URI: %s", aturi) | ||
|
@@ -54,17 +53,24 @@ func runValidateRecord(cctx *cli.Context) error { | |
pdsURL, ident.DID, aturi.Collection(), aturi.RecordKey()) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
return errors.Wrap(err, "in http.Get") | ||
} | ||
defer resp.Body.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Required after any |
||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("fetch failed") | ||
} | ||
|
||
respBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
return errors.Wrap(err, "in ReadAll") | ||
} | ||
|
||
body, err := data.UnmarshalJSON(respBytes) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error was not being checked. |
||
return errors.Wrap(err, "in UnmarshalJSON") | ||
} | ||
|
||
record, ok := body["value"].(map[string]any) | ||
if !ok { | ||
return fmt.Errorf("fetched record was not an object") | ||
|
@@ -73,7 +79,7 @@ func runValidateRecord(cctx *cli.Context) error { | |
slog.Info("validating", "did", ident.DID.String(), "collection", aturi.Collection().String(), "rkey", aturi.RecordKey().String()) | ||
err = lexicon.ValidateRecord(&cat, record, aturi.Collection().String(), lexicon.LenientMode) | ||
if err != nil { | ||
return err | ||
return errors.Wrap(err, "in ValidateRecord") | ||
} | ||
fmt.Println("success!") | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,8 @@ func (s MemFlagStore) Get(ctx context.Context, key string) ([]string, error) { | |
} | ||
|
||
func (s MemFlagStore) Add(ctx context.Context, key string, flags []string) error { | ||
v, ok := s.Data[key] | ||
if !ok { | ||
v = []string{} | ||
} | ||
for _, f := range flags { | ||
v = append(v, f) | ||
} | ||
v, _ := s.Data[key] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
v = append(v, flags...) | ||
v = dedupeStrings(v) | ||
s.Data[key] = v | ||
return nil | ||
|
@@ -51,8 +46,8 @@ func (s MemFlagStore) Remove(ctx context.Context, key string, flags []string) er | |
for _, f := range flags { | ||
delete(m, f) | ||
} | ||
out := []string{} | ||
for f, _ := range m { | ||
var out []string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is almost always the better way to initialize a slice. This sets it to |
||
for f := range m { | ||
out = append(out, f) | ||
} | ||
s.Data[key] = out | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant
return
.