diff --git a/console/balance.go b/console/balance.go index ccb914a7d..de1a09bb7 100644 --- a/console/balance.go +++ b/console/balance.go @@ -6,6 +6,10 @@ import ( "fmt" ) +func init() { + commands["get_balance"] = &CmdGetBalance{} +} + // Data being sent to the rpc responder type ArgsGetBalance struct { Tenant string @@ -25,10 +29,9 @@ type ReplyGetBalance struct { // Commander implementation type CmdGetBalance struct { - rpcMethod string - rpcParams *ArgsGetBalance - rpcResult *ReplyGetBalance - idxArgsToRpcPrms map[int]string + rpcMethod string + rpcParams *ArgsGetBalance + rpcResult *ReplyGetBalance } // name should be exec's name @@ -38,9 +41,8 @@ func (self *CmdGetBalance) Usage(name string) string { // set param defaults func (self *CmdGetBalance) defaults() error { - self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceId", 5: "Direction"} self.rpcMethod = "Responder.GetBalance" - self.rpcParams = &ArgsGetBalance{BalanceId: "MONETARY", Direction: "OUT"} + self.rpcParams = &ArgsGetBalance{BalanceId: "MONETARYOUT", Direction: "OUT"} return nil } @@ -51,11 +53,13 @@ func (self *CmdGetBalance) FromArgs(args []string) error { } // Args look OK, set defaults before going further self.defaults() - self.rpcParams = &ArgsGetBalance{ - Tenant: args[2], - User: args[3], - BalanceId: args[4], - Direction: args[5], + self.rpcParams.Tenant = args[2] + self.rpcParams.User = args[3] + if len(args) > 4 { + self.rpcParams.BalanceId = args[4] + } + if len(args) > 5 { + self.rpcParams.Direction = args[5] } return nil } diff --git a/console/command.go b/console/command.go index c30ce4ba3..01dfe4460 100644 --- a/console/command.go +++ b/console/command.go @@ -5,6 +5,10 @@ import ( "path/filepath" ) +var ( + commands = make(map[string]Commander) +) + // Console Command interface type Commander interface { FromArgs(args []string) error // Load data from os arguments or flag.Args() @@ -20,14 +24,8 @@ func GetCommandValue(args []string) (Commander, error) { if len(args) < 2 { return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] \n", filepath.Base(args[0])) } - cmd := args[1] - var cmdVal Commander - switch cmd { - case "status": - cmdVal = &CmdStatus{} - case "get_balance": - cmdVal = &CmdGetBalance{} - default: + cmdVal, exists := commands[args[1]] + if !exists { return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] \n", filepath.Base(args[0])) } if err := cmdVal.FromArgs(args); err != nil { diff --git a/console/status.go b/console/status.go index e7b4b8f74..942731f70 100644 --- a/console/status.go +++ b/console/status.go @@ -4,6 +4,10 @@ import ( "fmt" ) +func init() { + commands["status"] = &CmdStatus{} +} + type CmdStatus struct { rpcMethod string rpcParams string