Init
This commit is contained in:
1 parent
79e240037f
commit
9078060e65
20 files changed
+1152
No files matched your search
@@ -0,0 +1,110 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/integration-system/isp-lib/http"
|
||||
"github.com/valyala/fasthttp"
|
||||
"isp-ctl/cfg"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func checkFlags(c *cli.Context) error {
|
||||
var (
|
||||
uuid, host string
|
||||
color bool
|
||||
)
|
||||
colorFlag := strings.Split(flag.ColorName, ", ")
|
||||
if c.GlobalBool(colorFlag[0]) != false {
|
||||
color = true
|
||||
} else {
|
||||
color = c.GlobalBool(colorFlag[1])
|
||||
}
|
||||
service.ColorService.Enable = color
|
||||
|
||||
service.ColorService.Print(nil)
|
||||
|
||||
hostFlag := strings.Split(flag.GateHostName, ", ")
|
||||
if c.GlobalString(hostFlag[0]) != "" {
|
||||
host = c.GlobalString(hostFlag[0])
|
||||
} else {
|
||||
host = c.GlobalString(hostFlag[1])
|
||||
}
|
||||
|
||||
uuidFlag := strings.Split(flag.InstanceUuidName, ", ")
|
||||
if c.GlobalString(uuidFlag[0]) != "" {
|
||||
uuid = c.GlobalString(uuidFlag[0])
|
||||
} else {
|
||||
uuid = c.GlobalString(uuidFlag[1])
|
||||
}
|
||||
|
||||
if uuid == "" || host == "" {
|
||||
if configuration, err := service.YamlService.Parse(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if uuid == "" {
|
||||
uuid = configuration.InstanceUuid
|
||||
}
|
||||
if host == "" {
|
||||
host = configuration.GateHost
|
||||
}
|
||||
}
|
||||
}
|
||||
service.ConfigService.ReceiveConfiguration(host, uuid)
|
||||
return nil
|
||||
}
|
||||
|
||||
func printAnswer(data interface{}) {
|
||||
if answer, err := json.MarshalIndent(data, "", " "); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
service.ColorService.Print(answer)
|
||||
}
|
||||
}
|
||||
|
||||
func getModuleConfiguration(moduleName string) (*cfg.Config, []byte) {
|
||||
if moduleName == "" {
|
||||
fmt.Println("Need module name. Use 'info' for get module names")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
moduleConfiguration, err := service.ConfigClient.GetConfigByModuleName(moduleName)
|
||||
if err != nil {
|
||||
if errorResponse, ok := err.(http.ErrorResponse); ok {
|
||||
if errorResponse.StatusCode == fasthttp.StatusNotFound {
|
||||
fmt.Printf("Module '%s' not found\n", moduleName)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if jsonObject, err := json.Marshal(moduleConfiguration.Data); err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, nil
|
||||
} else {
|
||||
return moduleConfiguration, jsonObject
|
||||
}
|
||||
}
|
||||
|
||||
func createUpdateConfig(stringToChange string, configuration *cfg.Config) {
|
||||
newData := make(map[string]interface{})
|
||||
if stringToChange != "" {
|
||||
if err := json.Unmarshal([]byte(stringToChange), &newData); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
configuration.Data = newData
|
||||
if resp, err := service.ConfigClient.CreateUpdateConfig(*configuration); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
printAnswer(resp.Data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
func Delete() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "delete configuration info by module_name",
|
||||
Action: deleteComm.action,
|
||||
}
|
||||
}
|
||||
|
||||
var deleteComm deleteCommand
|
||||
|
||||
type deleteCommand struct{}
|
||||
|
||||
func (d deleteCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
pathObject := c.Args().Get(1)
|
||||
|
||||
moduleConfiguration, jsonObject := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
createUpdateConfig("", moduleConfiguration)
|
||||
} else {
|
||||
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
} else {
|
||||
createUpdateConfig(stringToChange, moduleConfiguration)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func Get() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "get",
|
||||
Usage: "get configuration info by module_name",
|
||||
Action: get.action,
|
||||
}
|
||||
}
|
||||
|
||||
var get getCommand
|
||||
|
||||
type getCommand struct{}
|
||||
|
||||
func (g getCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
pathObject := c.Args().Get(1)
|
||||
|
||||
moduleConfiguration, jsonObject := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
printAnswer(moduleConfiguration.Data)
|
||||
} else {
|
||||
g.checkObject(jsonObject, pathObject)
|
||||
}
|
||||
}
|
||||
|
||||
func (g getCommand) checkObject(jsonObject []byte, depth string) {
|
||||
jsonString := gjson.Get(string(jsonObject), depth)
|
||||
if jsonString.Raw == "" {
|
||||
fmt.Printf("Path '%s' not found\n", depth)
|
||||
} else {
|
||||
var data interface{}
|
||||
if err := json.Unmarshal([]byte(jsonString.Raw), &data); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
printAnswer(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
tableHeaderModuleName = "MODULE_NAME"
|
||||
tableHeaderStatus = "STATUS"
|
||||
tableHeaderAddresses = "ADDRESSES"
|
||||
|
||||
tableNotConnectedStatus = "NOT_CONNECTED"
|
||||
tableConnectedStatus = "CONNECTED"
|
||||
)
|
||||
|
||||
func Status() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "status",
|
||||
Usage: "get available configs",
|
||||
Action: status.action,
|
||||
}
|
||||
}
|
||||
|
||||
var status statusCommand
|
||||
|
||||
type statusCommand struct{}
|
||||
|
||||
func (statusCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
if arrayOfModules, err := service.ConfigClient.GetAvailableConfigs(); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
|
||||
table.SetHeader([]string{tableHeaderModuleName, tableHeaderStatus, tableHeaderAddresses})
|
||||
for _, module := range arrayOfModules {
|
||||
arrayOfAddress := ""
|
||||
connection := tableNotConnectedStatus
|
||||
for _, value := range module.Status {
|
||||
if arrayOfAddress == "" {
|
||||
arrayOfAddress = fmt.Sprintf("%s", value.Address.IP)
|
||||
} else {
|
||||
arrayOfAddress = fmt.Sprintf("%s, %s", arrayOfAddress, value.Address)
|
||||
}
|
||||
}
|
||||
if arrayOfAddress != "" {
|
||||
connection = tableConnectedStatus
|
||||
}
|
||||
table.Append([]string{module.Name, connection, arrayOfAddress})
|
||||
}
|
||||
table.Render()
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/sjson"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Set() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "set",
|
||||
Usage: "set configuration info",
|
||||
Action: set.action,
|
||||
}
|
||||
}
|
||||
|
||||
var set setCommand
|
||||
|
||||
type setCommand struct{}
|
||||
|
||||
func (s setCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
moduleName := c.Args().First()
|
||||
pathObject := c.Args().Get(1)
|
||||
changeObject := c.Args().Get(2)
|
||||
|
||||
moduleConfiguration, jsonObject := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if changeObject == "" {
|
||||
fmt.Print("Enter new value: ")
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
scanner.Scan()
|
||||
changeObject = scanner.Text()
|
||||
}
|
||||
|
||||
if changeObject == "" {
|
||||
fmt.Println("Error. Expected argument")
|
||||
return
|
||||
}
|
||||
|
||||
if pathObject == "" {
|
||||
createUpdateConfig(changeObject, moduleConfiguration)
|
||||
return
|
||||
} else {
|
||||
changeArgument := s.parseObject(changeObject)
|
||||
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
} else {
|
||||
createUpdateConfig(stringToChange, moduleConfiguration)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s setCommand) parseObject(argument string) interface{} {
|
||||
argument = strings.Replace(argument, " ", "", -1)
|
||||
tryParse := []byte(argument)
|
||||
if tryParse[0] == '"' && tryParse[len(tryParse)-1] == '"' {
|
||||
tryParse = tryParse[1 : len(tryParse)-1]
|
||||
return string(tryParse)
|
||||
}
|
||||
|
||||
if argument == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
mapStringInterface := make(map[string]interface{})
|
||||
if err := json.Unmarshal(tryParse, &mapStringInterface); err == nil {
|
||||
return mapStringInterface
|
||||
}
|
||||
|
||||
arrayOfObject := make([]interface{}, 0)
|
||||
if err := json.Unmarshal(tryParse, &arrayOfObject); err == nil {
|
||||
return arrayOfObject
|
||||
}
|
||||
|
||||
var integer int64
|
||||
if err := json.Unmarshal(tryParse, &integer); err == nil {
|
||||
return integer
|
||||
}
|
||||
|
||||
var float float64
|
||||
if err := json.Unmarshal(tryParse, &float); err == nil {
|
||||
return float
|
||||
}
|
||||
|
||||
var flag bool
|
||||
if err := json.Unmarshal(tryParse, &flag); err == nil {
|
||||
return flag
|
||||
}
|
||||
|
||||
return argument
|
||||
}
|
||||
Reference in new issue
Block a user