Running go fmt on modified files

This commit is contained in:
DanB
2012-09-13 09:32:41 +02:00
parent 213ece4060
commit 1633048cd4
4 changed files with 38 additions and 42 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package main
import (
"console"
"flag"
"fmt"
"log"
@@ -27,13 +28,12 @@ import (
"os"
"reflect"
"timespans"
"console"
)
var (
version = flag.Bool("version", false, "Prints the application version.")
server = flag.String("server", "127.0.0.1:2001", "server address host:port")
json = flag.Bool("json", true, "Use JSON for RPC encoding.")
version = flag.Bool("version", false, "Prints the application version.")
server = flag.String("server", "127.0.0.1:2001", "server address host:port")
json = flag.Bool("json", true, "Use JSON for RPC encoding.")
)
func main() {
@@ -55,16 +55,16 @@ func main() {
}
defer client.Close()
// Strict command parsing starts here
args := append( []string{os.Args[0]}, flag.Args()... ) // Emulate os.Args by prepending the cmd to list of args coming from flag
cmd, cmdErr := console.GetCommandValue( args )
args := append([]string{os.Args[0]}, flag.Args()...) // Emulate os.Args by prepending the cmd to list of args coming from flag
cmd, cmdErr := console.GetCommandValue(args)
if cmdErr != nil {
log.Fatal( cmdErr )
log.Fatal(cmdErr)
}
res := cmd.RpcResult()
if rpcErr := client.Call(cmd.RpcMethod(), cmd.RpcParams(), res); rpcErr != nil {
log.Fatal( err )
log.Fatal(err)
}
fmt.Println( reflect.ValueOf(res).Elem().String() )
fmt.Println(reflect.ValueOf(res).Elem().String())
}

View File

@@ -8,18 +8,17 @@ import (
"path/filepath"
)
type PrmsGetBalance struct {
User string
BalanceType string
Direction string
type PrmsGetBalance struct {
User string
BalanceType string
Direction string
}
type CmdGetBalance struct {
rpcMethod string
rpcParams PrmsGetBalance
rpcResult string
idxArgsToRpcPrms map[int]string
rpcMethod string
rpcParams PrmsGetBalance
rpcResult string
idxArgsToRpcPrms map[int]string
}
// name should be exec's name
@@ -44,11 +43,11 @@ func (self *CmdGetBalance) FromArgs(args []string) error {
// Args look OK, set defaults before going further
self.defaults()
// Dynamically set rpc params
CmdRpcPrmsFromArgs( self.rpcParams, args, self.idxArgsToRpcPrms )
CmdRpcPrmsFromArgs(self.rpcParams, args, self.idxArgsToRpcPrms)
return nil
}
func (self *CmdGetBalance) RpcMethod () string {
func (self *CmdGetBalance) RpcMethod() string {
return self.rpcMethod
}

View File

@@ -1,26 +1,23 @@
package console
import (
"fmt"
"reflect"
"path/filepath"
"reflect"
)
// Console Command interface
type Commander interface {
FromArgs(args []string) error // Load data from os arguments or flag.Args()
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
FromArgs(args []string) error // Load data from os arguments or flag.Args()
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 CmdRpcPrmsFromArgs( rpcPrms interface{}, args []string, idxArgsToRpcPrms map[int]string ) {
func CmdRpcPrmsFromArgs(rpcPrms interface{}, args []string, idxArgsToRpcPrms map[int]string) {
for idx := range args {
fldName, hasIdx := idxArgsToRpcPrms[idx]
if !hasIdx {
@@ -36,22 +33,22 @@ func CmdRpcPrmsFromArgs( rpcPrms interface{}, args []string, idxArgsToRpcPrms ma
}
// Process args and return right command Value or error
func GetCommandValue( args []string ) ( Commander, error ) {
func GetCommandValue(args []string) (Commander, error) {
if len(args) < 2 {
return nil, fmt.Errorf( "usage: %s <command>\n", filepath.Base(args[0]) )
}
cmd := args[1]
var cmdVal Commander
return nil, fmt.Errorf("usage: %s <command>\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 <command>\n", filepath.Base(args[0]) )
return nil, fmt.Errorf("usage: %s <command>\n", filepath.Base(args[0]))
}
if err := cmdVal.FromArgs(args); err != nil {
return nil, err
}
}
return cmdVal, nil
}

View File

@@ -5,9 +5,9 @@ import (
)
type CmdStatus struct {
rpcMethod string
rpcParams string
rpcResult string
rpcMethod string
rpcParams string
rpcResult string
}
func (self *CmdStatus) Usage(name string) string {
@@ -24,7 +24,7 @@ func (self *CmdStatus) FromArgs(args []string) error {
return nil
}
func (self *CmdStatus) RpcMethod () string {
func (self *CmdStatus) RpcMethod() string {
return self.rpcMethod
}