Rename projects to cache package

This commit is contained in:
Ryan McGuire 2024-01-15 16:02:15 -05:00
parent d6833a9ea0
commit e846821c44
26 changed files with 73 additions and 73 deletions

View File

@ -19,7 +19,7 @@ func init() {
}
func mustHaveAliases(cmd *cobra.Command, args []string) {
if len(cache.Aliases) == 0 {
if len(projectCache.Aliases) == 0 {
plog.Fatal("No aliases set, nothing to " + cmd.Name())
}
}

View File

@ -7,7 +7,7 @@ import (
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cacheProjects "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
)
@ -26,7 +26,7 @@ func runAddAliasCmd(cmd *cobra.Command, args []string) {
// Check by flag
if projectID := viper.GetInt("alias.add.projectid"); projectID > 0 {
plog.Debug(fmt.Sprintf("Adding for inbound project ID %d", projectID))
project = cache.GetProjectByID(projectID)
project = projectCache.GetProjectByID(projectID)
}
// Check by arg
@ -47,7 +47,7 @@ func runAddAliasCmd(cmd *cobra.Command, args []string) {
}
func addNewAliases(projectID int) {
project := cache.GetProjectByID(projectID)
project := projectCache.GetProjectByID(projectID)
if project == nil {
plog.Error("Failed to find project to alias", plog.Args("projectID", projectID))
return
@ -62,7 +62,7 @@ func addNewAliases(projectID int) {
if a == "" {
continue
}
if err := cache.AddAlias(a, project.ID, project.Remote); err != nil {
if err := projectCache.AddAlias(a, project.ID, project.Remote); err != nil {
plog.Debug("Skipping alias add", plog.Args(
"error", err,
"alias", a,
@ -77,11 +77,11 @@ func addNewAliases(projectID int) {
}
func promptAliasesForProject(p *projects.Project) []string {
aliases := cache.GetProjectAliases(p)
aliases := projectCache.GetProjectAliases(p)
if len(aliases) > 0 {
plog.Info("Adding aliases to project", plog.Args(
"project", p.String(),
"existingAliases", cacheProjects.ProjectAliasesString(aliases),
"existingAliases", cache.ProjectAliasesString(aliases),
))
} else {
pterm.Info.Printfln("Adding aliases to %s", p.Name)

View File

@ -40,7 +40,7 @@ func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
))
}
aliasStrings := cache.GetProjectAliasStrings(project)
aliasStrings := projectCache.GetProjectAliasStrings(project)
deletionCandidates, err := pterm.DefaultInteractiveMultiselect.
WithOptions(aliasStrings).
@ -70,10 +70,10 @@ func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
"alias", a,
))
cache.DeleteAlias(cache.GetAliasByName(a))
projectCache.DeleteAlias(projectCache.GetAliasByName(a))
}
fmt.Println(cache.ProjectString(project))
fmt.Println(projectCache.ProjectString(project))
}
func init() {

View File

@ -23,7 +23,7 @@ func runListAliasCmd(cmd *cobra.Command, args []string) {
WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Aliases by Project"))).
Print("\n" + cache.AliasesByProjectString())
Print("\n" + projectCache.AliasesByProjectString())
fmt.Print("\n\n")
}

View File

@ -5,10 +5,10 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
)
var cache *projects.Cache
var projectCache *cache.Cache
var cacheCmd = &cobra.Command{
Use: "cache",
@ -19,12 +19,12 @@ var cacheCmd = &cobra.Command{
func runCacheCmd(cmd *cobra.Command, args []string) {
initProjectCache(cmd, args)
cache.LockCache()
projectCache.LockCache()
}
func postCacheCmd(cmd *cobra.Command, args []string) {
postProjectCache(cmd, args)
cache.UnlockCache()
projectCache.UnlockCache()
}
func init() {

View File

@ -21,7 +21,7 @@ var clearCmd = &cobra.Command{
func clearCache(cmd *cobra.Command, args []string) {
slog.Debug("Preparing to clear local cache")
cache.Clear(conf.Cache.Clear.ClearAliases)
projectCache.Clear(conf.Cache.Clear.ClearAliases)
}
func init() {

View File

@ -15,9 +15,9 @@ var dumpCmd = &cobra.Command{
PostRun: postCacheCmd,
Run: func(cmd *cobra.Command, args []string) {
if conf.Dump.Full {
fmt.Println(cache.DumpString(true, searchStringFromArgs(args)))
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args)))
} else {
plog.Info(cache.String())
plog.Info(projectCache.String())
}
},
}

View File

@ -17,7 +17,7 @@ wants to find a new project.`,
}
func loadCache(cmd *cobra.Command, args []string) {
cache.Refresh()
projectCache.Refresh()
}
func init() {

View File

@ -13,11 +13,11 @@ var unlockCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
initProjectCache(cmd, args)
if viper.GetBool("cache.unlock.force") {
cache.UnlockCache()
projectCache.UnlockCache()
} else if yes, _ := pterm.DefaultInteractiveConfirm.
WithDefaultValue(false).
Show("Are you sure you want to manually unlock?"); yes {
cache.UnlockCache()
projectCache.UnlockCache()
} else {
plog.Error("You failed to confirm cache unlock")
}

View File

@ -3,7 +3,7 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
cacheProjects "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
)
@ -33,12 +33,12 @@ func getProject(args []string) *projects.Project {
} else {
plog.Debug("Houston, we have a project", plog.Args(
"project", project.String(),
"aliases", cacheProjects.ProjectAliasesString(
cache.GetProjectAliases(project)),
"aliases", cache.ProjectAliasesString(
projectCache.GetProjectAliases(project)),
))
}
if len(cache.GetProjectAliases(project)) == 0 {
if len(projectCache.GetProjectAliases(project)) == 0 {
plog.Info("New project, set aliases or press enter for default")
addNewAliases(project.ID)
}
@ -63,7 +63,7 @@ func init() {
}
func mustHaveProjects(cmd *cobra.Command, args []string) {
if len(cache.Projects) == 0 {
if len(projectCache.Projects) == 0 {
plog.Fatal("No projects to " + cmd.Name() + ", try running cache load")
}
}

View File

@ -34,11 +34,11 @@ func projectGoCmdRun(cmd *cobra.Command, args []string) {
plog.Fatal("No project selected, nowhere to go")
}
cache.GoTo(project)
project.SetRepo(cache.OpenProject(cmd.Context(), project))
projectCache.GoTo(project)
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
plog.Debug("Project ready", plog.Args(
"path", cache.GetProjectPath(project),
"path", projectCache.GetProjectPath(project),
"project", project,
))
@ -46,8 +46,8 @@ func projectGoCmdRun(cmd *cobra.Command, args []string) {
// This should be read by any source command, for instance
// `cd "$(gitlab-project-manager projects cd somealias)"`
fmt.Println(cache.GetProjectPath(project))
exec.Command("cd", cache.GetProjectPath(project)).Run()
fmt.Println(projectCache.GetProjectPath(project))
exec.Command("cd", projectCache.GetProjectPath(project)).Run()
}
func init() {

View File

@ -17,7 +17,7 @@ var projectListCmd = &cobra.Command{
func projectListCmdRun(cmd *cobra.Command, args []string) {
gitlabs := viper.GetStringSlice("project.gitlabs")
fmt.Println(cache.DumpString(viper.GetBool("project.list.all"), searchStringFromArgs(args), gitlabs...))
fmt.Println(projectCache.DumpString(viper.GetBool("project.list.all"), searchStringFromArgs(args), gitlabs...))
}
func init() {

View File

@ -63,7 +63,7 @@ func projectOpenCmdRun(cmd *cobra.Command, args []string) {
}
// Check the project
path := cache.GetProjectPath(project)
path := projectCache.GetProjectPath(project)
if _, err := os.Stat(path); err != nil {
plog.Fatal("Unable to open project", plog.Args("error", err))
}

View File

@ -33,7 +33,7 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) {
// Try to find project from current directory
if viper.GetBool("project.show.current") {
var err error
project, err = cache.GetProjectFromCwd()
project, err = projectCache.GetProjectFromCwd()
if err != nil {
// Not an error because we're still going to try to find a project
plog.Warn("Failed to get project from current directory", plog.Args(
@ -67,11 +67,11 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) {
WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Information"))).
Println(cache.ProjectString(project))
Println(projectCache.ProjectString(project))
fmt.Println()
if inCwd {
project.SetRepo(cache.OpenProject(cmd.Context(), project))
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
}
}

View File

@ -11,13 +11,13 @@ import (
func validProjectsFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) {
initProjectCache(cmd, args)
return cache.ProjectStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
return projectCache.ProjectStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
}
func validAliasesFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) {
initProjectCache(cmd, args)
return cache.AliasStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
return projectCache.AliasStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
}
func validProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete string) (
@ -53,8 +53,8 @@ func validLogLevelsFunc(cmd *cobra.Command, args []string, toComplete string) (
func validProjectIdFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) {
initProjectCache(cmd, args)
matchingIds := make([]string, 0, len(cache.Projects))
for _, p := range cache.Projects {
matchingIds := make([]string, 0, len(projectCache.Projects))
for _, p := range projectCache.Projects {
idString := strconv.FormatInt(int64(p.ID), 10)
if strings.HasPrefix(idString, toComplete) {
matchingIds = append(matchingIds, idString)

View File

@ -4,7 +4,7 @@ import (
"context"
fzf "github.com/ktr0731/go-fuzzyfinder"
cacheProjects "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
)
@ -40,7 +40,7 @@ func fzfFindProject(opts *fzfProjectOpts) *projects.Project {
func fzfCwdOrSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
var project *projects.Project
if opts.Search == "." {
project, _ = cache.GetProjectFromCwd()
project, _ = projectCache.GetProjectFromCwd()
} else {
project = fzfSearchProjectAliases(opts)
}
@ -51,13 +51,13 @@ func fzfCwdOrSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
// match if one is given
func fzfSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
var project *projects.Project
var alias *cacheProjects.ProjectAlias
if alias = cache.GetAliasByName(opts.Search, opts.Gitlabs...); alias != nil {
project = cache.GetProjectByAlias(alias)
var alias *cache.ProjectAlias
if alias = projectCache.GetAliasByName(opts.Search, opts.Gitlabs...); alias != nil {
project = projectCache.GetProjectByAlias(alias)
plog.Info("Perfect alias match... flawless")
} else {
// Get fuzzy if we don't have an exact match
aliases := cache.FuzzyFindAlias(opts.Search)
aliases := projectCache.FuzzyFindAlias(opts.Search)
if len(aliases) > 1 {
// If multiple aliases were found, switch over to project
// by alias mode with merging
@ -65,7 +65,7 @@ func fzfSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
project, _ = fzfProjectFromAliases(opts, aliases)
} else if len(aliases) == 1 {
alias = aliases[0]
project = cache.GetProjectByAlias(alias)
project = projectCache.GetProjectByAlias(alias)
}
}
return project
@ -75,12 +75,12 @@ func fzfSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
// a single one. Replaced by fzfProjectFromAliases in fzfSearchProjectAliases
// as merging is preferred, but can be used if it's ever desirable to
// return a single alias from all aliases
func fzfAliasFromAliases(opts *fzfProjectOpts, aliases []*cacheProjects.ProjectAlias) *cacheProjects.ProjectAlias {
var alias *cacheProjects.ProjectAlias
func fzfAliasFromAliases(opts *fzfProjectOpts, aliases []*cache.ProjectAlias) *cache.ProjectAlias {
var alias *cache.ProjectAlias
i, err := fzf.Find(
aliases,
func(i int) string {
return aliases[i].Alias + " -> " + cache.GetProjectByAlias(aliases[i]).PathWithNamespace
return aliases[i].Alias + " -> " + projectCache.GetProjectByAlias(aliases[i]).PathWithNamespace
},
fzf.WithContext(opts.Ctx),
fzf.WithHeader("Choose an Alias"),
@ -95,7 +95,7 @@ func fzfAliasFromAliases(opts *fzfProjectOpts, aliases []*cacheProjects.ProjectA
// Given a list of aliases, merge them together and use the resulting
// list of projects to return a project
func fzfProjectFromAliases(opts *fzfProjectOpts, aliases []*cacheProjects.ProjectAlias) (
func fzfProjectFromAliases(opts *fzfProjectOpts, aliases []*cache.ProjectAlias) (
*projects.Project, error) {
mergedProjects := projectsFromAliases(aliases)
if len(mergedProjects) == 1 {
@ -104,7 +104,7 @@ func fzfProjectFromAliases(opts *fzfProjectOpts, aliases []*cacheProjects.Projec
return fzfProjectFromProjects(opts, mergedProjects)
}
func projectsFromAliases(aliases []*cacheProjects.ProjectAlias) []*projects.Project {
func projectsFromAliases(aliases []*cache.ProjectAlias) []*projects.Project {
projects := make([]*projects.Project, 0)
ALIASES:
@ -115,7 +115,7 @@ ALIASES:
continue ALIASES
}
}
projects = append(projects, cache.GetProjectByAlias(a))
projects = append(projects, projectCache.GetProjectByAlias(a))
}
return projects
@ -126,9 +126,9 @@ ALIASES:
func fzfProject(opts *fzfProjectOpts) (*projects.Project, error) {
var searchableProjects []*projects.Project
if opts.MustHaveAlias {
searchableProjects = cache.GetProjectsWithAliases()
searchableProjects = projectCache.GetProjectsWithAliases()
} else {
searchableProjects = cache.Projects
searchableProjects = projectCache.Projects
}
// Filter out unwanted gitlabs if provided
searchableProjects = filterProjectsWithGitlabs(searchableProjects, opts.Gitlabs...)
@ -141,11 +141,11 @@ func fzfProjectFromProjects(opts *fzfProjectOpts, projects []*projects.Project)
i, err := fzf.Find(projects,
func(i int) string {
// Display the project along with its aliases
return cache.GetProjectStringWithAliases(projects[i])
return projectCache.GetProjectStringWithAliases(projects[i])
},
fzf.WithPreviewWindow(
func(i, width, height int) string {
return cache.ProjectString(projects[i])
return projectCache.ProjectString(projects[i])
},
),
fzf.WithContext(opts.Ctx),
@ -158,8 +158,8 @@ func fzfProjectFromProjects(opts *fzfProjectOpts, projects []*projects.Project)
}
func fzfPreviewWindow(i, w, h int) string {
p := cache.Projects[i]
return cache.ProjectString(p)
p := projectCache.Projects[i]
return projectCache.ProjectString(p)
}
func filterProjectsWithGitlabs(gitProjects []*projects.Project, gitlabs ...string) []*projects.Project {

View File

@ -7,8 +7,8 @@ import (
"strings"
"github.com/spf13/cobra"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
"golang.org/x/sys/unix"
)
@ -59,7 +59,7 @@ func initProjectCache(cmd *cobra.Command, args []string) {
os.Exit(1)
}
cacheOpts := &projects.CacheOpts{
cacheOpts := &cache.CacheOpts{
ProjectsPath: conf.ProjectPath,
Path: conf.Cache.File,
TTL: conf.Cache.Ttl,
@ -67,12 +67,12 @@ func initProjectCache(cmd *cobra.Command, args []string) {
Gitlabs: gitlabs,
Config: &conf,
}
if cache, err = projects.NewProjectCache(cacheOpts); err != nil {
if projectCache, err = cache.NewProjectCache(cacheOpts); err != nil {
plog.Error("Failed to prepare project cache", plog.Args("error", err))
os.Exit(1)
}
if err := cache.Read(); err != nil {
if err := projectCache.Read(); err != nil {
plog.Error("Cache load failed", plog.Args("error", err))
os.Exit(1)
}
@ -81,7 +81,7 @@ func initProjectCache(cmd *cobra.Command, args []string) {
}
func postProjectCache(cmd *cobra.Command, args []string) {
cache.Write()
projectCache.Write()
}
func initProjectPath(cmd *cobra.Command, args []string) {

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"fmt"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"errors"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"fmt"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"strings"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"strings"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"strings"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"bytes"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"errors"

View File

@ -1,4 +1,4 @@
package projects
package cache
import (
"context"