use string wrapper for string param commands

This commit is contained in:
Radu Ioan Fericean
2014-04-19 11:48:05 +03:00
parent efc9240142
commit 0122acced1
9 changed files with 83 additions and 51 deletions

View File

@@ -74,7 +74,14 @@ func executeCommand(command string) {
return
}
res := cmd.RpcResult()
if rpcErr := client.Call(cmd.RpcMethod(), cmd.RpcParams(), res); rpcErr != nil {
param := cmd.RpcParams()
//log.Print(reflect.TypeOf(param))
switch param.(type) {
case *console.StringWrapper:
param = param.(*console.StringWrapper).Item
}
//log.Printf("Param: %+v", param)
if rpcErr := client.Call(cmd.RpcMethod(), param, res); rpcErr != nil {
fmt.Println("Error executing command: " + rpcErr.Error())
}
result, _ := json.MarshalIndent(res, "", " ")

View File

@@ -1,4 +1,4 @@
/*
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
@@ -20,18 +20,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package console
import (
"bytes"
"fmt"
"regexp"
"strings"
"github.com/cgrates/cgrates/utils"
)
var (
commands = make(map[string]Commander)
lineR = regexp.MustCompile(`(\w+)\s*=\s*(.+?)(?:\s+|$)`)
jsonR = regexp.MustCompile(`"(\w+)":(.+?)[,|}]`)
)
// Console Command interface
@@ -82,25 +76,6 @@ func GetCommandValue(command string, verbose bool) (Commander, error) {
return cmdVal, nil
}
func ToJSON(line string) (jsn []byte) {
jsn = append(jsn, '{')
for _, group := range lineR.FindAllStringSubmatch(line, -1) {
if len(group) == 3 {
jsn = append(jsn, []byte(fmt.Sprintf("\"%s\":%s,", group[1], group[2]))...)
}
}
jsn = bytes.TrimRight(jsn, ",")
jsn = append(jsn, '}')
return
}
func FromJSON(jsn []byte, interestingFields []string) (line string) {
for _, group := range jsonR.FindAllSubmatch(jsn, -1) {
if len(group) == 3 {
if utils.IsSliceMember(interestingFields, string(group[1])) {
line += fmt.Sprintf("%s=%s ", group[1], group[2])
}
}
}
return strings.TrimSpace(line)
type StringWrapper struct {
Item string
}

View File

@@ -19,11 +19,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package console
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/cgrates/cgrates/apier"
"github.com/cgrates/cgrates/utils"
)
var (
lineR = regexp.MustCompile(`(\w+)\s*=\s*(.+?)(?:\s+|$)`)
jsonR = regexp.MustCompile(`"(\w+)":(.+?)[,|}]`)
)
// Commander implementation
@@ -38,10 +46,6 @@ func (ce *CommandExecuter) Usage() string {
// Parses command line args and builds CmdBalance value
func (ce *CommandExecuter) FromArgs(args string, verbose bool) error {
if len(args) == 0 {
return fmt.Errorf(ce.Usage())
}
if err := json.Unmarshal(ToJSON(args), ce.command.RpcParams()); err != nil {
return err
}
@@ -53,11 +57,39 @@ func (ce *CommandExecuter) FromArgs(args string, verbose bool) error {
}
func (ce *CommandExecuter) ClientArgs() (args []string) {
val := reflect.ValueOf(&apier.AttrAddBalance{}).Elem()
val := reflect.ValueOf(ce.command.RpcParams()).Elem()
for i := 0; i < val.NumField(); i++ {
typeField := val.Type().Field(i)
args = append(args, typeField.Name)
}
return
}
func ToJSON(line string) (jsn []byte) {
if !strings.Contains(line, "=") {
line = fmt.Sprintf("Item=\"%s\"", line)
}
jsn = append(jsn, '{')
for _, group := range lineR.FindAllStringSubmatch(line, -1) {
if len(group) == 3 {
jsn = append(jsn, []byte(fmt.Sprintf("\"%s\":%s,", group[1], group[2]))...)
}
}
jsn = bytes.TrimRight(jsn, ",")
jsn = append(jsn, '}')
return
}
func FromJSON(jsn []byte, interestingFields []string) (line string) {
if !bytes.Contains(jsn, []byte{':'}) {
return fmt.Sprintf("\"%s\"", string(jsn))
}
for _, group := range jsonR.FindAllSubmatch(jsn, -1) {
if len(group) == 3 {
if utils.IsSliceMember(interestingFields, string(group[1])) {
line += fmt.Sprintf("%s=%s ", group[1], group[2])
}
}
}
return strings.TrimSpace(line)
}

View File

@@ -41,11 +41,18 @@ func TestToJSONValid(t *testing.T) {
func TestToJSONEmpty(t *testing.T) {
jsn := ToJSON("")
if string(jsn) != "{}" {
if string(jsn) != `{"Item":""}` {
t.Error("Error empty: ", string(jsn))
}
}
func TestToJSONString(t *testing.T) {
jsn := ToJSON("1002")
if string(jsn) != `{"Item":"1002"}` {
t.Error("Error string: ", string(jsn))
}
}
func TestFromJSON(t *testing.T) {
line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`), []string{"TimeStart", "Test", "Crazy", "Mama", "Test"})
expected := `TimeStart="Test" Crazy=1 Mama=true Test=1`
@@ -61,3 +68,11 @@ func TestFromJSONInterestingFields(t *testing.T) {
t.Errorf("Expected: %s got: '%s'", expected, line)
}
}
func TestFromJSONString(t *testing.T) {
line := FromJSON([]byte(`1002`), []string{"string"})
expected := `"1002"`
if line != expected {
t.Errorf("Expected: %s got: '%s'", expected, line)
}
}

View File

@@ -33,7 +33,7 @@ func init() {
type CmdGetCacheAge struct {
name string
rpcMethod string
rpcParams string
rpcParams *StringWrapper
rpcResult *utils.CachedItemAge
*CommandExecuter
}
@@ -47,6 +47,9 @@ func (self *CmdGetCacheAge) RpcMethod() string {
}
func (self *CmdGetCacheAge) RpcParams() interface{} {
if self.rpcParams == nil {
self.rpcParams = &StringWrapper{}
}
return self.rpcParams
}

View File

@@ -18,10 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package console
import (
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
import "github.com/cgrates/cgrates/engine"
func init() {
c := &CmdGetDestination{
@@ -36,7 +33,7 @@ func init() {
type CmdGetDestination struct {
name string
rpcMethod string
rpcParams *utils.AttrGetDestination
rpcParams *StringWrapper
rpcResult engine.Destination
*CommandExecuter
}
@@ -51,7 +48,7 @@ func (self *CmdGetDestination) RpcMethod() string {
func (self *CmdGetDestination) RpcParams() interface{} {
if self.rpcParams == nil {
self.rpcParams = &utils.AttrGetDestination{}
self.rpcParams = &StringWrapper{}
}
return self.rpcParams
}

View File

@@ -31,7 +31,7 @@ func init() {
type CmdReloadScheduler struct {
name string
rpcMethod string
rpcParams string
rpcParams *StringWrapper
rpcResult string
*CommandExecuter
}
@@ -45,9 +45,9 @@ func (self *CmdReloadScheduler) RpcMethod() string {
}
func (self *CmdReloadScheduler) RpcParams() interface{} {
/*if self.rpcParams == nil {
self.rpcParams = &utils.ApiReloadCache{}
}*/
if self.rpcParams == nil {
self.rpcParams = &StringWrapper{}
}
return self.rpcParams
}

View File

@@ -30,7 +30,7 @@ func init() {
type CmdStatus struct {
name string
rpcMethod string
rpcParams string
rpcParams *StringWrapper
rpcResult string
*CommandExecuter
}
@@ -44,7 +44,10 @@ func (self *CmdStatus) RpcMethod() string {
}
func (self *CmdStatus) RpcParams() interface{} {
return &self.rpcParams
if self.rpcParams == nil {
self.rpcParams = &StringWrapper{}
}
return self.rpcParams
}
func (self *CmdStatus) RpcResult() interface{} {

View File

@@ -153,7 +153,7 @@ func (cd *CallDescriptor) getAccount() (ub *Account, err error) {
cd.account, err = accountingStorage.GetAccount(cd.GetAccountKey())
}
if cd.account != nil && cd.account.Disabled {
return nil, fmt.Errorf("User %s is disabled", ub.Id)
return nil, fmt.Errorf("User %s is disabled", cd.account.Id)
}
return cd.account, err
}