bedrocktool/utils/command_register.go

43 lines
961 B
Go
Raw Permalink Normal View History

2022-09-04 14:26:32 +00:00
package utils
2023-03-05 21:50:58 +00:00
import (
"context"
"flag"
2023-03-05 21:50:58 +00:00
"github.com/google/subcommands"
"github.com/sirupsen/logrus"
2023-03-05 21:50:58 +00:00
)
2022-09-04 14:26:32 +00:00
2023-03-05 21:50:58 +00:00
var ValidCMDs = make(map[string]Command, 0)
2022-09-04 14:26:32 +00:00
2023-03-05 21:50:58 +00:00
type Command interface {
Name() string
Synopsis() string
SetFlags(f *flag.FlagSet)
Execute(ctx context.Context, ui UI) error
}
type cmdWrap struct {
2023-03-05 21:50:58 +00:00
subcommands.Command
cmd Command
}
func (c *cmdWrap) Name() string { return c.cmd.Name() }
func (c *cmdWrap) Synopsis() string { return c.cmd.Synopsis() }
func (c *cmdWrap) SetFlags(f *flag.FlagSet) { c.cmd.SetFlags(f) }
func (c *cmdWrap) Usage() string { return c.Name() + ": " + c.Synopsis() }
func (c *cmdWrap) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
2023-03-14 01:07:39 +00:00
err := c.cmd.Execute(ctx, CurrentUI)
if err != nil {
logrus.Error(err)
return 1
}
return 0
2023-03-05 21:50:58 +00:00
}
func RegisterCommand(sub Command) {
subcommands.Register(&cmdWrap{cmd: sub}, "")
2023-03-05 21:50:58 +00:00
ValidCMDs[sub.Name()] = sub
2022-09-04 14:26:32 +00:00
}