refactor!: cobra migrate

This commit is contained in:
2024-12-26 17:53:25 +01:00
parent 0df2b15c33
commit 671e1ca276
76 changed files with 12042 additions and 2545 deletions

View File

@ -1,103 +1,119 @@
package autocomplete
import (
"context"
"fmt"
"sort"
"coopcloud.tech/abra/pkg/app"
appPkg "coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"github.com/urfave/cli/v3"
"github.com/spf13/cobra"
)
// AppNameComplete copletes app names.
func AppNameComplete(ctx context.Context, cmd *cli.Command) {
func AppNameComplete() ([]string, cobra.ShellCompDirective) {
appNames, err := app.GetAppNames()
if err != nil {
log.Warn(err)
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
if cmd.NArg() > 0 {
return
}
for _, a := range appNames {
fmt.Println(a)
}
return appNames, cobra.ShellCompDirectiveDefault
}
func ServiceNameComplete(appName string) {
func ServiceNameComplete(appName string) ([]string, cobra.ShellCompDirective) {
serviceNames, err := app.GetAppServiceNames(appName)
if err != nil {
return
}
for _, s := range serviceNames {
fmt.Println(s)
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
return serviceNames, cobra.ShellCompDirectiveDefault
}
// RecipeNameComplete completes recipe names.
func RecipeNameComplete(ctx context.Context, cmd *cli.Command) {
func RecipeNameComplete() ([]string, cobra.ShellCompDirective) {
catl, err := recipe.ReadRecipeCatalogue(false)
if err != nil {
log.Warn(err)
}
if cmd.NArg() > 0 {
return
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
var recipeNames []string
for name := range catl {
fmt.Println(name)
recipeNames = append(recipeNames, name)
}
return recipeNames, cobra.ShellCompDirectiveDefault
}
// RecipeVersionComplete completes versions for the recipe.
func RecipeVersionComplete(recipeName string) {
func RecipeVersionComplete(recipeName string) ([]string, cobra.ShellCompDirective) {
catl, err := recipe.ReadRecipeCatalogue(false)
if err != nil {
log.Warn(err)
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
var recipeVersions []string
for _, v := range catl[recipeName].Versions {
for v2 := range v {
fmt.Println(v2)
recipeVersions = append(recipeVersions, v2)
}
}
return recipeVersions, cobra.ShellCompDirectiveDefault
}
// ServerNameComplete completes server names.
func ServerNameComplete(ctx context.Context, cmd *cli.Command) {
func ServerNameComplete() ([]string, cobra.ShellCompDirective) {
files, err := app.LoadAppFiles("")
if err != nil {
log.Fatal(err)
}
if cmd.NArg() > 0 {
return
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
var serverNames []string
for _, appFile := range files {
fmt.Println(appFile.Server)
serverNames = append(serverNames, appFile.Server)
}
return serverNames, cobra.ShellCompDirectiveDefault
}
// SubcommandComplete completes sub-commands.
func SubcommandComplete(ctx context.Context, cmd *cli.Command) {
if cmd.NArg() > 0 {
return
// CommandNameComplete completes recipe commands.
func CommandNameComplete(appName string) ([]string, cobra.ShellCompDirective) {
app, err := app.Get(appName)
if err != nil {
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
subcmds := []string{
"app",
"autocomplete",
"catalogue",
"recipe",
"server",
"upgrade",
cmdNames, err := appPkg.ReadAbraShCmdNames(app.Recipe.AbraShPath)
if err != nil {
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
for _, cmd := range subcmds {
fmt.Println(cmd)
}
sort.Strings(cmdNames)
return cmdNames, cobra.ShellCompDirectiveDefault
}
// SecretsComplete completes recipe secrets.
func SecretComplete(recipeName string) ([]string, cobra.ShellCompDirective) {
r := recipe.Get(recipeName)
config, err := r.GetComposeConfig(nil)
if err != nil {
log.Debugf("autocomplete failed: %s", err)
return nil, cobra.ShellCompDirectiveError
}
var secretNames []string
for name := range config.Secrets {
secretNames = append(secretNames, name)
}
return secretNames, cobra.ShellCompDirectiveDefault
}