add(new command schema)
This commit is contained in:
1 parent
fcafab05d1
commit
f32d9a80b0
16 files changed
+306
-77
No files matched your search
+19
-14
@@ -2,6 +2,7 @@ package command
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/integration-system/isp-lib/http"
|
||||
@@ -17,23 +18,23 @@ func checkFlags(c *cli.Context) error {
|
||||
uuid, host string
|
||||
color bool
|
||||
)
|
||||
colorFlag := strings.Split(flag.ColorName, ", ")
|
||||
colorFlag := strings.Split(flag.Color.Name, ", ")
|
||||
if c.GlobalBool(colorFlag[0]) != false {
|
||||
color = true
|
||||
} else {
|
||||
color = c.GlobalBool(colorFlag[1])
|
||||
}
|
||||
service.ColorService.Enable = color
|
||||
service.UnsafeService.Enable = c.GlobalBool(flag.UnsafeName)
|
||||
service.UnsafeService.Enable = c.GlobalBool(flag.Unsafe.Name)
|
||||
|
||||
hostFlag := strings.Split(flag.GateHostName, ", ")
|
||||
hostFlag := strings.Split(flag.Host.Name, ", ")
|
||||
if c.GlobalString(hostFlag[0]) != "" {
|
||||
host = c.GlobalString(hostFlag[0])
|
||||
} else {
|
||||
host = c.GlobalString(hostFlag[1])
|
||||
}
|
||||
|
||||
uuidFlag := strings.Split(flag.InstanceUuidName, ", ")
|
||||
uuidFlag := strings.Split(flag.Uuid.Name, ", ")
|
||||
if c.GlobalString(uuidFlag[0]) != "" {
|
||||
uuid = c.GlobalString(uuidFlag[0])
|
||||
} else {
|
||||
@@ -60,7 +61,7 @@ func checkPath(pathObject string) (string, bool) {
|
||||
str := strings.Split(pathObject, ".")
|
||||
pathObject = ""
|
||||
if len(str) == 1 || str[0] != "" {
|
||||
fmt.Println("Path must start with '.'")
|
||||
printError(errors.New("path must start with '.'"))
|
||||
return pathObject, false
|
||||
}
|
||||
for key, value := range str {
|
||||
@@ -78,7 +79,7 @@ func checkPath(pathObject string) (string, bool) {
|
||||
|
||||
func printAnswer(data interface{}) {
|
||||
if answer, err := json.MarshalIndent(data, "", " "); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
} else {
|
||||
service.ColorService.Print(answer)
|
||||
}
|
||||
@@ -86,7 +87,7 @@ func printAnswer(data interface{}) {
|
||||
|
||||
func getModuleConfiguration(moduleName string) (*cfg.Config, []byte) {
|
||||
if moduleName == "" {
|
||||
fmt.Println("Need module name. Use 'info' for get module names")
|
||||
printError(errors.New("need module name. Use 'schema' for get module names"))
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -94,18 +95,18 @@ func getModuleConfiguration(moduleName string) (*cfg.Config, []byte) {
|
||||
if err != nil {
|
||||
if errorResponse, ok := err.(http.ErrorResponse); ok {
|
||||
if errorResponse.StatusCode == fasthttp.StatusNotFound {
|
||||
fmt.Printf("Module '%s' not found\n", moduleName)
|
||||
printError(errors.New(fmt.Sprintf("module '%s' not found\n", moduleName)))
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if jsonObject, err := json.Marshal(moduleConfiguration.Data); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return nil, nil
|
||||
} else {
|
||||
return moduleConfiguration, jsonObject
|
||||
@@ -116,13 +117,13 @@ 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)
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if ok, err := service.UnsafeService.CheckConfigurationSchema(configuration.ModuleId, newData); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
} else if !ok {
|
||||
return
|
||||
@@ -130,8 +131,12 @@ func createUpdateConfig(stringToChange string, configuration *cfg.Config) {
|
||||
|
||||
configuration.Data = newData
|
||||
if resp, err := service.ConfigClient.CreateUpdateConfig(*configuration); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
} else {
|
||||
printAnswer(resp.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func printError(err error) {
|
||||
fmt.Println("ERROR", err)
|
||||
}
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
@@ -20,7 +19,7 @@ type deleteCommand struct{}
|
||||
|
||||
func (d deleteCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
@@ -40,7 +39,7 @@ func (d deleteCommand) action(c *cli.Context) {
|
||||
createUpdateConfig("", moduleConfiguration)
|
||||
} else {
|
||||
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
} else {
|
||||
createUpdateConfig(stringToChange, moduleConfiguration)
|
||||
|
||||
+4
-3
@@ -2,6 +2,7 @@ package command
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -21,7 +22,7 @@ type getCommand struct{}
|
||||
|
||||
func (g getCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
@@ -47,11 +48,11 @@ func (g getCommand) action(c *cli.Context) {
|
||||
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)
|
||||
printError(errors.New(fmt.Sprintf("Path '%s' not found\n", depth)))
|
||||
} else {
|
||||
var data interface{}
|
||||
if err := json.Unmarshal([]byte(jsonString.Raw), &data); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
} else {
|
||||
printAnswer(data)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package command
|
||||
|
||||
const htmlTemplate = `<html>
|
||||
<head>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/json-schema-view-js@2.0.1/dist/bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/json-schema-view-js@2.0.1/dist/style.min.css">
|
||||
<style>
|
||||
.json-schema-view-dark {
|
||||
background: #222;
|
||||
}
|
||||
h3 + div > div {
|
||||
padding: 1em;
|
||||
}
|
||||
pre {
|
||||
background: #333;
|
||||
color: white;
|
||||
padding: 10px;
|
||||
font-size: 1.2em;
|
||||
line-height: 1.3;
|
||||
}
|
||||
/*
|
||||
* Page layout
|
||||
*/
|
||||
body {
|
||||
max-width: 80vw;
|
||||
margin: 30px auto;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
header {
|
||||
overflow: hidden;
|
||||
}
|
||||
header h1 {
|
||||
float: left;
|
||||
}
|
||||
header > div {
|
||||
text-align: right;
|
||||
padding: 10px;
|
||||
}
|
||||
header > div * {
|
||||
vertical-align: middle;
|
||||
}
|
||||
.code-in-gh {
|
||||
background: steelblue;
|
||||
color: white;
|
||||
padding: .5em;
|
||||
border-radius: 5px;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="results"></div>
|
||||
<script>
|
||||
var schema = [
|
||||
{{ . }}
|
||||
];
|
||||
var results = document.querySelector('.results');
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
schema.forEach(function (example) {
|
||||
var title = document.createElement('h3');
|
||||
var formatter = new JSONSchemaView(example.schema, 1, { theme: example.theme });
|
||||
title.innerText = example.title;
|
||||
results.appendChild(title)
|
||||
results.appendChild(formatter.render());
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
@@ -0,0 +1,73 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"html/template"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Schema() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "schema",
|
||||
Usage: "get schema configuration by module_name",
|
||||
Action: schema.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrint,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var schema schemaCommand
|
||||
|
||||
type schemaCommand struct{}
|
||||
|
||||
func (s schemaCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
|
||||
if schemaConfig := s.getSchemaConfig(moduleName); schemaConfig != nil {
|
||||
schema := make(map[string]interface{})
|
||||
schema["title"] = moduleName
|
||||
schema["schema"] = schemaConfig
|
||||
|
||||
switch c.String(flag.OutPrint.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
printAnswer(schema)
|
||||
case flag.OutPrintHtmlValue:
|
||||
s.printHtml(schema)
|
||||
default:
|
||||
printError(errors.New(fmt.Sprintf(
|
||||
"invalid flag value, expected %s or %s", flag.OutPrintJsonValue, flag.OutPrintHtmlValue)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s schemaCommand) getSchemaConfig(moduleName string) interface{} {
|
||||
if configuration, _ := getModuleConfiguration(moduleName); configuration == nil {
|
||||
return nil
|
||||
} else if schema, err := service.ConfigClient.GetSchemaByModuleId(configuration.ModuleId); err != nil {
|
||||
printError(err)
|
||||
return nil
|
||||
} else {
|
||||
return schema.Schema
|
||||
}
|
||||
}
|
||||
|
||||
func (s schemaCommand) printHtml(schema map[string]interface{}) {
|
||||
if temp, err := template.New("template").Parse(htmlTemplate); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
} else {
|
||||
if err := temp.ExecuteTemplate(os.Stdout, "template", schema); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/sjson"
|
||||
@@ -23,7 +24,7 @@ type setCommand struct{}
|
||||
|
||||
func (s setCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,7 +50,7 @@ func (s setCommand) action(c *cli.Context) {
|
||||
}
|
||||
|
||||
if changeObject == "" {
|
||||
fmt.Println("Error. Expected argument")
|
||||
printError(errors.New("expected argument"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -59,7 +60,7 @@ func (s setCommand) action(c *cli.Context) {
|
||||
} else {
|
||||
changeArgument := s.parseObject(changeObject)
|
||||
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
} else {
|
||||
createUpdateConfig(stringToChange, moduleConfiguration)
|
||||
|
||||
+2
-2
@@ -31,11 +31,11 @@ type statusCommand struct{}
|
||||
|
||||
func (statusCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
if arrayOfModules, err := service.ConfigClient.GetAvailableConfigs(); err != nil {
|
||||
fmt.Println(err)
|
||||
printError(err)
|
||||
} else {
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
|
||||
|
||||
Reference in new issue
Block a user