mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
introducing command executer
This commit is contained in:
@@ -18,48 +18,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/cgrates/cgrates/apier"
|
||||
)
|
||||
import "github.com/cgrates/cgrates/apier"
|
||||
|
||||
func init() {
|
||||
commands["add_account"] = &CmdAddAccount{
|
||||
c := &CmdAddAccount{
|
||||
name: "add_account",
|
||||
rpcMethod: "ApierV1.SetAccount",
|
||||
}
|
||||
commands[c.Name()] = c
|
||||
c.CommandExecuter = &CommandExecuter{c}
|
||||
}
|
||||
|
||||
// Commander implementation
|
||||
type CmdAddAccount struct {
|
||||
name string
|
||||
rpcMethod string
|
||||
rpcParams *apier.AttrSetAccount
|
||||
rpcResult string
|
||||
*CommandExecuter
|
||||
}
|
||||
|
||||
func (self *CmdAddAccount) Usage() string {
|
||||
jsn, _ := json.Marshal(apier.AttrSetAccount{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) Name() string {
|
||||
return self.name
|
||||
}
|
||||
|
||||
func (self *CmdAddAccount) RpcMethod() string {
|
||||
@@ -73,13 +53,3 @@ func (self *CmdAddAccount) RpcParams() interface{} {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -19,49 +19,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package console
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/cgrates/cgrates/apier"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
)
|
||||
|
||||
func init() {
|
||||
commands["add_balance"] = &CmdAddBalance{
|
||||
c := &CmdAddBalance{
|
||||
name: "add_balance",
|
||||
rpcMethod: "ApierV1.AddBalance",
|
||||
rpcParams: &apier.AttrAddBalance{BalanceType: engine.CREDIT},
|
||||
}
|
||||
commands[c.Name()] = c
|
||||
c.CommandExecuter = &CommandExecuter{c}
|
||||
}
|
||||
|
||||
// Commander implementation
|
||||
type CmdAddBalance struct {
|
||||
name string
|
||||
rpcMethod string
|
||||
rpcParams *apier.AttrAddBalance
|
||||
rpcResult string
|
||||
*CommandExecuter
|
||||
}
|
||||
|
||||
func (self *CmdAddBalance) Usage() string {
|
||||
jsn, _ := json.Marshal(apier.AttrAddBalance{Direction: "*out"})
|
||||
return "\n\tUsage: add_balance " + FromJSON(jsn, self.ClientArgs()) + "\n"
|
||||
}
|
||||
|
||||
// Parses command line args and builds CmdBalance value
|
||||
func (self *CmdAddBalance) FromArgs(args string, verbose bool) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf(self.Usage())
|
||||
}
|
||||
// defaults
|
||||
self.rpcParams = &apier.AttrAddBalance{Direction: "*out"}
|
||||
|
||||
if err := json.Unmarshal(ToJSON(args), &self.rpcParams); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
jsn, _ := json.Marshal(self.rpcParams)
|
||||
fmt.Println("add_balance ", FromJSON(jsn, self.ClientArgs()))
|
||||
}
|
||||
return nil
|
||||
func (self *CmdAddBalance) Name() string {
|
||||
return self.name
|
||||
}
|
||||
|
||||
func (self *CmdAddBalance) RpcMethod() string {
|
||||
@@ -75,13 +57,3 @@ func (self *CmdAddBalance) RpcParams() interface{} {
|
||||
func (self *CmdAddBalance) RpcResult() interface{} {
|
||||
return &self.rpcResult
|
||||
}
|
||||
|
||||
func (self *CmdAddBalance) ClientArgs() (args []string) {
|
||||
val := reflect.ValueOf(&apier.AttrAddBalance{}).Elem()
|
||||
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
typeField := val.Type().Field(i)
|
||||
args = append(args, typeField.Name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ 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,
|
||||
@@ -41,6 +42,7 @@ type Commander interface {
|
||||
RpcParams() interface{} // Parameters to send out on rpc
|
||||
RpcResult() interface{} // Only requirement is to have a String method to print on console
|
||||
ClientArgs() []string // for autocompletion
|
||||
Name() string
|
||||
}
|
||||
|
||||
func GetCommands() map[string]Commander {
|
||||
|
||||
63
console/command_executer.go
Normal file
63
console/command_executer.go
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/cgrates/cgrates/apier"
|
||||
)
|
||||
|
||||
// Commander implementation
|
||||
type CommandExecuter struct {
|
||||
command Commander
|
||||
}
|
||||
|
||||
func (ce *CommandExecuter) Usage() string {
|
||||
jsn, _ := json.Marshal(ce.command.RpcParams())
|
||||
return fmt.Sprintf("\n\tUsage: %s %s \n", ce.command.Name(), FromJSON(jsn, ce.command.ClientArgs()))
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
if verbose {
|
||||
jsn, _ := json.Marshal(ce.command.RpcParams())
|
||||
fmt.Println(ce.command.Name(), FromJSON(jsn, ce.command.ClientArgs()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ce *CommandExecuter) ClientArgs() (args []string) {
|
||||
val := reflect.ValueOf(&apier.AttrAddBalance{}).Elem()
|
||||
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
typeField := val.Type().Field(i)
|
||||
args = append(args, typeField.Name)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -18,49 +18,30 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
)
|
||||
import "github.com/cgrates/cgrates/engine"
|
||||
|
||||
func init() {
|
||||
commands["get_cost"] = &CmdGetCost{
|
||||
c := &CmdGetCost{
|
||||
name: "get_cost",
|
||||
rpcMethod: "Responder.GetCost",
|
||||
clientArgs: []string{"Direction", "TOR", "Tenant", "Subject", "Account", "Destination", "TimeStart", "TimeEnd", "CallDuration", "FallbackSubject"},
|
||||
}
|
||||
commands[c.Name()] = c
|
||||
c.CommandExecuter = &CommandExecuter{c}
|
||||
}
|
||||
|
||||
// Commander implementation
|
||||
type CmdGetCost struct {
|
||||
name string
|
||||
rpcMethod string
|
||||
rpcParams *engine.CallDescriptor
|
||||
rpcResult engine.CallCost
|
||||
clientArgs []string
|
||||
*CommandExecuter
|
||||
}
|
||||
|
||||
func (self *CmdGetCost) Usage() string {
|
||||
jsn, _ := json.Marshal(engine.CallDescriptor{Direction: "*out"})
|
||||
return "\n\tUsage: get_cost " + FromJSON(jsn, self.clientArgs) + "\n"
|
||||
}
|
||||
|
||||
// Parses command line args and builds CmdBalance value
|
||||
func (self *CmdGetCost) FromArgs(args string, verbose bool) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf(self.Usage())
|
||||
}
|
||||
// defaults
|
||||
self.rpcParams = &engine.CallDescriptor{Direction: "*out"}
|
||||
|
||||
if err := json.Unmarshal(ToJSON(args), &self.rpcParams); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
jsn, _ := json.Marshal(self.rpcParams)
|
||||
fmt.Println("get_cost ", FromJSON(jsn, self.clientArgs))
|
||||
}
|
||||
return nil
|
||||
func (self *CmdGetCost) Name() string {
|
||||
return self.name
|
||||
}
|
||||
|
||||
func (self *CmdGetCost) RpcMethod() string {
|
||||
@@ -68,6 +49,9 @@ func (self *CmdGetCost) RpcMethod() string {
|
||||
}
|
||||
|
||||
func (self *CmdGetCost) RpcParams() interface{} {
|
||||
if self.rpcParams == nil {
|
||||
self.rpcParams = &engine.CallDescriptor{Direction: "*out"}
|
||||
}
|
||||
return self.rpcParams
|
||||
}
|
||||
|
||||
|
||||
@@ -19,22 +19,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package console
|
||||
|
||||
func init() {
|
||||
commands["status"] = &CmdStatus{rpcMethod: "Responder.Status"}
|
||||
c := &CmdStatus{
|
||||
name: "status",
|
||||
rpcMethod: "Responder.Status",
|
||||
}
|
||||
commands[c.Name()] = c
|
||||
c.CommandExecuter = &CommandExecuter{c}
|
||||
}
|
||||
|
||||
type CmdStatus struct {
|
||||
name string
|
||||
rpcMethod string
|
||||
rpcParams string
|
||||
rpcResult string
|
||||
*CommandExecuter
|
||||
}
|
||||
|
||||
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) Name() string {
|
||||
return self.name
|
||||
}
|
||||
|
||||
func (self *CmdStatus) RpcMethod() string {
|
||||
|
||||
Reference in New Issue
Block a user