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

Commit

Permalink
Merge pull request #7 from raulk/raulk/single
Browse files Browse the repository at this point in the history
add a single mode.
  • Loading branch information
xinaxu authored Feb 27, 2023
2 parents d252581 + 2f4aa6b commit 2e7d814
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions generate-car.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path"

commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/go-fil-commp-hashhash"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/tech-greedy/go-generate-car/util"
"github.com/urfave/cli/v2"
"io"
"log"
"os"
"path"
)

type CommpResult struct {
Expand Down Expand Up @@ -51,6 +52,10 @@ func main() {
Name: "generate-car",
Usage: "generate car archive from list of files and compute commp in the mean time",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "single",
Usage: "When enabled, it indicates that the input is a single file to be included in full, instead of a spec JSON",
},
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Expand Down Expand Up @@ -88,26 +93,41 @@ func main() {
outDir := c.String("out-dir")
parent := c.String("parent")
tmpDir := c.String("tmp-dir")
var inputBytes []byte
if inputFile == "-" {
reader := bufio.NewReader(os.Stdin)
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(reader)
single := c.Bool("single")

var input Input
if single {
stat, err := os.Stat(inputFile)
if err != nil {
return err
}
inputBytes = buf.Bytes()
input = append(input, util.Finfo{
Path: inputFile,
Size: stat.Size(),
Start: 0,
End: stat.Size(),
})
} else {
bytes, err := os.ReadFile(inputFile)
var inputBytes []byte
if inputFile == "-" {
reader := bufio.NewReader(os.Stdin)
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(reader)
if err != nil {
return err
}
inputBytes = buf.Bytes()
} else {
bytes, err := os.ReadFile(inputFile)
if err != nil {
return err
}
inputBytes = bytes
}
err := json.Unmarshal(inputBytes, &input)
if err != nil {
return err
}
inputBytes = bytes
}
var input Input
err := json.Unmarshal(inputBytes, &input)
if err != nil {
return err
}

outFilename := uuid.New().String() + ".car"
Expand Down

0 comments on commit 2e7d814

Please sign in to comment.