Merge pull request #10860 from filecoin-project/phi-fix-args-redeclare

fix: cli: make redeclare cmd work properly
This commit is contained in:
Phi-rjan 2023-05-18 19:13:07 +02:00 committed by GitHub
commit bd85a6a72f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 6 deletions

View File

@ -225,6 +225,7 @@ var storageRedeclareCmd = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "drop-missing", Name: "drop-missing",
Usage: "Drop index entries with missing files", Usage: "Drop index entries with missing files",
Value: true,
}, },
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
@ -235,14 +236,19 @@ var storageRedeclareCmd = &cli.Command{
defer closer() defer closer()
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
if cctx.NArg() != 1 { // check if no argument and no --id or --all flag is provided
return lcli.IncorrectNumArgs(cctx) if cctx.NArg() == 0 && !cctx.IsSet("id") && !cctx.Bool("all") {
return xerrors.Errorf("You must specify a storage path, or --id, or --all")
} }
if cctx.IsSet("id") && cctx.Bool("all") { if cctx.IsSet("id") && cctx.Bool("all") {
return xerrors.Errorf("--id and --all can't be passed at the same time") return xerrors.Errorf("--id and --all can't be passed at the same time")
} }
if cctx.Bool("all") && cctx.NArg() > 0 {
return xerrors.Errorf("No additional arguments are expected when --all is set")
}
if cctx.IsSet("id") { if cctx.IsSet("id") {
id := storiface.ID(cctx.String("id")) id := storiface.ID(cctx.String("id"))
return minerApi.StorageRedeclareLocal(ctx, &id, cctx.Bool("drop-missing")) return minerApi.StorageRedeclareLocal(ctx, &id, cctx.Bool("drop-missing"))
@ -252,7 +258,28 @@ var storageRedeclareCmd = &cli.Command{
return minerApi.StorageRedeclareLocal(ctx, nil, cctx.Bool("drop-missing")) return minerApi.StorageRedeclareLocal(ctx, nil, cctx.Bool("drop-missing"))
} }
return xerrors.Errorf("either --all or --id must be specified") // As no --id or --all flag is set, we can assume the argument is a path.
path := cctx.Args().First()
metaFilePath := filepath.Join(path, "sectorstore.json")
var meta storiface.LocalStorageMeta
metaFile, err := os.Open(metaFilePath)
if err != nil {
return xerrors.Errorf("Failed to open file: %w", err)
}
defer func() {
if closeErr := metaFile.Close(); closeErr != nil {
log.Error("Failed to close the file: %v", closeErr)
}
}()
err = json.NewDecoder(metaFile).Decode(&meta)
if err != nil {
return xerrors.Errorf("Failed to decode file: %w", err)
}
id := meta.ID
return minerApi.StorageRedeclareLocal(ctx, &id, cctx.Bool("drop-missing"))
}, },
} }

View File

@ -178,6 +178,7 @@ var storageRedeclareCmd = &cli.Command{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "drop-missing", Name: "drop-missing",
Usage: "Drop index entries with missing files", Usage: "Drop index entries with missing files",
Value: true,
}, },
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
@ -188,10 +189,19 @@ var storageRedeclareCmd = &cli.Command{
defer closer() defer closer()
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
// check if no argument and no --id or --all flag is provided
if cctx.NArg() == 0 && !cctx.IsSet("id") && !cctx.Bool("all") {
return xerrors.Errorf("You must specify a storage path, or --id, or --all")
}
if cctx.IsSet("id") && cctx.Bool("all") { if cctx.IsSet("id") && cctx.Bool("all") {
return xerrors.Errorf("--id and --all can't be passed at the same time") return xerrors.Errorf("--id and --all can't be passed at the same time")
} }
if cctx.Bool("all") && cctx.NArg() > 0 {
return xerrors.Errorf("No additional arguments are expected when --all is set")
}
if cctx.IsSet("id") { if cctx.IsSet("id") {
id := storiface.ID(cctx.String("id")) id := storiface.ID(cctx.String("id"))
return nodeApi.StorageRedeclareLocal(ctx, &id, cctx.Bool("drop-missing")) return nodeApi.StorageRedeclareLocal(ctx, &id, cctx.Bool("drop-missing"))
@ -201,6 +211,27 @@ var storageRedeclareCmd = &cli.Command{
return nodeApi.StorageRedeclareLocal(ctx, nil, cctx.Bool("drop-missing")) return nodeApi.StorageRedeclareLocal(ctx, nil, cctx.Bool("drop-missing"))
} }
return xerrors.Errorf("either --all or --id must be specified") // As no --id or --all flag is set, we can assume the argument is a path.
path := cctx.Args().First()
metaFilePath := filepath.Join(path, "sectorstore.json")
var meta storiface.LocalStorageMeta
metaFile, err := os.Open(metaFilePath)
if err != nil {
return xerrors.Errorf("Failed to open file: %w", err)
}
defer func() {
if closeErr := metaFile.Close(); closeErr != nil {
log.Error("Failed to close the file: %v", closeErr)
}
}()
err = json.NewDecoder(metaFile).Decode(&meta)
if err != nil {
return xerrors.Errorf("Failed to decode file: %w", err)
}
id := meta.ID
return nodeApi.StorageRedeclareLocal(ctx, &id, cctx.Bool("drop-missing"))
}, },
} }

View File

@ -1285,7 +1285,7 @@ USAGE:
OPTIONS: OPTIONS:
--all redeclare all storage paths (default: false) --all redeclare all storage paths (default: false)
--drop-missing Drop index entries with missing files (default: false) --drop-missing Drop index entries with missing files (default: true)
--id value storage path ID --id value storage path ID
``` ```

View File

@ -148,7 +148,7 @@ USAGE:
OPTIONS: OPTIONS:
--all redeclare all storage paths (default: false) --all redeclare all storage paths (default: false)
--drop-missing Drop index entries with missing files (default: false) --drop-missing Drop index entries with missing files (default: true)
--id value storage path ID --id value storage path ID
``` ```