fix: worker: add all task type flag

Add all flag for the `lotus-worker tasks enable/disable` cmd
This commit is contained in:
Phi 2023-02-07 16:12:31 +01:00
parent 8066833b3a
commit cf0997b434

View File

@ -45,32 +45,52 @@ var tasksEnableCmd = &cli.Command{
Name: "enable", Name: "enable",
Usage: "Enable a task type", Usage: "Enable a task type",
ArgsUsage: "[" + settableStr + "]", ArgsUsage: "[" + settableStr + "]",
Action: taskAction(api.Worker.TaskEnable), Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "Enable all task types",
},
},
Action: taskAction(api.Worker.TaskEnable),
} }
var tasksDisableCmd = &cli.Command{ var tasksDisableCmd = &cli.Command{
Name: "disable", Name: "disable",
Usage: "Disable a task type", Usage: "Disable a task type",
ArgsUsage: "[" + settableStr + "]", ArgsUsage: "[" + settableStr + "]",
Action: taskAction(api.Worker.TaskDisable), Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "Disable all task types",
},
},
Action: taskAction(api.Worker.TaskDisable),
} }
func taskAction(tf func(a api.Worker, ctx context.Context, tt sealtasks.TaskType) error) func(cctx *cli.Context) error { func taskAction(tf func(a api.Worker, ctx context.Context, tt sealtasks.TaskType) error) func(cctx *cli.Context) error {
return func(cctx *cli.Context) error { return func(cctx *cli.Context) error {
if cctx.NArg() != 1 { allFlag := cctx.Bool("all")
return xerrors.Errorf("expected 1 argument")
if cctx.NArg() == 1 && allFlag {
return xerrors.Errorf("Cannot use --all flag with task type argument")
}
if cctx.NArg() == 0 && !allFlag {
return xerrors.Errorf("Expected 1 argument or use --all flag")
} }
var tt sealtasks.TaskType var tt sealtasks.TaskType
for taskType := range allowSetting { if cctx.NArg() == 1 {
if taskType.Short() == cctx.Args().First() { for taskType := range allowSetting {
tt = taskType if taskType.Short() == cctx.Args().First() {
break tt = taskType
break
}
} }
}
if tt == "" { if tt == "" {
return xerrors.Errorf("unknown task type '%s'", cctx.Args().First()) return xerrors.Errorf("unknown task type '%s'", cctx.Args().First())
}
} }
api, closer, err := lcli.GetWorkerAPI(cctx) api, closer, err := lcli.GetWorkerAPI(cctx)
@ -81,6 +101,15 @@ func taskAction(tf func(a api.Worker, ctx context.Context, tt sealtasks.TaskType
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
if allFlag {
for taskType := range allowSetting {
if err := tf(api, ctx, taskType); err != nil {
return err
}
}
return nil
}
return tf(api, ctx, tt) return tf(api, ctx, tt)
} }
} }