fix(bash complete)
This commit is contained in:
1 parent
6155ebf062
commit
d1558438cf
18 files changed
+305
-261
No files matched your search
@@ -1,51 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/integration-system/bellows"
|
||||
"isp-ctl/service"
|
||||
)
|
||||
|
||||
var (
|
||||
bash = bashAction{enableConfigurationBash: true}
|
||||
bashSchema = bashAction{enableConfigurationBash: false}
|
||||
)
|
||||
|
||||
type (
|
||||
bashAction struct{ enableConfigurationBash bool }
|
||||
)
|
||||
|
||||
func (b bashAction) run(ctx *cli.Context) {
|
||||
if err := checkFlags(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
switch ctx.NArg() {
|
||||
case 0:
|
||||
b.moduleNameHelper()
|
||||
case 1:
|
||||
if b.enableConfigurationBash {
|
||||
b.configurationHelper(ctx.Args().First(), ctx.Args().Get(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (bashAction) moduleNameHelper() {
|
||||
if arrayOfModules, err := service.ConfigClient.GetAvailableConfigs(); err != nil {
|
||||
return
|
||||
} else {
|
||||
for _, module := range arrayOfModules {
|
||||
fmt.Println(module.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (bashAction) configurationHelper(moduleName, path string) {
|
||||
moduleConfiguration, _ := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
return
|
||||
}
|
||||
for key, _ := range bellows.Flatten(moduleConfiguration.Data) {
|
||||
fmt.Printf(".%v\n", key)
|
||||
}
|
||||
}
|
||||
+12
-105
@@ -4,65 +4,16 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"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.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.Unsafe.Name)
|
||||
|
||||
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.Uuid.Name, ", ")
|
||||
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 checkPath(pathObject string) (string, bool) {
|
||||
func checkPath(pathObject string) (string, error) {
|
||||
str := strings.Split(pathObject, ".")
|
||||
pathObject = ""
|
||||
if len(str) == 1 || str[0] != "" {
|
||||
printError(errors.New("path must start with '.'"))
|
||||
return pathObject, false
|
||||
return pathObject, errors.New("path must start with '.'")
|
||||
}
|
||||
for key, value := range str {
|
||||
if key == 0 {
|
||||
@@ -74,7 +25,15 @@ func checkPath(pathObject string) (string, bool) {
|
||||
}
|
||||
pathObject = fmt.Sprintf("%s.%s", pathObject, value)
|
||||
}
|
||||
return pathObject, true
|
||||
return pathObject, nil
|
||||
}
|
||||
|
||||
func createUpdateConfig(stringToChange string, configuration *cfg.Config) {
|
||||
if answer, err := service.Config.CreateUpdateConfig(stringToChange, configuration); err != nil {
|
||||
printError(err)
|
||||
} else if answer != nil {
|
||||
printAnswer(answer)
|
||||
}
|
||||
}
|
||||
|
||||
func printAnswer(data interface{}) {
|
||||
@@ -85,58 +44,6 @@ func printAnswer(data interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func getModuleConfiguration(moduleName string) (*cfg.Config, []byte) {
|
||||
if moduleName == "" {
|
||||
printError(errors.New("need module name. Use 'schema' 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 {
|
||||
printError(errors.New(fmt.Sprintf("module '%s' not found\n", moduleName)))
|
||||
} else {
|
||||
printError(err)
|
||||
}
|
||||
} else {
|
||||
printError(err)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if jsonObject, err := json.Marshal(moduleConfiguration.Data); err != nil {
|
||||
printError(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 {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if ok, err := service.UnsafeService.CheckConfigurationSchema(configuration.ModuleId, newData); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
} else if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
configuration.Data = newData
|
||||
if resp, err := service.ConfigClient.CreateUpdateConfig(*configuration); err != nil {
|
||||
printError(err)
|
||||
} else {
|
||||
printAnswer(resp.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func printError(err error) {
|
||||
fmt.Println("ERROR", err)
|
||||
fmt.Println("ERROR:", err)
|
||||
}
|
||||
+14
-9
@@ -3,6 +3,9 @@ package command
|
||||
import (
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/sjson"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
)
|
||||
|
||||
func Delete() cli.Command {
|
||||
@@ -10,7 +13,7 @@ func Delete() cli.Command {
|
||||
Name: "delete",
|
||||
Usage: "delete configuration by module_name",
|
||||
Action: deleteComm.action,
|
||||
BashComplete: bash.run,
|
||||
BashComplete: bash.ModuleNameAndConfigurationPath.Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,21 +21,23 @@ var deleteComm deleteCommand
|
||||
|
||||
type deleteCommand struct{}
|
||||
|
||||
func (d deleteCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
func (d deleteCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
pathObject := c.Args().Get(1)
|
||||
moduleName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
moduleConfiguration, jsonObject := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
moduleConfiguration, jsonObject, err := service.Config.GetConfigurationAndJsonByModuleName(moduleName)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
pathObject, ok := checkPath(pathObject)
|
||||
if !ok {
|
||||
pathObject, err = checkPath(pathObject)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+14
-9
@@ -6,6 +6,9 @@ import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/gjson"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
)
|
||||
|
||||
func Get() cli.Command {
|
||||
@@ -13,7 +16,7 @@ func Get() cli.Command {
|
||||
Name: "get",
|
||||
Usage: "get configuration by module_name",
|
||||
Action: get.action,
|
||||
BashComplete: bash.run,
|
||||
BashComplete: bash.ModuleNameAndConfigurationPath.Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,21 +24,23 @@ var get getCommand
|
||||
|
||||
type getCommand struct{}
|
||||
|
||||
func (g getCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
func (g getCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
pathObject := c.Args().Get(1)
|
||||
moduleName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
|
||||
moduleConfiguration, jsonObject := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
moduleConfiguration, jsonObject, err := service.Config.GetConfigurationAndJsonByModuleName(moduleName)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
pathObject, ok := checkPath(pathObject)
|
||||
if !ok {
|
||||
pathObject, err = checkPath(pathObject)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
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>
|
||||
`
|
||||
+12
-10
@@ -4,10 +4,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
convert "github.com/integration-system/isp-lib/config/schema"
|
||||
"html/template"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"isp-ctl/tmpl"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -19,7 +20,7 @@ func Schema() cli.Command {
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrint,
|
||||
},
|
||||
BashComplete: bashSchema.run,
|
||||
BashComplete: bash.ModuleName.Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,18 +28,18 @@ var schema schemaCommand
|
||||
|
||||
type schemaCommand struct{}
|
||||
|
||||
func (s schemaCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
func (s schemaCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
moduleName := c.Args().First()
|
||||
moduleName := ctx.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) {
|
||||
switch ctx.String(flag.OutPrint.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
printAnswer(schema)
|
||||
case flag.OutPrintHtmlValue:
|
||||
@@ -51,18 +52,19 @@ func (s schemaCommand) action(c *cli.Context) {
|
||||
}
|
||||
|
||||
func (s schemaCommand) getSchemaConfig(moduleName string) interface{} {
|
||||
if configuration, _ := getModuleConfiguration(moduleName); configuration == nil {
|
||||
if configuration, _, err := service.Config.GetConfigurationAndJsonByModuleName(moduleName); err != nil {
|
||||
printError(err)
|
||||
return nil
|
||||
} else if schema, err := service.ConfigClient.GetSchemaByModuleId(configuration.ModuleId); err != nil {
|
||||
} else if schema, err := service.Config.GetSchemaByModuleId(configuration.ModuleId); err != nil {
|
||||
printError(err)
|
||||
return nil
|
||||
} else {
|
||||
return convert.DereferenceSchema(schema.Schema)
|
||||
return schema
|
||||
}
|
||||
}
|
||||
|
||||
func (s schemaCommand) printHtml(schema map[string]interface{}) {
|
||||
if temp, err := template.New("template").Parse(htmlTemplate); err != nil {
|
||||
if temp, err := template.New("template").Parse(tmpl.HtmlFile); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
} else {
|
||||
|
||||
+15
-10
@@ -7,6 +7,9 @@ import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/tidwall/sjson"
|
||||
"isp-ctl/bash"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -15,7 +18,7 @@ func Set() cli.Command {
|
||||
Name: "set",
|
||||
Usage: "set configuration by module_name",
|
||||
Action: set.action,
|
||||
BashComplete: bash.run,
|
||||
BashComplete: bash.ModuleNameAndConfigurationPath.Complete,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,23 +26,25 @@ var set setCommand
|
||||
|
||||
type setCommand struct{}
|
||||
|
||||
func (s setCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
func (s setCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
moduleName := c.Args().First()
|
||||
pathObject := c.Args().Get(1)
|
||||
changeObject := c.Args().Get(2)
|
||||
moduleName := ctx.Args().First()
|
||||
pathObject := ctx.Args().Get(1)
|
||||
changeObject := ctx.Args().Get(2)
|
||||
|
||||
moduleConfiguration, jsonObject := getModuleConfiguration(moduleName)
|
||||
if moduleConfiguration == nil {
|
||||
moduleConfiguration, jsonObject, err := service.Config.GetConfigurationAndJsonByModuleName(moduleName)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
pathObject, ok := checkPath(pathObject)
|
||||
if !ok {
|
||||
pathObject, err = checkPath(pathObject)
|
||||
if err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"isp-ctl/flag"
|
||||
"isp-ctl/service"
|
||||
"os"
|
||||
)
|
||||
@@ -29,12 +30,12 @@ var status statusCommand
|
||||
|
||||
type statusCommand struct{}
|
||||
|
||||
func (statusCommand) action(c *cli.Context) {
|
||||
if err := checkFlags(c); err != nil {
|
||||
func (statusCommand) action(ctx *cli.Context) {
|
||||
if err := flag.CheckGlobal(ctx); err != nil {
|
||||
printError(err)
|
||||
return
|
||||
}
|
||||
if arrayOfModules, err := service.ConfigClient.GetAvailableConfigs(); err != nil {
|
||||
if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil {
|
||||
printError(err)
|
||||
} else {
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
|
||||
Reference in new issue
Block a user