From 5f4580947a51be6f9568c4a91e3c13c896d2c7ca Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 17 Apr 2014 16:32:20 +0300 Subject: [PATCH] add_account and status --- console/add_account.go | 86 ++++++++++++++++++++++++++++++++++++++++++ console/command.go | 18 ++++++--- console/status.go | 54 ++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 console/add_account.go create mode 100644 console/status.go diff --git a/console/add_account.go b/console/add_account.go new file mode 100644 index 000000000..10a6345f2 --- /dev/null +++ b/console/add_account.go @@ -0,0 +1,86 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +import ( + "encoding/json" + "fmt" + "reflect" + + "github.com/cgrates/cgrates/apier" + "github.com/cgrates/cgrates/engine" +) + +func init() { + commands["add_account"] = &CmdAddAccount{ + rpcMethod: "ApierV1.SetAccount", + } +} + +// Commander implementation +type CmdAddAccount struct { + rpcMethod string + rpcParams *apier.AttrSetAccount + rpcResult string +} + +func (self *CmdAddAccount) Usage() string { + jsn, _ := json.Marshal(engine.CallDescriptor{Direction: "*out"}) + return "\n\tUsage: add_account " + FromJSON(jsn, self.ClientArgs()) + "\n" +} + +// Parses command line args and builds CmdBalance value +func (self *CmdAddAccount) FromArgs(args string, verbose bool) error { + if len(args) == 0 { + return fmt.Errorf(self.Usage()) + } + // defaults + self.rpcParams = &apier.AttrSetAccount{Direction: "*out"} + + if err := json.Unmarshal(ToJSON(args), &self.rpcParams); err != nil { + return err + } + if verbose { + jsn, _ := json.Marshal(self.rpcParams) + fmt.Println("add_account ", FromJSON(jsn, self.ClientArgs())) + } + return nil +} + +func (self *CmdAddAccount) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdAddAccount) RpcParams() interface{} { + return self.rpcParams +} + +func (self *CmdAddAccount) RpcResult() interface{} { + return &self.rpcResult +} + +func (self *CmdAddAccount) ClientArgs() (args []string) { + val := reflect.ValueOf(apier.AttrSetAccount{}).Elem() + + for i := 0; i < val.NumField(); i++ { + typeField := val.Type().Field(i) + args = append(args, typeField.Name) + } + return +} diff --git a/console/command.go b/console/command.go index 3483c15b1..e9346fafd 100644 --- a/console/command.go +++ b/console/command.go @@ -57,16 +57,24 @@ func getAvailabelCommandsErr() error { // Process args and return right command Value or error func GetCommandValue(command string, verbose bool) (Commander, error) { - - firstSpace := strings.Index(command, " ") - if firstSpace < 0 { + if len(command) == 0 { return nil, getAvailabelCommandsErr() } - cmdVal, exists := commands[command[:firstSpace]] + firstSpace := strings.Index(command, " ") + var cmdName string + var cmdArgs string + if firstSpace <= 0 { + cmdName = command[:len(command)] + cmdArgs = "" + } else { + cmdName = command[:firstSpace] + cmdArgs = command[firstSpace+1:] + } + cmdVal, exists := commands[cmdName] if !exists { return nil, getAvailabelCommandsErr() } - if err := cmdVal.FromArgs(command[firstSpace+1:], verbose); err != nil { + if err := cmdVal.FromArgs(cmdArgs, verbose); err != nil { return nil, err } return cmdVal, nil diff --git a/console/status.go b/console/status.go new file mode 100644 index 000000000..cf3ccab97 --- /dev/null +++ b/console/status.go @@ -0,0 +1,54 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +func init() { + commands["status"] = &CmdStatus{rpcMethod: "Responder.Status"} +} + +type CmdStatus struct { + rpcMethod string + rpcParams string + rpcResult string +} + +func (self *CmdStatus) Usage() string { + return "\n\tUsage: status \n" +} + +// Parses command line args and builds CmdBalance value +func (self *CmdStatus) FromArgs(args string, verbose bool) error { + 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 +} + +func (self *CmdStatus) ClientArgs() (args []string) { + return +}