From 2f4aa6b390242cdfb39d0f1b938651798a8e220f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Mon, 27 Feb 2023 21:29:02 +0000 Subject: [PATCH] add a single mode. --- generate-car.go | 54 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/generate-car.go b/generate-car.go index e189478..14fa7b8 100644 --- a/generate-car.go +++ b/generate-car.go @@ -6,6 +6,11 @@ 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" @@ -13,10 +18,6 @@ import ( 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 { @@ -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"}, @@ -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"