MSP-5014 Try get rid of global service and autocomplete

This commit is contained in:
Синельник Тимур Романович committed 2025-03-24 11:30:39 +03:00
1 parent 84f639a9d3
commit c267b1ae6e
15 files changed
+468 -335

No files matched your search

+32 -1
View File
@@ -4,9 +4,13 @@ import (
"fmt"
"log"
"os"
"strings"
"ispctl/bash"
"ispctl/command"
"ispctl/command/flag"
"ispctl/repository"
"ispctl/service"
"github.com/urfave/cli/v2"
)
@@ -27,7 +31,20 @@ func initCommands() {
flag.Unsafe,
}
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) {
for _, appCommand := range app.Commands {
fmt.Println(appCommand.Name)
@@ -39,3 +56,17 @@ func initCommands() {
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
}