Skip to content

Commit

Permalink
chore: minor examples cleanup
Browse files Browse the repository at this point in the history
- Pass time.Duration instead of passing int that gets converted to time.Duration.
- Remove unneeded else clauses
- spelling
  • Loading branch information
gammazero committed Dec 2, 2024
1 parent caa6763 commit 89c8b8c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/bitswap-transfer/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Transfering UnixFS file with Bitswap
# Transferring UnixFS file with Bitswap

This is an example that quickly shows how to use IPFS tooling to move around a file.

Expand Down
15 changes: 8 additions & 7 deletions examples/routing/delegated-routing-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,32 @@ func main() {
namePtr := flag.String("ipns", "", "ipns name to retrieve record for")
flag.Parse()

if err := run(os.Stdout, *gatewayUrlPtr, *cidPtr, *pidPtr, *namePtr, *timeoutPtr); err != nil {
timeout := time.Duration(*timeoutPtr) * time.Second
if err := run(os.Stdout, *gatewayUrlPtr, *cidPtr, *pidPtr, *namePtr, timeout); err != nil {

Check warning on line 30 in examples/routing/delegated-routing-client/main.go

View check run for this annotation

Codecov / codecov/patch

examples/routing/delegated-routing-client/main.go#L29-L30

Added lines #L29 - L30 were not covered by tests
log.Fatal(err)
}
}

func run(w io.Writer, gatewayURL, cidStr, pidStr, nameStr string, timeoutSeconds int) error {
func run(w io.Writer, gatewayURL, cidStr, pidStr, nameStr string, timeout time.Duration) error {
// Creates a new Delegated Routing V1 client.
client, err := client.New(gatewayURL)
if err != nil {
return err
}

timeout := time.Duration(timeoutSeconds) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

if cidStr != "" {
return findProviders(w, ctx, client, cidStr)
} else if pidStr != "" {
}
if pidStr != "" {
return findPeers(w, ctx, client, pidStr)
} else if nameStr != "" {
}
if nameStr != "" {
return findIPNS(w, ctx, client, nameStr)
} else {
return errors.New("cid or peer must be provided")
}
return errors.New("cid or peer must be provided")

Check warning on line 54 in examples/routing/delegated-routing-client/main.go

View check run for this annotation

Codecov / codecov/patch

examples/routing/delegated-routing-client/main.go#L54

Added line #L54 was not covered by tests
}

func findProviders(w io.Writer, ctx context.Context, client *client.Client, cidStr string) error {
Expand Down
6 changes: 3 additions & 3 deletions examples/routing/delegated-routing-client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFindProviders(t *testing.T) {
t.Cleanup(ts.Close)

out := &bytes.Buffer{}
err := run(out, ts.URL, cidStr, "", "", 1)
err := run(out, ts.URL, cidStr, "", "", time.Second)
assert.Contains(t, out.String(), "12D3KooWM8sovaEGU1bmiWGWAzvs47DEcXKZZTuJnpQyVTkRs2Vn\n\tProtocols: [transport-bitswap]\n\tAddresses: [/ip4/111.222.222.111/tcp/5734]\n")
assert.Contains(t, out.String(), "12D3KooWB6RAWgcmHAP7TGEGK7utV2ZuqSzX1DNjRa97TtJ7139n\n\tProtocols: []\n\tAddresses: [/ip4/127.0.0.1/tcp/5734]\n")
assert.NoError(t, err)
Expand All @@ -50,7 +50,7 @@ func TestFindPeers(t *testing.T) {
t.Cleanup(ts.Close)

out := &bytes.Buffer{}
err := run(out, ts.URL, "", pidStr, "", 1)
err := run(out, ts.URL, "", pidStr, "", time.Second)
assert.Contains(t, out.String(), "12D3KooWM8sovaEGU1bmiWGWAzvs47DEcXKZZTuJnpQyVTkRs2Vn\n\tProtocols: [transport-bitswap]\n\tAddresses: [/ip4/111.222.222.111/tcp/5734]\n")
assert.NoError(t, err)
}
Expand All @@ -67,7 +67,7 @@ func TestGetIPNS(t *testing.T) {
t.Cleanup(ts.Close)

out := &bytes.Buffer{}
err := run(out, ts.URL, "", "", name.String(), 1)
err := run(out, ts.URL, "", "", name.String(), time.Second)
assert.Contains(t, out.String(), fmt.Sprintf("/ipns/%s\n\tSignature: VALID\n\tValue: /ipfs/bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4\n", name.String()))
assert.NoError(t, err)
}
Expand Down

0 comments on commit 89c8b8c

Please sign in to comment.