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
+38
-31
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"ispctl/command/flag"
|
||||
"ispctl/command/utils"
|
||||
"ispctl/service"
|
||||
"ispctl/model"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/urfave/cli/v2"
|
||||
@@ -22,48 +22,55 @@ const (
|
||||
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{
|
||||
Name: "status",
|
||||
Usage: "get available configs",
|
||||
Before: flag.ApplyGlobalFlags,
|
||||
Action: status.action,
|
||||
Action: c.action,
|
||||
Flags: []cli.Flag{
|
||||
flag.OutPrintStatus,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var status statusCommand
|
||||
|
||||
type statusCommand struct{}
|
||||
|
||||
func (statusCommand) action(ctx *cli.Context) error {
|
||||
if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil {
|
||||
func (c Status) action(ctx *cli.Context) error {
|
||||
arrayOfModules, err := c.service.GetAvailableConfigs()
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
switch ctx.String(flag.OutPrintStatus.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
utils.PrintAnswer(arrayOfModules)
|
||||
default:
|
||||
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 {
|
||||
addresses := ""
|
||||
connection := tableNotConnectedStatus
|
||||
instanceList := make([]string, 0, len(module.Status))
|
||||
for _, value := range module.Status {
|
||||
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})
|
||||
}
|
||||
switch ctx.String(flag.OutPrintStatus.Name) {
|
||||
case flag.OutPrintJsonValue:
|
||||
utils.PrintAnswer(arrayOfModules)
|
||||
default:
|
||||
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 {
|
||||
addresses := ""
|
||||
connection := tableNotConnectedStatus
|
||||
instanceList := make([]string, 0, len(module.Status))
|
||||
for _, value := range module.Status {
|
||||
instanceList = append(instanceList, fmt.Sprintf("%s:%s %s", value.Address.IP, value.Address.Port, value.Version))
|
||||
}
|
||||
table.Render()
|
||||
addresses = strings.Join(instanceList, "\n")
|
||||
if addresses != "" {
|
||||
connection = tableConnectedStatus
|
||||
}
|
||||
table.Append([]string{module.Name, connection, addresses})
|
||||
}
|
||||
table.Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user