diff --git a/console/command.go b/console/command.go new file mode 100644 index 000000000..07afb3251 --- /dev/null +++ b/console/command.go @@ -0,0 +1,60 @@ +package console + + +import ( + "fmt" + "reflect" + "path/filepath" +) + + + +// 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 + 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 +} + + +// Set command fields based on indexes defined in default() +func CmdFieldsFromArgs( cmd Commander, args []string ) { + for idx := range args { + fldName, hasIdx := cmd.idxArgsToFields()[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 { + fld.SetString(args[idx]) + } else if fld.Kind() == reflect.Int { + fld.SetInt(1) // Placeholder for future usage of data types other than strings + } + } +} + +// Process args and return right command Value or error +func GetCommandValue( args []string ) ( Commander, error ) { + if len(args) < 2 { + return nil, fmt.Errorf( "usage: %s \n", filepath.Base(args[0]) ) + } + cmd := args[1] + var cmdVal Commander + switch cmd { + case "status": + cmdVal = &CmdStatus{} + case "get_balance": + cmdVal = &CmdGetBalance{} + default: + return nil, fmt.Errorf( "usage: %s \n", filepath.Base(args[0]) ) + } + if err := cmdVal.FromArgs(args); err != nil { + return nil, err + } + return cmdVal, nil +} + diff --git a/console/status.go b/console/status.go new file mode 100644 index 000000000..e9f793a27 --- /dev/null +++ b/console/status.go @@ -0,0 +1,41 @@ +package console + +import ( + "fmt" +) + +type CmdStatus struct { + rpcMethod string + rpcParams string + rpcResult string +} + +func (self *CmdStatus) usage(name string) string { + return fmt.Sprintf("usage: %s status", name) +} + +func (self *CmdStatus) defaults() error { + self.rpcMethod = "Responder.Status" + return nil +} + +func( self *CmdStatus) idxArgsToFields() map[int]string { + return nil +} + +func (self *CmdStatus) FromArgs(args []string) error { + self.defaults() + return nil +} + +func (self *CmdStatus) RpcMethod () string { + return self.rpcMethod +} + +func (self *CmdStatus) RpcParams() interface{} { + return self.rpcParams +} + +func (self *CmdStatus) RpcResult() interface{} { + return &self.rpcResult +}