Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Very simple command line #14

Open
wants to merge 6 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package main
import (
"io/fs"
"os"
"fmt"
"context"
"github.com/web3-storage/go-w3s-client"
)

Expand Down
9 changes: 7 additions & 2 deletions put.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (c *client) PutCar(ctx context.Context, car io.Reader) (cid.Cid, error) {
var root cid.Cid
for {
r, err := spltr.Next()

if err != nil {
if err == io.EOF {
break
Expand All @@ -110,8 +111,8 @@ func (c *client) PutCar(ctx context.Context, car io.Reader) (cid.Cid, error) {
return root, nil
}

// TODO: retry
func (c *client) sendCar(ctx context.Context, r io.Reader) (cid.Cid, error) {

req, err := http.NewRequestWithContext(ctx, "POST", c.cfg.endpoint+"/car", r)
if err != nil {
return cid.Undef, err
Expand All @@ -124,7 +125,11 @@ func (c *client) sendCar(ctx context.Context, r io.Reader) (cid.Cid, error) {
return cid.Undef, err
}
if res.StatusCode != 200 {
return cid.Undef, fmt.Errorf("unexpected response status: %d", res.StatusCode)

//retry on failure
fmt.Printf("unexpected response status: %d, retrying..\n", res.StatusCode)
c.sendCar(ctx, r)

}
d := json.NewDecoder(res.Body)
var out struct {
Expand Down
44 changes: 44 additions & 0 deletions w3scli/w3scli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"fmt"
"io/fs"
"os"
"github.com/ipfs/go-cid"
"github.com/web3-storage/go-w3s-client"

)

// Usage:
// TOKEN="API_TOKEN" go run ./w3scli.go
func main() {


c, err := w3s.NewClient(
w3s.WithEndpoint(os.Getenv("ENDPOINT")),
w3s.WithToken(os.Getenv("TOKEN")),
)
if err != nil {
panic(err)
}

arg := os.Args[1]
file, err := os.Open(arg)
if err != nil {
panic(err)
}

putFile(c, file)

}


func putFile(c w3s.Client, f fs.File, opts ...w3s.PutOption) cid.Cid {
cid, err := c.Put(context.Background(), f, opts...)
if err != nil {
panic(err)
}
fmt.Printf("https://%v.ipfs.dweb.link\n", cid)
return cid
}