mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
polymorphic factory
This commit is contained in:
@@ -6,6 +6,10 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
commands["get_balance"] = &CmdGetBalance{}
|
||||
}
|
||||
|
||||
// Data being sent to the rpc responder
|
||||
type ArgsGetBalance struct {
|
||||
Tenant string
|
||||
@@ -25,10 +29,9 @@ type ReplyGetBalance struct {
|
||||
|
||||
// Commander implementation
|
||||
type CmdGetBalance struct {
|
||||
rpcMethod string
|
||||
rpcParams *ArgsGetBalance
|
||||
rpcResult *ReplyGetBalance
|
||||
idxArgsToRpcPrms map[int]string
|
||||
rpcMethod string
|
||||
rpcParams *ArgsGetBalance
|
||||
rpcResult *ReplyGetBalance
|
||||
}
|
||||
|
||||
// name should be exec's name
|
||||
@@ -38,9 +41,8 @@ func (self *CmdGetBalance) Usage(name string) string {
|
||||
|
||||
// set param defaults
|
||||
func (self *CmdGetBalance) defaults() error {
|
||||
self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceId", 5: "Direction"}
|
||||
self.rpcMethod = "Responder.GetBalance"
|
||||
self.rpcParams = &ArgsGetBalance{BalanceId: "MONETARY", Direction: "OUT"}
|
||||
self.rpcParams = &ArgsGetBalance{BalanceId: "MONETARYOUT", Direction: "OUT"}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -51,11 +53,13 @@ func (self *CmdGetBalance) FromArgs(args []string) error {
|
||||
}
|
||||
// Args look OK, set defaults before going further
|
||||
self.defaults()
|
||||
self.rpcParams = &ArgsGetBalance{
|
||||
Tenant: args[2],
|
||||
User: args[3],
|
||||
BalanceId: args[4],
|
||||
Direction: args[5],
|
||||
self.rpcParams.Tenant = args[2]
|
||||
self.rpcParams.User = args[3]
|
||||
if len(args) > 4 {
|
||||
self.rpcParams.BalanceId = args[4]
|
||||
}
|
||||
if len(args) > 5 {
|
||||
self.rpcParams.Direction = args[5]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
commands = make(map[string]Commander)
|
||||
)
|
||||
|
||||
// Console Command interface
|
||||
type Commander interface {
|
||||
FromArgs(args []string) error // Load data from os arguments or flag.Args()
|
||||
@@ -20,14 +24,8 @@ func GetCommandValue(args []string) (Commander, error) {
|
||||
if len(args) < 2 {
|
||||
return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] <command>\n", filepath.Base(args[0]))
|
||||
}
|
||||
cmd := args[1]
|
||||
var cmdVal Commander
|
||||
switch cmd {
|
||||
case "status":
|
||||
cmdVal = &CmdStatus{}
|
||||
case "get_balance":
|
||||
cmdVal = &CmdGetBalance{}
|
||||
default:
|
||||
cmdVal, exists := commands[args[1]]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] <status|get_balance>\n", filepath.Base(args[0]))
|
||||
}
|
||||
if err := cmdVal.FromArgs(args); err != nil {
|
||||
|
||||
@@ -4,6 +4,10 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
commands["status"] = &CmdStatus{}
|
||||
}
|
||||
|
||||
type CmdStatus struct {
|
||||
rpcMethod string
|
||||
rpcParams string
|
||||
|
||||
Reference in New Issue
Block a user