MSP-5014 Try get rid of global service and autocomplete
This commit is contained in:
1 parent
84f639a9d3
commit
c267b1ae6e
15 files changed
+468
-335
No files matched your search
+47
-37
@@ -2,72 +2,82 @@ package bash
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"ispctl/model"
|
||||||
|
"ispctl/service"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"ispctl/command/flag"
|
|
||||||
"ispctl/service"
|
|
||||||
|
|
||||||
"github.com/txix-open/bellows"
|
"github.com/txix-open/bellows"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CommonConfigName bashArg = "config_name"
|
CommonConfigName = "config_name"
|
||||||
ModuleName bashArg = "module_name"
|
ModuleName = "module_name"
|
||||||
ModuleData bashArg = "module_data"
|
ModuleData = "module_data"
|
||||||
|
|
||||||
VariableName bashArg = "variable_name"
|
VariableName = "variable_name"
|
||||||
Empty bashArg = "empty"
|
Empty = "empty"
|
||||||
)
|
)
|
||||||
|
|
||||||
type bashArg string
|
type BashService interface {
|
||||||
|
GetAllVariables() ([]model.Variable, error)
|
||||||
type bashCommand struct {
|
GetAvailableConfigs() ([]model.ModuleInfo, error)
|
||||||
first bashArg
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
second bashArg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get(first bashArg, second bashArg) *bashCommand {
|
type BeforeComplete func(ctx *cli.Context) (service.Config, error)
|
||||||
return &bashCommand{
|
|
||||||
first: first,
|
type Autocomplete struct {
|
||||||
second: second,
|
beforeComplete BeforeComplete
|
||||||
|
service BashService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAutocomplete(beforeComplete BeforeComplete) Autocomplete {
|
||||||
|
return Autocomplete{
|
||||||
|
beforeComplete: beforeComplete,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c bashCommand) Complete(ctx *cli.Context) {
|
func (c Autocomplete) Complete(first string, second string) cli.BashCompleteFunc {
|
||||||
err := flag.ApplyGlobalFlags(ctx)
|
return func(ctx *cli.Context) {
|
||||||
if err != nil {
|
cfgService, err := c.beforeComplete(ctx)
|
||||||
return
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.service = cfgService
|
||||||
|
c.complete(ctx, first, second)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Autocomplete) complete(ctx *cli.Context, first string, second string) {
|
||||||
switch ctx.NArg() {
|
switch ctx.NArg() {
|
||||||
case 0:
|
case 0:
|
||||||
c.completeFirstArg(ctx)
|
c.completeFirstArg(ctx, first)
|
||||||
case 1:
|
case 1:
|
||||||
c.completeSecondArg(ctx)
|
c.completeSecondArg(ctx, second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c bashCommand) completeFirstArg(ctx *cli.Context) {
|
func (c Autocomplete) completeFirstArg(ctx *cli.Context, first string) {
|
||||||
switch c.first {
|
switch first {
|
||||||
case ModuleName:
|
case ModuleName:
|
||||||
printAvailableModules()
|
c.printAvailableModules()
|
||||||
case VariableName:
|
case VariableName:
|
||||||
printAvailableVariables()
|
c.printAvailableVariables()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c bashCommand) completeSecondArg(ctx *cli.Context) {
|
func (c Autocomplete) completeSecondArg(ctx *cli.Context, second string) {
|
||||||
switch c.second {
|
switch second {
|
||||||
case ModuleName:
|
case ModuleName:
|
||||||
printAvailableModules()
|
c.printAvailableModules()
|
||||||
case ModuleData:
|
case ModuleData:
|
||||||
printModuleData(ctx.Args().First())
|
c.printModuleData(ctx.Args().First())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printAvailableVariables() {
|
func (c Autocomplete) printAvailableVariables() {
|
||||||
vars, err := service.Config.GetAllVariables()
|
vars, err := c.service.GetAllVariables()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -76,8 +86,8 @@ func printAvailableVariables() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printAvailableModules() {
|
func (c Autocomplete) printAvailableModules() {
|
||||||
modules, err := service.Config.GetAvailableConfigs()
|
modules, err := c.service.GetAvailableConfigs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -86,8 +96,8 @@ func printAvailableModules() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printModuleData(moduleName string) {
|
func (c Autocomplete) printModuleData(moduleName string) {
|
||||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
config, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-10
@@ -1,16 +1,45 @@
|
|||||||
package command
|
package command
|
||||||
|
|
||||||
import "github.com/urfave/cli/v2"
|
import (
|
||||||
|
"ispctl/model"
|
||||||
|
|
||||||
func AllCommands() []*cli.Command {
|
"github.com/txix-open/isp-kit/rc/schema"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AutoComplete interface {
|
||||||
|
Complete(first string, second string) cli.BashCompleteFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConfigService interface {
|
||||||
|
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||||
|
CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error)
|
||||||
|
DeleteVariable(variableName string) error
|
||||||
|
GetAllVariables() ([]model.Variable, error)
|
||||||
|
GetAvailableConfigs() ([]model.ModuleInfo, error)
|
||||||
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
|
GetSchemaByModuleId(moduleId string) (schema.Schema, error)
|
||||||
|
GetVariableByName(variableName string) (*model.Variable, error)
|
||||||
|
UpsertVariables(vars []model.UpsertVariableRequest) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func AllCommands(configService ConfigService, autoComplete AutoComplete) []*cli.Command {
|
||||||
|
status := NewStatus(configService)
|
||||||
|
get := NewGet(configService, autoComplete)
|
||||||
|
set := NewSet(configService, autoComplete)
|
||||||
|
delete := NewDelete(configService, autoComplete)
|
||||||
|
schema := NewSchema(configService, autoComplete)
|
||||||
|
merge := NewMerge(configService, autoComplete)
|
||||||
|
gitGet := NewGitGet()
|
||||||
|
variables := NewVariables(configService, autoComplete)
|
||||||
return []*cli.Command{
|
return []*cli.Command{
|
||||||
Status(),
|
status.Command(),
|
||||||
Get(),
|
get.Command(),
|
||||||
Set(),
|
set.Command(),
|
||||||
Delete(),
|
delete.Command(),
|
||||||
Schema(),
|
schema.Command(),
|
||||||
Merge(),
|
merge.Command(),
|
||||||
GitGet(),
|
gitGet.Command(),
|
||||||
VariablesCommands(),
|
variables.Command(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+37
-27
@@ -4,33 +4,44 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"ispctl/bash"
|
"ispctl/bash"
|
||||||
"ispctl/command/flag"
|
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/service"
|
"ispctl/model"
|
||||||
|
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Delete() *cli.Command {
|
type DeleteService interface {
|
||||||
return &cli.Command{
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
Name: "delete",
|
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||||
Usage: "delete configuration by module_name",
|
}
|
||||||
Before: flag.ApplyGlobalFlags,
|
|
||||||
Action: deleteComm.action,
|
type Delete struct {
|
||||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
service DeleteService
|
||||||
|
autoComplete AutoComplete
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDelete(service DeleteService, autoComplete AutoComplete) Delete {
|
||||||
|
return Delete{
|
||||||
|
service: service,
|
||||||
|
autoComplete: autoComplete,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var deleteComm deleteCommand
|
func (c Delete) Command() *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "delete",
|
||||||
|
Usage: "delete configuration by module_name",
|
||||||
|
Action: c.action,
|
||||||
|
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.ModuleData),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type deleteCommand struct{}
|
func (c Delete) action(ctx *cli.Context) error {
|
||||||
|
|
||||||
func (d deleteCommand) action(ctx *cli.Context) error {
|
|
||||||
moduleName := ctx.Args().First()
|
moduleName := ctx.Args().First()
|
||||||
pathObject := ctx.Args().Get(1)
|
pathObject := ctx.Args().Get(1)
|
||||||
|
|
||||||
moduleConfiguration, err := service.Config.GetConfigurationByModuleName(moduleName)
|
moduleConfiguration, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -39,19 +50,18 @@ func (d deleteCommand) action(ctx *cli.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if pathObject == "" {
|
if pathObject == "" {
|
||||||
return utils.CreateUpdateConfig("", moduleConfiguration)
|
return utils.CreateUpdateConfig("", moduleConfiguration, c.service)
|
||||||
} else {
|
|
||||||
jsonObject, err := json.Marshal(moduleConfiguration.Data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
return utils.CreateUpdateConfig(stringToChange, moduleConfiguration)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jsonObject, err := json.Marshal(moduleConfiguration.Data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stringToChange, err := sjson.Delete(string(jsonObject), pathObject)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.CreateUpdateConfig(stringToChange, moduleConfiguration, c.service)
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,6 @@
|
|||||||
package flag
|
package flag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
|
|
||||||
"ispctl/service"
|
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,13 +20,3 @@ var (
|
|||||||
|
|
||||||
SetVariableSecretType = &cli.BoolFlag{Name: secretVariableName, Usage: secretVariableUsage}
|
SetVariableSecretType = &cli.BoolFlag{Name: secretVariableName, Usage: secretVariableUsage}
|
||||||
)
|
)
|
||||||
|
|
||||||
func ApplyGlobalFlags(c *cli.Context) error {
|
|
||||||
var host string
|
|
||||||
service.Config.UnsafeEnable = c.Bool(Unsafe.Name)
|
|
||||||
|
|
||||||
host = c.String(Host.Name)
|
|
||||||
host = strings.Replace(host, "'", "", -1)
|
|
||||||
|
|
||||||
return service.Config.ReceiveConfiguration(host)
|
|
||||||
}
|
|
||||||
+24
-17
@@ -4,35 +4,42 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"ispctl/bash"
|
"ispctl/bash"
|
||||||
"ispctl/command/flag"
|
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/service"
|
"ispctl/model"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Get() *cli.Command {
|
type GetService interface {
|
||||||
return &cli.Command{
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
Name: "get",
|
}
|
||||||
Usage: "get configuration by module_name",
|
|
||||||
Before: flag.ApplyGlobalFlags,
|
type Get struct {
|
||||||
Action: get.action,
|
service SetService
|
||||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
autoComplete AutoComplete
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGet(service SetService, autoComplete AutoComplete) Get {
|
||||||
|
return Get{
|
||||||
|
service: service,
|
||||||
|
autoComplete: autoComplete,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var get getCommand
|
func (c Get) Command() *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
type getCommand struct{}
|
Name: "get",
|
||||||
|
Usage: "get configuration by module_name",
|
||||||
func (g getCommand) action(ctx *cli.Context) error {
|
Action: c.action,
|
||||||
if err := flag.ApplyGlobalFlags(ctx); err != nil {
|
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.ModuleData),
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Get) action(ctx *cli.Context) error {
|
||||||
moduleName := ctx.Args().First()
|
moduleName := ctx.Args().First()
|
||||||
pathObject := ctx.Args().Get(1)
|
pathObject := ctx.Args().Get(1)
|
||||||
|
|
||||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
config, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-47
@@ -13,57 +13,65 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type GitGet struct {
|
||||||
semverRegexp = regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$`)
|
semverRegexp *regexp.Regexp
|
||||||
)
|
}
|
||||||
|
|
||||||
func GitGet() *cli.Command {
|
func NewGitGet() GitGet {
|
||||||
|
return GitGet{
|
||||||
|
semverRegexp: regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$`),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c GitGet) Command() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "gitget",
|
Name: "gitget",
|
||||||
Usage: "gitget [repository] [file] (commit|tag)",
|
Usage: "gitget [repository] [file] (commit|tag)",
|
||||||
Description: "clone and print specified file from specified repository and specified commit hash or tag",
|
Description: "clone and print specified file from specified repository and specified commit hash or tag",
|
||||||
Action: func(context *cli.Context) error {
|
Action: c.action,
|
||||||
storage := memory.NewStorage()
|
|
||||||
repo := context.Args().Get(0)
|
|
||||||
|
|
||||||
file := context.Args().Get(1)
|
|
||||||
commit := context.Args().Get(2)
|
|
||||||
|
|
||||||
repository, err := git.Clone(storage, memfs.New(), &git.CloneOptions{
|
|
||||||
URL: repo,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessagef(err, "clone %s", repo)
|
|
||||||
}
|
|
||||||
tree, err := repository.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessage(err, "get worktree")
|
|
||||||
}
|
|
||||||
|
|
||||||
if commit != "" {
|
|
||||||
checkoutOpts := git.CheckoutOptions{}
|
|
||||||
if semverRegexp.MatchString(commit) {
|
|
||||||
checkoutOpts.Branch = plumbing.NewTagReferenceName(commit)
|
|
||||||
} else {
|
|
||||||
checkoutOpts.Hash = plumbing.NewHash(commit)
|
|
||||||
}
|
|
||||||
err = tree.Checkout(&checkoutOpts)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessagef(err, "checkout: %s", commit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := tree.Filesystem.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessagef(err, "open %s", file)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(os.Stdout, f)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c GitGet) action(ctx *cli.Context) error {
|
||||||
|
storage := memory.NewStorage()
|
||||||
|
repo := ctx.Args().Get(0)
|
||||||
|
|
||||||
|
file := ctx.Args().Get(1)
|
||||||
|
commit := ctx.Args().Get(2)
|
||||||
|
|
||||||
|
repository, err := git.Clone(storage, memfs.New(), &git.CloneOptions{
|
||||||
|
URL: repo,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessagef(err, "clone %s", repo)
|
||||||
|
}
|
||||||
|
tree, err := repository.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "get worktree")
|
||||||
|
}
|
||||||
|
|
||||||
|
if commit != "" {
|
||||||
|
checkoutOpts := git.CheckoutOptions{}
|
||||||
|
if c.semverRegexp.MatchString(commit) {
|
||||||
|
checkoutOpts.Branch = plumbing.NewTagReferenceName(commit)
|
||||||
|
} else {
|
||||||
|
checkoutOpts.Hash = plumbing.NewHash(commit)
|
||||||
|
}
|
||||||
|
err = tree.Checkout(&checkoutOpts)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessagef(err, "checkout: %s", commit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := tree.Filesystem.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessagef(err, "open %s", file)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(os.Stdout, f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+46
-29
@@ -5,43 +5,60 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"ispctl/bash"
|
"ispctl/bash"
|
||||||
"ispctl/command/flag"
|
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/service"
|
"ispctl/model"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Merge() *cli.Command {
|
type MergeService interface {
|
||||||
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
|
CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Merge struct {
|
||||||
|
service MergeService
|
||||||
|
autoComplete AutoComplete
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMerge(service MergeService, autoComplete AutoComplete) Merge {
|
||||||
|
return Merge{
|
||||||
|
service: service,
|
||||||
|
autoComplete: autoComplete,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Merge) Command() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "merge",
|
Name: "merge",
|
||||||
Usage: "merge actual config with config from stdin",
|
Usage: "merge actual config with config from stdin",
|
||||||
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
|
BashComplete: c.autoComplete.Complete(bash.CommonConfigName, bash.Empty),
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.action,
|
||||||
Action: func(context *cli.Context) error {
|
|
||||||
moduleName := context.Args().First()
|
|
||||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessagef(err, "get module config '%s'", moduleName)
|
|
||||||
}
|
|
||||||
|
|
||||||
inputConfig := make(map[string]any)
|
|
||||||
err = json.NewDecoder(os.Stdin).Decode(&inputConfig)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessage(err, "json unmarshal input config")
|
|
||||||
}
|
|
||||||
for key, value := range inputConfig {
|
|
||||||
config.Data[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := service.Config.CreateUpdateConfigV2(config)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithMessagef(err, "update module config '%s'", moduleName)
|
|
||||||
}
|
|
||||||
utils.PrintAnswer(result)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Merge) action(ctx *cli.Context) error {
|
||||||
|
moduleName := ctx.Args().First()
|
||||||
|
config, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessagef(err, "get module config '%s'", moduleName)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputConfig := make(map[string]any)
|
||||||
|
err = json.NewDecoder(os.Stdin).Decode(&inputConfig)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "json unmarshal input config")
|
||||||
|
}
|
||||||
|
for key, value := range inputConfig {
|
||||||
|
config.Data[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := c.service.CreateUpdateConfigV2(config)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessagef(err, "update module config '%s'", moduleName)
|
||||||
|
}
|
||||||
|
utils.PrintAnswer(result)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+49
-39
@@ -7,36 +7,50 @@ import (
|
|||||||
"ispctl/bash"
|
"ispctl/bash"
|
||||||
"ispctl/command/flag"
|
"ispctl/command/flag"
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/service"
|
"ispctl/model"
|
||||||
"ispctl/tmpl"
|
"ispctl/tmpl"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/txix-open/isp-kit/rc/schema"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Schema() *cli.Command {
|
type SchemaService interface {
|
||||||
return &cli.Command{
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
Name: "schema",
|
GetSchemaByModuleId(moduleId string) (schema.Schema, error)
|
||||||
Usage: "get schema configuration by module_name",
|
}
|
||||||
Before: flag.ApplyGlobalFlags,
|
|
||||||
Action: schema.action,
|
type Schema struct {
|
||||||
Flags: []cli.Flag{
|
service SchemaService
|
||||||
flag.OutPrintSchema,
|
autoComplete AutoComplete
|
||||||
},
|
}
|
||||||
BashComplete: bash.Get(bash.ModuleName, bash.Empty).Complete,
|
|
||||||
|
func NewSchema(service SchemaService, autoComplete AutoComplete) Schema {
|
||||||
|
return Schema{
|
||||||
|
service: service,
|
||||||
|
autoComplete: autoComplete,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var schema schemaCommand
|
func (c Schema) Command() *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
type schemaCommand struct{}
|
Name: "schema",
|
||||||
|
Usage: "get schema configuration by module_name",
|
||||||
func (s schemaCommand) action(ctx *cli.Context) error {
|
Action: c.action,
|
||||||
moduleName := ctx.Args().First()
|
Flags: []cli.Flag{
|
||||||
schemaConfig := s.getSchemaConfig(moduleName)
|
flag.OutPrintSchema,
|
||||||
if schemaConfig == nil {
|
},
|
||||||
return nil
|
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.Empty),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Schema) action(ctx *cli.Context) error {
|
||||||
|
moduleName := ctx.Args().First()
|
||||||
|
schemaConfig, err := c.getSchemaConfig(moduleName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
schema := make(map[string]any)
|
schema := make(map[string]any)
|
||||||
schema["title"] = moduleName
|
schema["title"] = moduleName
|
||||||
schema["schema"] = schemaConfig
|
schema["schema"] = schemaConfig
|
||||||
@@ -44,33 +58,29 @@ func (s schemaCommand) action(ctx *cli.Context) error {
|
|||||||
case flag.OutPrintJsonValue:
|
case flag.OutPrintJsonValue:
|
||||||
utils.PrintAnswer(schema)
|
utils.PrintAnswer(schema)
|
||||||
case flag.OutPrintHtmlValue:
|
case flag.OutPrintHtmlValue:
|
||||||
s.printHtml(schema)
|
return c.printHtml(schema)
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue)
|
return errors.Errorf("invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s schemaCommand) getSchemaConfig(moduleName string) any {
|
func (c Schema) getSchemaConfig(moduleName string) (schema.Schema, error) {
|
||||||
if configuration, err := service.Config.GetConfigurationByModuleName(moduleName); err != nil {
|
configuration, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||||
utils.PrintError(err)
|
if err != nil {
|
||||||
return nil
|
return nil, err
|
||||||
} else if schema, err := service.Config.GetSchemaByModuleId(configuration.ModuleId); err != nil {
|
|
||||||
utils.PrintError(err)
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return schema
|
|
||||||
}
|
}
|
||||||
|
schema, err := c.service.GetSchemaByModuleId(configuration.ModuleId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return schema, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s schemaCommand) printHtml(schema map[string]any) {
|
func (c Schema) printHtml(schema map[string]any) error {
|
||||||
if temp, err := template.New("template").Parse(tmpl.HtmlFile); err != nil {
|
temp, err := template.New("template").Parse(tmpl.HtmlFile)
|
||||||
utils.PrintError(err)
|
if err != nil {
|
||||||
return
|
return err
|
||||||
} else {
|
|
||||||
if err := temp.ExecuteTemplate(os.Stdout, "template", schema); err != nil {
|
|
||||||
utils.PrintError(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return temp.ExecuteTemplate(os.Stdout, "template", schema)
|
||||||
}
|
}
|
||||||
+37
-27
@@ -4,34 +4,45 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"ispctl/bash"
|
"ispctl/bash"
|
||||||
"ispctl/command/flag"
|
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/service"
|
"ispctl/model"
|
||||||
|
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Set() *cli.Command {
|
type SetService interface {
|
||||||
return &cli.Command{
|
GetConfigurationByModuleName(moduleName string) (*model.Config, error)
|
||||||
Name: "set",
|
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||||
Usage: "set configuration by module_name",
|
}
|
||||||
Before: flag.ApplyGlobalFlags,
|
|
||||||
Action: set.action,
|
type Set struct {
|
||||||
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
|
service SetService
|
||||||
|
autoComplete AutoComplete
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSet(service SetService, autoComplete AutoComplete) Set {
|
||||||
|
return Set{
|
||||||
|
service: service,
|
||||||
|
autoComplete: autoComplete,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var set setCommand
|
func (c Set) Command() *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "set",
|
||||||
|
Usage: "set configuration by module_name",
|
||||||
|
Action: c.action,
|
||||||
|
BashComplete: c.autoComplete.Complete(bash.ModuleName, bash.ModuleData),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type setCommand struct{}
|
func (c Set) action(ctx *cli.Context) error {
|
||||||
|
|
||||||
func (s setCommand) action(ctx *cli.Context) error {
|
|
||||||
moduleName := ctx.Args().First()
|
moduleName := ctx.Args().First()
|
||||||
pathObject := ctx.Args().Get(1)
|
pathObject := ctx.Args().Get(1)
|
||||||
changeObject := ctx.Args().Get(2)
|
changeObject := ctx.Args().Get(2)
|
||||||
|
|
||||||
config, err := service.Config.GetConfigurationByModuleName(moduleName)
|
config, err := c.service.GetConfigurationByModuleName(moduleName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -47,18 +58,17 @@ func (s setCommand) action(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if pathObject == "" {
|
if pathObject == "" {
|
||||||
return utils.CreateUpdateConfig(changeObject, config)
|
return utils.CreateUpdateConfig(changeObject, config, c.service)
|
||||||
} else {
|
|
||||||
jsonObject, err := json.Marshal(config.Data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
changeArgument := utils.ParseSetObject(changeObject)
|
|
||||||
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
return utils.CreateUpdateConfig(stringToChange, config)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
jsonObject, err := json.Marshal(config.Data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
changeArgument := utils.ParseSetObject(changeObject)
|
||||||
|
stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return utils.CreateUpdateConfig(stringToChange, config, c.service)
|
||||||
}
|
}
|
||||||
+38
-31
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"ispctl/command/flag"
|
"ispctl/command/flag"
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/service"
|
"ispctl/model"
|
||||||
|
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@@ -22,48 +22,55 @@ const (
|
|||||||
tableConnectedStatus = "CONNECTED"
|
tableConnectedStatus = "CONNECTED"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Status() *cli.Command {
|
type StatusService interface {
|
||||||
|
GetAvailableConfigs() ([]model.ModuleInfo, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
service StatusService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStatus(service StatusService) Status {
|
||||||
|
return Status{service: service}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Status) Command() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "status",
|
Name: "status",
|
||||||
Usage: "get available configs",
|
Usage: "get available configs",
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.action,
|
||||||
Action: status.action,
|
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
flag.OutPrintStatus,
|
flag.OutPrintStatus,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var status statusCommand
|
func (c Status) action(ctx *cli.Context) error {
|
||||||
|
arrayOfModules, err := c.service.GetAvailableConfigs()
|
||||||
type statusCommand struct{}
|
if err != nil {
|
||||||
|
|
||||||
func (statusCommand) action(ctx *cli.Context) error {
|
|
||||||
if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else {
|
}
|
||||||
switch ctx.String(flag.OutPrintStatus.Name) {
|
switch ctx.String(flag.OutPrintStatus.Name) {
|
||||||
case flag.OutPrintJsonValue:
|
case flag.OutPrintJsonValue:
|
||||||
utils.PrintAnswer(arrayOfModules)
|
utils.PrintAnswer(arrayOfModules)
|
||||||
default:
|
default:
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
|
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
|
||||||
table.SetHeader([]string{tableHeaderModuleName, tableHeaderStatus, tableHeaderAddresses})
|
table.SetHeader([]string{tableHeaderModuleName, tableHeaderStatus, tableHeaderAddresses})
|
||||||
for _, module := range arrayOfModules {
|
for _, module := range arrayOfModules {
|
||||||
addresses := ""
|
addresses := ""
|
||||||
connection := tableNotConnectedStatus
|
connection := tableNotConnectedStatus
|
||||||
instanceList := make([]string, 0, len(module.Status))
|
instanceList := make([]string, 0, len(module.Status))
|
||||||
for _, value := range module.Status {
|
for _, value := range module.Status {
|
||||||
instanceList = append(instanceList, fmt.Sprintf("%s:%s %s", value.Address.IP, value.Address.Port, value.Version))
|
instanceList = append(instanceList, fmt.Sprintf("%s:%s %s", value.Address.IP, value.Address.Port, value.Version))
|
||||||
}
|
|
||||||
addresses = strings.Join(instanceList, "\n")
|
|
||||||
if addresses != "" {
|
|
||||||
connection = tableConnectedStatus
|
|
||||||
}
|
|
||||||
table.Append([]string{module.Name, connection, addresses})
|
|
||||||
}
|
}
|
||||||
table.Render()
|
addresses = strings.Join(instanceList, "\n")
|
||||||
|
if addresses != "" {
|
||||||
|
connection = tableConnectedStatus
|
||||||
|
}
|
||||||
|
table.Append([]string{module.Name, connection, addresses})
|
||||||
}
|
}
|
||||||
|
table.Render()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,14 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"ispctl/model"
|
"ispctl/model"
|
||||||
"ispctl/service"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateUpdateConfig(stringToChange string, configuration *model.Config) error {
|
type UpdateConfigService interface {
|
||||||
answer, err := service.Config.CreateUpdateConfig(stringToChange, configuration)
|
CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateUpdateConfig(stringToChange string, configuration *model.Config, service UpdateConfigService) error {
|
||||||
|
answer, err := service.CreateUpdateConfig(stringToChange, configuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if answer != nil {
|
} else if answer != nil {
|
||||||
|
|||||||
+40
-31
@@ -7,7 +7,6 @@ import (
|
|||||||
"ispctl/command/flag"
|
"ispctl/command/flag"
|
||||||
"ispctl/command/utils"
|
"ispctl/command/utils"
|
||||||
"ispctl/model"
|
"ispctl/model"
|
||||||
"ispctl/service"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -15,7 +14,26 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func VariablesCommands() *cli.Command {
|
type VariablesService interface {
|
||||||
|
GetAllVariables() ([]model.Variable, error)
|
||||||
|
GetVariableByName(variableName string) (*model.Variable, error)
|
||||||
|
DeleteVariable(variableName string) error
|
||||||
|
UpsertVariables(vars []model.UpsertVariableRequest) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Variables struct {
|
||||||
|
service VariablesService
|
||||||
|
autoComplete AutoComplete
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewVariables(service VariablesService, autoComplete AutoComplete) Variables {
|
||||||
|
return Variables{
|
||||||
|
service: service,
|
||||||
|
autoComplete: autoComplete,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Variables) Command() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "vars",
|
Name: "vars",
|
||||||
Usage: "manage variables",
|
Usage: "manage variables",
|
||||||
@@ -23,49 +41,40 @@ func VariablesCommands() *cli.Command {
|
|||||||
{
|
{
|
||||||
Name: "get",
|
Name: "get",
|
||||||
Usage: "vars get <name>",
|
Usage: "vars get <name>",
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.getByName,
|
||||||
Action: varsComm.getByName,
|
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||||
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
Usage: "list all variables",
|
Usage: "list all variables",
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.list,
|
||||||
Action: varsComm.list,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "set",
|
Name: "set",
|
||||||
Usage: "vars set <name> <value>",
|
Usage: "vars set <name> <value>",
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.set,
|
||||||
Action: varsComm.set,
|
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
flag.SetVariableSecretType,
|
flag.SetVariableSecretType,
|
||||||
},
|
},
|
||||||
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
|
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "delete",
|
Name: "delete",
|
||||||
Usage: "vars delete <name>",
|
Usage: "vars delete <name>",
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.delete,
|
||||||
Action: varsComm.delete,
|
BashComplete: c.autoComplete.Complete(bash.VariableName, bash.Empty),
|
||||||
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "upload",
|
Name: "upload",
|
||||||
Usage: "vars upload <filepath>",
|
Usage: "vars upload <filepath>",
|
||||||
Before: flag.ApplyGlobalFlags,
|
Action: c.upload,
|
||||||
Action: varsComm.upload,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var varsComm varsCommands
|
func (c Variables) list(ctx *cli.Context) error {
|
||||||
|
vars, err := c.service.GetAllVariables()
|
||||||
type varsCommands struct{}
|
|
||||||
|
|
||||||
func (g varsCommands) list(ctx *cli.Context) error {
|
|
||||||
vars, err := service.Config.GetAllVariables()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -84,9 +93,9 @@ func (g varsCommands) list(ctx *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g varsCommands) getByName(ctx *cli.Context) error {
|
func (c Variables) getByName(ctx *cli.Context) error {
|
||||||
variableName := ctx.Args().Get(0)
|
variableName := ctx.Args().Get(0)
|
||||||
variable, err := service.Config.GetVariableByName(variableName)
|
variable, err := c.service.GetVariableByName(variableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -94,33 +103,33 @@ func (g varsCommands) getByName(ctx *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g varsCommands) set(ctx *cli.Context) error {
|
func (c Variables) set(ctx *cli.Context) error {
|
||||||
variableName := ctx.Args().Get(0)
|
variableName := ctx.Args().Get(0)
|
||||||
variableValue := ctx.Args().Get(1)
|
variableValue := ctx.Args().Get(1)
|
||||||
variableType := model.TextVariableType
|
variableType := model.TextVariableType
|
||||||
if ctx.Bool(flag.SetVariableSecretType.Name) {
|
if ctx.Bool(flag.SetVariableSecretType.Name) {
|
||||||
variableType = model.SecretVariableType
|
variableType = model.SecretVariableType
|
||||||
}
|
}
|
||||||
return service.Config.UpsertVariables([]model.UpsertVariableRequest{{
|
return c.service.UpsertVariables([]model.UpsertVariableRequest{{
|
||||||
Name: variableName,
|
Name: variableName,
|
||||||
Value: variableValue,
|
Value: variableValue,
|
||||||
Type: variableType,
|
Type: variableType,
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g varsCommands) delete(ctx *cli.Context) error {
|
func (c Variables) delete(ctx *cli.Context) error {
|
||||||
return service.Config.DeleteVariable(ctx.Args().Get(0))
|
return c.service.DeleteVariable(ctx.Args().Get(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g varsCommands) upload(ctx *cli.Context) error {
|
func (c Variables) upload(ctx *cli.Context) error {
|
||||||
variables, err := g.readVariablesFromCsv(ctx.Args().First())
|
variables, err := c.readVariablesFromCsv(ctx.Args().First())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return service.Config.UpsertVariables(variables)
|
return c.service.UpsertVariables(variables)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g varsCommands) readVariablesFromCsv(filepath string) ([]model.UpsertVariableRequest, error) {
|
func (c Variables) readVariablesFromCsv(filepath string) ([]model.UpsertVariableRequest, error) {
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open file %s: %w", filepath, err)
|
return nil, fmt.Errorf("failed to open file %s: %w", filepath, err)
|
||||||
|
|||||||
@@ -4,9 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"ispctl/bash"
|
||||||
"ispctl/command"
|
"ispctl/command"
|
||||||
"ispctl/command/flag"
|
"ispctl/command/flag"
|
||||||
|
"ispctl/repository"
|
||||||
|
"ispctl/service"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
@@ -27,7 +31,20 @@ func initCommands() {
|
|||||||
flag.Unsafe,
|
flag.Unsafe,
|
||||||
}
|
}
|
||||||
app.EnableBashCompletion = true
|
app.EnableBashCompletion = true
|
||||||
app.Commands = command.AllCommands()
|
|
||||||
|
var configService service.Config
|
||||||
|
var autoComplete bash.Autocomplete
|
||||||
|
app.Before = func(ctx *cli.Context) error {
|
||||||
|
service, err := configServiceFromGlobalFlags(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
configService = service
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
autoComplete = bash.NewAutocomplete(configServiceFromGlobalFlags)
|
||||||
|
app.Commands = command.AllCommands(&configService, autoComplete)
|
||||||
|
|
||||||
app.BashComplete = func(context *cli.Context) {
|
app.BashComplete = func(context *cli.Context) {
|
||||||
for _, appCommand := range app.Commands {
|
for _, appCommand := range app.Commands {
|
||||||
fmt.Println(appCommand.Name)
|
fmt.Println(appCommand.Name)
|
||||||
@@ -39,3 +56,17 @@ func initCommands() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func configServiceFromGlobalFlags(ctx *cli.Context) (service.Config, error) {
|
||||||
|
enableUnsafe := ctx.Bool(flag.Unsafe.Name)
|
||||||
|
|
||||||
|
host := ctx.String(flag.Host.Name)
|
||||||
|
host = strings.Replace(host, "'", "", -1)
|
||||||
|
|
||||||
|
configCli, err := repository.NewGrpcClientWithHost(host)
|
||||||
|
if err != nil {
|
||||||
|
return service.Config{}, err
|
||||||
|
}
|
||||||
|
configRepo := repository.NewConfig(configCli)
|
||||||
|
return service.NewConfig(enableUnsafe, configRepo), nil
|
||||||
|
}
|
||||||
@@ -4,11 +4,11 @@ import (
|
|||||||
"github.com/txix-open/isp-kit/grpc/client"
|
"github.com/txix-open/isp-kit/grpc/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClientFromHost(address string) (*client.Client, error) {
|
func NewGrpcClientWithHost(host string) (*client.Client, error) {
|
||||||
cli, err := client.Default()
|
cli, err := client.Default()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cli.Upgrade([]string{address})
|
cli.Upgrade([]string{host})
|
||||||
return cli, nil
|
return cli, nil
|
||||||
}
|
}
|
||||||
+16
-20
@@ -10,27 +10,23 @@ import (
|
|||||||
"github.com/txix-open/isp-kit/rc/schema"
|
"github.com/txix-open/isp-kit/rc/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Config = &ConfigService{}
|
type Config struct {
|
||||||
|
enableUnsafe bool
|
||||||
type ConfigService struct {
|
|
||||||
UnsafeEnable bool
|
|
||||||
configRepo repository.Config
|
configRepo repository.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) ReceiveConfiguration(host string) error {
|
func NewConfig(enableUnsafe bool, configRepo repository.Config) Config {
|
||||||
cli, err := repository.NewClientFromHost(host)
|
return Config{
|
||||||
if err != nil {
|
enableUnsafe: enableUnsafe,
|
||||||
return err
|
configRepo: configRepo,
|
||||||
}
|
}
|
||||||
c.configRepo = repository.NewConfig(cli)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) GetAvailableConfigs() ([]model.ModuleInfo, error) {
|
func (c Config) GetAvailableConfigs() ([]model.ModuleInfo, error) {
|
||||||
return c.configRepo.GetAvailableConfigs()
|
return c.configRepo.GetAvailableConfigs()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) GetConfigurationByModuleName(moduleName string) (*model.Config, error) {
|
func (c Config) GetConfigurationByModuleName(moduleName string) (*model.Config, error) {
|
||||||
if moduleName == "" {
|
if moduleName == "" {
|
||||||
return nil, errors.New("need module name")
|
return nil, errors.New("need module name")
|
||||||
}
|
}
|
||||||
@@ -44,7 +40,7 @@ func (c *ConfigService) GetConfigurationByModuleName(moduleName string) (*model.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) GetSchemaByModuleId(moduleId string) (schema.Schema, error) {
|
func (c Config) GetSchemaByModuleId(moduleId string) (schema.Schema, error) {
|
||||||
configSchema, err := c.configRepo.GetSchemaByModuleId(moduleId)
|
configSchema, err := c.configRepo.GetSchemaByModuleId(moduleId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -52,7 +48,7 @@ func (c *ConfigService) GetSchemaByModuleId(moduleId string) (schema.Schema, err
|
|||||||
return configSchema.Schema, nil
|
return configSchema.Schema, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error) {
|
func (c Config) CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error) {
|
||||||
newData := make(map[string]any)
|
newData := make(map[string]any)
|
||||||
if stringToChange != "" {
|
if stringToChange != "" {
|
||||||
err := json.Unmarshal([]byte(stringToChange), &newData)
|
err := json.Unmarshal([]byte(stringToChange), &newData)
|
||||||
@@ -65,8 +61,8 @@ func (c *ConfigService) CreateUpdateConfig(stringToChange string, configuration
|
|||||||
return c.CreateUpdateConfigV2(configuration)
|
return c.CreateUpdateConfigV2(configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error) {
|
func (c Config) CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error) {
|
||||||
configuration.Unsafe = c.UnsafeEnable
|
configuration.Unsafe = c.enableUnsafe
|
||||||
resp, err := c.configRepo.CreateUpdateConfig(*configuration)
|
resp, err := c.configRepo.CreateUpdateConfig(*configuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -74,11 +70,11 @@ func (c *ConfigService) CreateUpdateConfigV2(configuration *model.Config) (map[s
|
|||||||
return resp.Data, nil
|
return resp.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) GetAllVariables() ([]model.Variable, error) {
|
func (c Config) GetAllVariables() ([]model.Variable, error) {
|
||||||
return c.configRepo.GetAllVariables()
|
return c.configRepo.GetAllVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) GetVariableByName(variableName string) (*model.Variable, error) {
|
func (c Config) GetVariableByName(variableName string) (*model.Variable, error) {
|
||||||
if variableName == "" {
|
if variableName == "" {
|
||||||
return nil, errors.New("need variable name")
|
return nil, errors.New("need variable name")
|
||||||
}
|
}
|
||||||
@@ -94,7 +90,7 @@ func (c *ConfigService) GetVariableByName(variableName string) (*model.Variable,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) DeleteVariable(variableName string) error {
|
func (c Config) DeleteVariable(variableName string) error {
|
||||||
if variableName == "" {
|
if variableName == "" {
|
||||||
return errors.New("need variable name")
|
return errors.New("need variable name")
|
||||||
}
|
}
|
||||||
@@ -108,6 +104,6 @@ func (c *ConfigService) DeleteVariable(variableName string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigService) UpsertVariables(vars []model.UpsertVariableRequest) error {
|
func (c Config) UpsertVariables(vars []model.UpsertVariableRequest) error {
|
||||||
return c.configRepo.UpsertVariables(vars)
|
return c.configRepo.UpsertVariables(vars)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user