Skip to content

Commit 2829cf6

Browse files
committed
always write in json file
1 parent a240685 commit 2829cf6

3 files changed

Lines changed: 65 additions & 8 deletions

File tree

cmd/curio/storacha-migration.go

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"os"
910
"path/filepath"
@@ -34,6 +35,7 @@ const storachaMigrationDataURL = "storacha-migration"
3435
type importPiecesOutput struct {
3536
Count int `json:"count"`
3637
Pieces []string `json:"pieces"`
38+
Error string `json:"error,omitempty"`
3739
}
3840

3941
type storachaImportResult struct {
@@ -66,14 +68,19 @@ var importPiecesCmd = &cli.Command{
6668
Name: "target",
6769
Usage: "path to storage directory in Curio's attached permanent storage",
6870
},
71+
&cli.StringFlag{
72+
Name: "result",
73+
Usage: "path to write the JSON result",
74+
},
6975
&cli.IntFlag{
7076
Name: "batch-size",
7177
Usage: "number of pieces to move to permanent storage",
7278
Value: 20,
7379
},
7480
},
75-
Action: func(cctx *cli.Context) error {
81+
Action: func(cctx *cli.Context) (err error) {
7682
ctx := cctx.Context
83+
var out importPiecesOutput
7784

7885
/*
7986
How this migration works:
@@ -108,6 +115,29 @@ var importPiecesCmd = &cli.Command{
108115
already present.
109116
*/
110117

118+
if cctx.String("result") == "" {
119+
return fmt.Errorf("result is required")
120+
}
121+
122+
resultPath, err := homedir.Expand(cctx.String("result"))
123+
if err != nil {
124+
return xerrors.Errorf("expanding result path: %w", err)
125+
}
126+
resultPath, err = filepath.Abs(filepath.Clean(resultPath))
127+
if err != nil {
128+
return xerrors.Errorf("resolving result path: %w", err)
129+
}
130+
131+
defer func() {
132+
if err != nil {
133+
out.Error = err.Error()
134+
}
135+
writeErr := writeImportPiecesResult(resultPath, out)
136+
if writeErr != nil {
137+
err = errors.Join(err, writeErr)
138+
}
139+
}()
140+
111141
if cctx.String("source") == "" || cctx.String("target") == "" {
112142
return fmt.Errorf("source and target both are required")
113143
}
@@ -138,12 +168,7 @@ var importPiecesCmd = &cli.Command{
138168

139169
si := paths.NewDBIndex(curioalerting.NewAlertingSystem(), db)
140170

141-
out, err := runImportPieces(ctx, db, si, storageID, sourcePath, targetPath, cctx.Int("batch-size"))
142-
if err != nil {
143-
return err
144-
}
145-
146-
err = PrintJson(out)
171+
out, err = runImportPieces(ctx, db, si, storageID, sourcePath, targetPath, cctx.Int("batch-size"))
147172
if err != nil {
148173
return err
149174
}
@@ -152,6 +177,19 @@ var importPiecesCmd = &cli.Command{
152177
},
153178
}
154179

180+
func writeImportPiecesResult(path string, out importPiecesOutput) error {
181+
resJson, err := json.MarshalIndent(out, "", " ")
182+
if err != nil {
183+
return xerrors.Errorf("marshalling import result: %w", err)
184+
}
185+
186+
if err := os.WriteFile(path, resJson, 0644); err != nil {
187+
return xerrors.Errorf("writing import result: %w", err)
188+
}
189+
190+
return nil
191+
}
192+
155193
func runImportPieces(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex, storageID storiface.ID, sourcePath, targetPath string, batchSize int) (importPiecesOutput, error) {
156194
var out importPiecesOutput
157195

cmd/curio/storacha-migration_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ func TestStorachaPieceInfoFromFileName(t *testing.T) {
8888
require.Nil(t, pi)
8989
}
9090

91+
func TestWriteImportPiecesResultIncludesError(t *testing.T) {
92+
path := filepath.Join(t.TempDir(), "result.json")
93+
expected := importPiecesOutput{
94+
Count: 2,
95+
Pieces: []string{"piece-a", "piece-b"},
96+
Error: "import failed",
97+
}
98+
99+
require.NoError(t, writeImportPiecesResult(path, expected))
100+
101+
mb, err := os.ReadFile(path)
102+
require.NoError(t, err)
103+
104+
var actual importPiecesOutput
105+
require.NoError(t, json.Unmarshal(mb, &actual))
106+
require.Equal(t, expected, actual)
107+
}
108+
91109
func TestStorachaMigrationFreshImport(t *testing.T) {
92110
env := setupStorachaMigrationTest(t)
93111
pieces := newStorachaPieceFixtures(t)

documentation/en/curio-cli/curio.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,8 @@ USAGE:
14471447
14481448
OPTIONS:
14491449
--source value path to store the pieces from
1450-
--target value path to piece storage directory in Curio's attached permanent storage
1450+
--target value path to storage directory in Curio's attached permanent storage
1451+
--result value path to write the JSON result
14511452
--batch-size value number of pieces to move to permanent storage (default: 20)
14521453
--help, -h show help
14531454
```

0 commit comments

Comments
 (0)