diff --git a/cmd/cgr-console/cgr-console.go b/cmd/cgr-console/cgr-console.go index a692adf98..ed4d40aa4 100644 --- a/cmd/cgr-console/cgr-console.go +++ b/cmd/cgr-console/cgr-console.go @@ -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, "", " ") diff --git a/console/command.go b/console/command.go index a1a793619..d81b658cc 100644 --- a/console/command.go +++ b/console/command.go @@ -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 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 } diff --git a/console/command_executer.go b/console/command_executer.go index 81271c3af..d3bd1d174 100644 --- a/console/command_executer.go +++ b/console/command_executer.go @@ -19,11 +19,19 @@ along with this program. If not, see 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) +} diff --git a/console/command_test.go b/console/command_executer_test.go similarity index 83% rename from console/command_test.go rename to console/command_executer_test.go index 63e3b710a..0aeb14cc1 100644 --- a/console/command_test.go +++ b/console/command_executer_test.go @@ -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) + } +} diff --git a/console/get_cache_age.go b/console/get_cache_age.go index 43d81b831..c29f815b0 100644 --- a/console/get_cache_age.go +++ b/console/get_cache_age.go @@ -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 } diff --git a/console/get_destination.go b/console/get_destination.go index a9e9732b9..355b9e129 100644 --- a/console/get_destination.go +++ b/console/get_destination.go @@ -18,10 +18,7 @@ along with this program. If not, see 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 } diff --git a/console/reload_scheduler.go b/console/reload_scheduler.go index 1c8899d10..6863dff21 100644 --- a/console/reload_scheduler.go +++ b/console/reload_scheduler.go @@ -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 } diff --git a/console/status.go b/console/status.go index 47af4197d..4bf66b1b1 100644 --- a/console/status.go +++ b/console/status.go @@ -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{} { diff --git a/engine/calldesc.go b/engine/calldesc.go index a6d1385eb..fae773f79 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -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 }