fix args.go affected from cherry-pick

This commit is contained in:
yusing 2025-02-18 16:35:23 +08:00
parent b2a6a20f10
commit e6f77376b9

View file

@ -1,7 +1,9 @@
package common package common
import ( import (
"flag"
"fmt" "fmt"
"log"
) )
type Args struct { type Args struct {
@ -20,11 +22,8 @@ const (
CommandDebugListMTrace = "debug-ls-mtrace" CommandDebugListMTrace = "debug-ls-mtrace"
) )
type MainServerCommandValidator struct{} var ValidCommands = []string{
CommandStart,
func (v MainServerCommandValidator) IsCommandValid(cmd string) bool {
switch cmd {
case CommandStart,
CommandValidate, CommandValidate,
CommandListConfigs, CommandListConfigs,
CommandListRoutes, CommandListRoutes,
@ -32,8 +31,24 @@ func (v MainServerCommandValidator) IsCommandValid(cmd string) bool {
CommandReload, CommandReload,
CommandDebugListEntries, CommandDebugListEntries,
CommandDebugListProviders, CommandDebugListProviders,
CommandDebugListMTrace: CommandDebugListMTrace,
return true }
func validateArg(arg string) error {
for _, v := range ValidCommands {
if arg == v {
return nil
}
} }
return fmt.Errorf("invalid command %q", arg) return fmt.Errorf("invalid command %q", arg)
} }
func GetArgs() Args {
var args Args
flag.Parse()
args.Command = flag.Arg(0)
if err := validateArg(args.Command); err != nil {
log.Fatalf("invalid command: %s", err)
}
return args
}