package cmd import ( "context" fzf "github.com/ktr0731/go-fuzzyfinder" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects" ) func fzfFindProject(searchString string) *gitlab.Project { var project *gitlab.Project if searchString != "" { project = fzfSearchProjectAliases(searchString) } else { var err error project, err = fzfProject(rootCmd.Context()) if project == nil || err != nil { return nil } } return project } func fzfSearchProjectAliases(searchString string) *gitlab.Project { var project *gitlab.Project var alias *projects.ProjectAlias if alias = cache.GetAliasByName(searchString); alias != nil { project = cache.GetProjectByAlias(alias) plog.Info("Perfect alias match... flawless") } else { // Get fuzzy if we don't have an exact match aliases := cache.FuzzyFindAlias(searchString) if len(aliases) > 1 { alias = fzfAliasFromAliases(aliasAddCmd.Context(), aliases) } else if len(aliases) == 1 { alias = aliases[0] } project = cache.GetProjectByAlias(alias) } return project } func fzfAliasFromAliases(ctx context.Context, aliases []*projects.ProjectAlias) *projects.ProjectAlias { var alias *projects.ProjectAlias i, err := fzf.Find( aliases, func(i int) string { return aliases[i].Alias + " -> " + cache.GetProjectByAlias(aliases[i]).PathWithNamespace }, fzf.WithContext(ctx), fzf.WithHeader("Choose an Alias"), ) if err != nil { plog.Error("Failed to fzf alias slice", plog.Args("error", err)) } else { alias = aliases[i] } return alias } func fzfProject(ctx context.Context) (*gitlab.Project, error) { i, err := fzf.Find(cache.Projects, fzfProjectString, fzf.WithPreviewWindow(fzfPreviewWindow), fzf.WithContext(ctx), fzf.WithHeader("Fuzzy find yourself a project"), ) if err != nil || i < 0 { return nil, err } return cache.Projects[i], nil } func fzfPreviewWindow(i, w, h int) string { p := cache.Projects[i] return cache.ProjectString(p) } func fzfProjectString(i int) string { return cache.Projects[i].String() }