mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化command模块
This commit is contained in:
@@ -2,11 +2,9 @@ package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CommandFunctionCB func(param interface{})error
|
||||
type CommandFunctionCB func(args []string)error
|
||||
|
||||
var mapRegisterCmd map[string]CommandFunctionCB = map[string]CommandFunctionCB{}
|
||||
var programName string
|
||||
@@ -22,37 +20,9 @@ func Run(args []string) error {
|
||||
return fmt.Errorf("command not found, try `%s help` for help",args[0])
|
||||
}
|
||||
|
||||
switch args[1] {
|
||||
case "start":
|
||||
if len(args)<2 {
|
||||
return fmt.Errorf("command not found, try `%s help` for help",args[0])
|
||||
}else{
|
||||
return start(fn,args[2])
|
||||
}
|
||||
case "stop":
|
||||
return fn(nil)
|
||||
}
|
||||
|
||||
return fmt.Errorf("command not found, try `%s help` for help",args[0])
|
||||
return fn(args)
|
||||
}
|
||||
|
||||
func start(fn CommandFunctionCB,param string) error {
|
||||
sparam := strings.Split(param,"=")
|
||||
if len(sparam) != 2 {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
}
|
||||
if sparam[0]!="nodeid" {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
}
|
||||
nodeId,err:= strconv.Atoi(sparam[1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
}
|
||||
|
||||
return fn(nodeId)
|
||||
}
|
||||
|
||||
|
||||
func RegisterCommand(cmd string,fn CommandFunctionCB){
|
||||
mapRegisterCmd[cmd] = fn
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user