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"
3435type importPiecesOutput struct {
3536 Count int `json:"count"`
3637 Pieces []string `json:"pieces"`
38+ Error string `json:"error,omitempty"`
3739}
3840
3941type 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+
155193func 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
0 commit comments