diff --git a/console/balance.go b/console/balance.go index d4d0db1f2..caa631589 100644 --- a/console/balance.go +++ b/console/balance.go @@ -8,42 +8,43 @@ import ( "path/filepath" ) -type CmdGetBalance struct { + +type PrmsGetBalance struct { User string BalanceType string Direction string - rpcMethod string - rpcParams interface{} - rpcResult string +} + +type CmdGetBalance struct { + rpcMethod string + rpcParams PrmsGetBalance + rpcResult string + idxArgsToRpcPrms map[int]string } // name should be exec's name -func (self *CmdGetBalance) usage(name string) string { +func (self *CmdGetBalance) Usage(name string) string { return fmt.Sprintf("usage: %s get_balance []", name) } // set param defaults func (self *CmdGetBalance) defaults() error { + self.idxArgsToRpcPrms = map[int]string{2: "User", 3: "BalanceType", 4: "Direction"} self.rpcMethod = "Responder.GetBalance" - self.rpcParams = self - self.BalanceType = "MONETARY" - self.Direction = "OUT" + self.rpcParams.BalanceType = "MONETARY" + self.rpcParams.Direction = "OUT" return nil } -func( self *CmdGetBalance) idxArgsToFields() map[int]string { - return map[int]string{2: "User", 3: "BalanceType", 4: "Direction"} -} - // Parses command line args and builds CmdBalance value func (self *CmdGetBalance) FromArgs(args []string) error { if len(os.Args) < 3 { - return fmt.Errorf(self.usage(filepath.Base(args[0]))) + return fmt.Errorf(self.Usage(filepath.Base(args[0]))) } // Args look OK, set defaults before going further self.defaults() - // Dynamically set field values - CmdFieldsFromArgs( self, args ) + // Dynamically set rpc params + CmdRpcPrmsFromArgs( self.rpcParams, args, self.idxArgsToRpcPrms ) return nil } diff --git a/console/command.go b/console/command.go index 0ee5a6ddf..5489d7414 100644 --- a/console/command.go +++ b/console/command.go @@ -11,24 +11,23 @@ import ( // Console Command interface type Commander interface { FromArgs(args []string) error // Load data from os arguments or flag.Args() - usage(string) string // usage message - defaults() error // set default field values - idxArgsToFields() map[int]string // field's index position in command arguments + Usage(string) string // usage message RpcMethod() string // Method which should be called remotely RpcParams() interface{} // Parameters to send out on rpc RpcResult() interface{} // Only requirement is to have a String method to print on console + defaults() error // set default field values } // Set command fields based on indexes defined in default() -func CmdFieldsFromArgs( cmd Commander, args []string ) { +func CmdRpcPrmsFromArgs( rpcPrms interface{}, args []string, idxArgsToRpcPrms map[int]string ) { for idx := range args { - fldName, hasIdx := cmd.idxArgsToFields()[idx] + fldName, hasIdx := idxArgsToRpcPrms[idx] if !hasIdx { continue } // field defined to be set by os.Args index - if fld := reflect.ValueOf(cmd).Elem().FieldByName(fldName); fld.Kind() == reflect.String { + if fld := reflect.ValueOf(rpcPrms).Elem().FieldByName(fldName); fld.Kind() == reflect.String { fld.SetString(args[idx]) } else if fld.Kind() == reflect.Int { fld.SetInt(1) // Placeholder for future usage of data types other than strings @@ -56,5 +55,3 @@ func GetCommandValue( args []string ) ( Commander, error ) { } return cmdVal, nil } - - diff --git a/console/status.go b/console/status.go index e9f793a27..593376913 100644 --- a/console/status.go +++ b/console/status.go @@ -10,7 +10,7 @@ type CmdStatus struct { rpcResult string } -func (self *CmdStatus) usage(name string) string { +func (self *CmdStatus) Usage(name string) string { return fmt.Sprintf("usage: %s status", name) } @@ -19,10 +19,6 @@ func (self *CmdStatus) defaults() error { return nil } -func( self *CmdStatus) idxArgsToFields() map[int]string { - return nil -} - func (self *CmdStatus) FromArgs(args []string) error { self.defaults() return nil