fix: make recipe name autocomplete less slow #604

Merged
decentral1se merged 4 commits from fix/567 into main 2025-08-24 07:24:48 +00:00
5 changed files with 54 additions and 3 deletions

View File

@ -20,6 +20,24 @@ import (
"github.com/spf13/cobra"
)
var CatalogueSyncCommand = &cobra.Command{
Use: i18n.G("sync [flags]"),
Aliases: []string{i18n.G("g")},
Short: i18n.G("Sync recipe catalogue for latest changes"),
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if err := catalogue.EnsureCatalogue(); err != nil {
log.Fatal(err)
}
if err := catalogue.EnsureUpToDate(); err != nil {
log.Fatal(err)
}
log.Info(i18n.G("catalogue successfully synced"))
},
}
var CatalogueGenerateCommand = &cobra.Command{
Use: i18n.G("generate [recipe] [flags]"),
Aliases: []string{i18n.G("g")},

View File

@ -143,6 +143,7 @@ func Run(version, commit string) {
catalogue.CatalogueCommand.AddCommand(
catalogue.CatalogueGenerateCommand,
catalogue.CatalogueSyncCommand,
)
server.ServerCommand.AddCommand(

View File

@ -2,6 +2,7 @@ package autocomplete
import (
"sort"
"strings"
"coopcloud.tech/abra/pkg/app"
appPkg "coopcloud.tech/abra/pkg/app"
@ -38,17 +39,27 @@ func ServiceNameComplete(appName string) ([]string, cobra.ShellCompDirective) {
// RecipeNameComplete completes recipe names.
func RecipeNameComplete() ([]string, cobra.ShellCompDirective) {
catl, err := recipe.ReadRecipeCatalogue(false)
catl, err := recipe.ReadRecipeCatalogue(true)
decentral1se marked this conversation as resolved Outdated
Outdated
Review

Even when offline, I think it would be nice to "cache" the catalogue in memory. Otherwise everytime we hit it reads the recipe catalogue again. What do you think @decentral1se

Even when offline, I think it would be nice to "cache" the catalogue in memory. Otherwise everytime we hit <tab> it reads the recipe catalogue again. What do you think @decentral1se

@p4u1 good idea, that does make a lot of sense. I can see we are doing multiple calls in some code paths. Should we maybe try something like https://github.com/kofalt/go-memoize for this? Otherwise, cobra does provide context, but it might be awkward putting that in / retrieving it elsewhere...

@p4u1 good idea, that does make a lot of sense. I can see we are doing multiple calls in some code paths. Should we maybe try something like https://github.com/kofalt/go-memoize for this? Otherwise, `cobra` does provide context, but it might be awkward putting that in / retrieving it elsewhere...

I tried it out but I fear the situation that people then want a flag or some way to bust the cache or they don't even realise it is being cache and can't seem to get the latest catalogue values... I am gonna leave this off for someone smarter than me 🙃

I tried it out but I fear the situation that people then want a flag or some way to bust the cache or they don't even realise it is being cache and can't seem to get the latest catalogue values... I am gonna leave this off for someone smarter than me 🙃
if err != nil {
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
}
localRecipes, err := recipe.GetRecipesLocal()
if err != nil && !strings.Contains(err.Error(), "empty") {
err := i18n.G("autocomplete failed: %s", err)
return []string{err}, cobra.ShellCompDirectiveError
}
var recipeNames []string
for name := range catl {
recipeNames = append(recipeNames, name)
}
for _, recipeLocal := range localRecipes {
recipeNames = append(recipeNames, recipeLocal)
decentral1se marked this conversation as resolved Outdated
Outdated
Review

Sweet!

Sweet!
}
return recipeNames, cobra.ShellCompDirectiveDefault
}

View File

@ -373,8 +373,6 @@ func GetStringInBetween(recipeName, str, start, end string) (result string, err
// ReadRecipeCatalogue reads the recipe catalogue.
func ReadRecipeCatalogue(offline bool) (RecipeCatalogue, error) {
recipes := make(RecipeCatalogue)
if err := catalogue.EnsureCatalogue(); err != nil {
return nil, err
}
@ -385,6 +383,8 @@ func ReadRecipeCatalogue(offline bool) (RecipeCatalogue, error) {
}
}
recipes := make(RecipeCatalogue)
if err := readRecipeCatalogueFS(&recipes); err != nil {
return nil, err
}

View File

@ -47,3 +47,24 @@ setup(){
assert_success
assert_exists "$ABRA_DIR/recipes/gitea/.git"
}
# bats test_tags=slow
@test "sync latest changes" {
_ensure_catalogue
latestHash=$(git -C "$ABRA_DIR/catalogue" show -s --format="%H")
wantHash=$(git -C "$ABRA_DIR/catalogue" show -s --format="%H" "HEAD~3")
run git -C "$ABRA_DIR/catalogue" reset --hard HEAD~3
assert_success
currHash=$(git -C "$ABRA_DIR/catalogue" show -s --format="%H")
assert_equal "$currHash" "$wantHash"
run $ABRA catalogue sync
assert_success
syncHash=$(git -C "$ABRA_DIR/catalogue" show -s --format="%H")
assert_equal "$syncHash" "$latestHash"
}