Files
abra/pkg/autocomplete/autocomplete.go
T

136 lines
3.6 KiB
Go
Raw Normal View History

2021-12-12 00:17:39 +01:00
package autocomplete
import (
2024-12-26 17:53:25 +01:00
"sort"
"strings"
2021-12-12 00:17:39 +01:00
"coopcloud.tech/abra/pkg/app"
2024-12-26 17:53:25 +01:00
appPkg "coopcloud.tech/abra/pkg/app"
2025-08-19 11:22:52 +02:00
"coopcloud.tech/abra/pkg/i18n"
2021-12-27 16:40:59 +01:00
"coopcloud.tech/abra/pkg/recipe"
2024-12-26 17:53:25 +01:00
"github.com/spf13/cobra"
2021-12-12 00:17:39 +01:00
)
// AppNameComplete copletes app names.
2024-12-26 17:53:25 +01:00
func AppNameComplete() ([]string, cobra.ShellCompDirective) {
appFiles, err := app.LoadAppFiles("")
2021-12-12 00:17:39 +01:00
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
}
var appNames []string
for appName := range appFiles {
appNames = append(appNames, appName)
2021-12-12 00:17:39 +01:00
}
2024-12-26 17:53:25 +01:00
return appNames, cobra.ShellCompDirectiveDefault
2021-12-12 00:17:39 +01:00
}
2024-12-26 17:53:25 +01:00
func ServiceNameComplete(appName string) ([]string, cobra.ShellCompDirective) {
serviceNames, err := app.GetAppServiceNames(appName)
2023-11-10 12:03:10 +01:00
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
2023-11-10 12:03:10 +01:00
}
2024-12-26 17:53:25 +01:00
return serviceNames, cobra.ShellCompDirectiveDefault
2023-11-10 12:03:10 +01:00
}
// RecipeNameComplete completes recipe names.
2024-12-26 17:53:25 +01:00
func RecipeNameComplete() ([]string, cobra.ShellCompDirective) {
2025-08-18 10:11:23 +02:00
catl, err := recipe.ReadRecipeCatalogue(true)
2021-12-12 00:17:39 +01:00
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
2021-12-12 00:17:39 +01:00
}
localRecipes, err := recipe.GetRecipesLocal()
if err != nil && !strings.Contains(err.Error(), "empty") {
2025-08-24 09:16:44 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
}
2024-12-26 17:53:25 +01:00
var recipeNames []string
2021-12-12 00:17:39 +01:00
for name := range catl {
2024-12-26 17:53:25 +01:00
recipeNames = append(recipeNames, name)
2021-12-12 00:17:39 +01:00
}
2024-12-26 17:53:25 +01:00
for _, recipeLocal := range localRecipes {
recipeNames = append(recipeNames, recipeLocal)
}
2024-12-26 17:53:25 +01:00
return recipeNames, cobra.ShellCompDirectiveDefault
2021-12-12 00:17:39 +01:00
}
2022-01-20 11:42:04 +01:00
// RecipeVersionComplete completes versions for the recipe.
2024-12-26 17:53:25 +01:00
func RecipeVersionComplete(recipeName string) ([]string, cobra.ShellCompDirective) {
2025-01-03 08:10:57 +01:00
catl, err := recipe.ReadRecipeCatalogue(true)
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
}
2024-12-26 17:53:25 +01:00
var recipeVersions []string
for _, v := range catl[recipeName].Versions {
for v2 := range v {
2024-12-26 17:53:25 +01:00
recipeVersions = append(recipeVersions, v2)
}
}
2024-12-26 17:53:25 +01:00
return recipeVersions, cobra.ShellCompDirectiveDefault
}
// ServerNameComplete completes server names.
2024-12-26 17:53:25 +01:00
func ServerNameComplete() ([]string, cobra.ShellCompDirective) {
files, err := app.LoadAppFiles("")
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
}
2024-12-26 17:53:25 +01:00
var serverNames []string
for _, appFile := range files {
2024-12-26 17:53:25 +01:00
serverNames = append(serverNames, appFile.Server)
}
2024-12-26 17:53:25 +01:00
return serverNames, cobra.ShellCompDirectiveDefault
}
2024-12-26 17:53:25 +01:00
// CommandNameComplete completes recipe commands.
func CommandNameComplete(appName string) ([]string, cobra.ShellCompDirective) {
app, err := app.Get(appName)
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
2022-01-20 11:42:04 +01:00
}
2024-12-26 17:53:25 +01:00
cmdNames, err := appPkg.ReadAbraShCmdNames(app.Recipe.AbraShPath)
if err != nil {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
2022-01-20 11:42:04 +01:00
}
2024-12-26 17:53:25 +01:00
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 {
2025-08-19 11:22:52 +02:00
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
2022-01-20 11:42:04 +01:00
}
2024-12-26 17:53:25 +01:00
var secretNames []string
for name := range config.Secrets {
secretNames = append(secretNames, name)
}
return secretNames, cobra.ShellCompDirectiveDefault
2022-01-20 11:42:04 +01:00
}