Rename projects to cache package

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

222
internal/cache/cache.go vendored Normal file
View File

@ -0,0 +1,222 @@
package cache
import (
"fmt"
"os"
"sync"
"time"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"gopkg.in/yaml.v3"
)
type Cache struct {
Projects []*projects.Project
Aliases []*ProjectAlias
Updated time.Time
config *config.Config
readFromFile bool
lock *sync.Mutex // Lock the entire cache
contentLock *sync.Mutex // Lock projects / aliases
ttl time.Duration
file string
log *pterm.Logger
path string
gitlabs *remotes.Clients
}
type CacheOpts struct {
Path string
ProjectsPath string
TTL time.Duration
Logger *pterm.Logger
Gitlabs *remotes.Clients
Config *config.Config
}
// Load cache, if already loaded and up to date, nothing is done.
// If the cache is not yet loaded from disk, it is loaded
// If the updated timestamp is beyond the set ttl, a refresh is triggered
func (c *Cache) Load() error {
var err error
if !c.readFromFile {
c.Read()
}
if time.Since(c.Updated) > c.ttl {
c.log.Info("Project cache stale, updating.")
c.Refresh()
}
return err
}
func (c *Cache) UnlockCache() {
c.log.Info("Unlocking cache")
if err := os.Remove(c.file + ".lock"); err != nil {
c.log.Fatal("Failed unlocking cache, manual rm may be necessary",
c.log.Args("error", err, "lockFile", c.file+".lock"),
)
}
}
func (c *Cache) LockCache() {
c.log.Info("Attempting to lock cache")
c.checkLock()
file, err := os.OpenFile(c.file+".lock", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
defer file.Close()
if err != nil {
c.log.Fatal("Failed to lock cache", c.log.Args("error", err))
}
}
func (c *Cache) checkLock() {
if _, err := os.Stat(c.file + ".lock"); err == nil {
c.log.Fatal("Can't manage cache, already locked")
}
}
// Saves the current state of the cache to disk
func (c *Cache) write() {
file, err := os.OpenFile(c.file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
c.log.Error("Failed to write cache to disk", c.log.Args("error", err))
}
d := yaml.NewEncoder(file)
if err = d.Encode(*c); err != nil {
c.log.Error("Failed to Marshal cache to yaml", c.log.Args("error", err))
} else {
c.log.Debug("Cache saved to disk")
}
}
func (c *Cache) Write() {
c.lock.Lock()
defer c.lock.Unlock()
c.log.Debug("Saving cache to disk", c.log.Args(
"projects", len(c.Projects),
"aliases", len(c.Aliases),
))
c.write()
}
// Loads and unmarshals the project cache from disk.
func (c *Cache) Read() error {
c.lock.Lock()
defer c.lock.Unlock()
c.log.Debug("Reading project cache from disk", c.log.Args("file", c.file))
file, err := os.Open(c.file)
if err != nil {
c.log.Error("Failed to read project cache", c.log.Args("error", err))
return err
}
d := yaml.NewDecoder(file)
d.Decode(c)
c.readFromFile = true
return nil
}
// Resets projects cache and also optionally clears
// project aliase cache. Writes to disk.
func (c *Cache) clear(clearAliases bool) {
c.log.Info("Clearing project cache")
c.Projects = make([]*projects.Project, 0)
if clearAliases {
c.log.Info("Clearing project alias cache")
c.Aliases = make([]*ProjectAlias, 0)
}
c.setUpdated()
}
func (c *Cache) Clear(clearAliases bool) {
c.lock.Lock()
defer c.lock.Unlock()
c.clear(clearAliases)
}
func (c *Cache) refresh() {
c.log.Info("Loading project cache, this may take a while\n")
defer c.setUpdated()
// Fix any dangling aliases
// For backwards-compatibility only
c.setAliasRemotes()
// Retrieve and add/update projects
c.LoadGitlabs()
}
// Iterates through all GitLab projects the user has access to, updating
// the project cache where necessary
func (c *Cache) Refresh() {
c.lock.Lock()
defer c.lock.Unlock()
c.refresh()
}
func (c *Cache) String() string {
cacheString := fmt.Sprintf("Cache Updated %s: Projects %d, Aliases %d\nRemotes %d:",
c.Updated.String(),
len(c.Projects),
len(c.Aliases),
len(*c.gitlabs),
)
for _, r := range *c.gitlabs {
cacheString += " " + r.Config.Host
}
return cacheString
}
func (c *Cache) setUpdated() {
c.Updated = time.Now()
}
func (c *Cache) SetUpdated() {
c.lock.Lock()
defer c.lock.Unlock()
c.setUpdated()
}
func (c *Cache) GetUpdated() time.Time {
return c.Updated
}
// Returns a new project cache ready to load from
// cache file.
// Cache.Load() must be called manually
func NewProjectCache(opts *CacheOpts) (*Cache, error) {
var err error
if _, err = os.Stat(opts.Path); err != nil {
err = createProjectCache(opts.Path)
}
// Combine old-and-new gitlabs
var gitlabs *remotes.Clients
if opts.Gitlabs != nil {
gitlabs = opts.Gitlabs
} else {
gitlabs = remotes.NewCLients()
}
cache := &Cache{
Projects: make([]*projects.Project, 0),
Aliases: make([]*ProjectAlias, 0),
config: opts.Config,
file: opts.Path,
ttl: opts.TTL,
lock: &sync.Mutex{},
contentLock: &sync.Mutex{},
log: opts.Logger,
gitlabs: gitlabs,
path: opts.ProjectsPath,
}
return cache, err
}
func createProjectCache(path string) error {
return os.WriteFile(path, nil, 0640)
}

79
internal/cache/cache_aliases.go vendored Normal file
View File

@ -0,0 +1,79 @@
package cache
import (
"errors"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
)
func (c *Cache) deleteAlias(alias *ProjectAlias) {
for i, a := range c.Aliases {
if a.Alias == alias.Alias {
c.Aliases = append(c.Aliases[:i], c.Aliases[i+1:]...)
}
}
}
func (c *Cache) DeleteAlias(alias *ProjectAlias) {
c.lock.Lock()
defer c.lock.Unlock()
c.deleteAlias(alias)
}
func (c *Cache) addAlias(alias string, projectID int, remote string) error {
if c.GetAliasByName(alias) != nil {
return errors.New("Failed to add alias, already exists")
}
c.Aliases = append(c.Aliases,
&ProjectAlias{
Alias: alias,
ProjectID: projectID,
Remote: remote,
})
return nil
}
func (c *Cache) AddAlias(alias string, projectID int, remote string) error {
c.lock.Lock()
defer c.lock.Unlock()
return c.addAlias(alias, projectID, remote)
}
func (c *Cache) GetProjectsWithAliases() []*projects.Project {
projectList := make([]*projects.Project, 0)
projectsFound := make([]int, 0)
for _, a := range c.Aliases {
if !slices.Contains(projectsFound, a.ProjectID) {
projectList = append(projectList, c.GetProjectByAlias(a))
projectsFound = append(projectsFound, a.ProjectID)
}
}
return projectList
}
// This method only exists if a cache was built
// before multi-remote support. Upon the first load
// with multi-remotes, this will be run first to update
// any missing alias remotes
func (c *Cache) setAliasRemotes() {
for _, alias := range c.Aliases {
if alias.Remote == "" {
c.setAliasRemote(alias)
}
}
}
func (c *Cache) setAliasRemote(alias *ProjectAlias) {
project := c.GetProjectByID(alias.ProjectID)
if project != nil {
alias.Remote = project.Remote
c.log.Debug("Fixed missing alias remote", c.log.Args(
"alias", alias.Alias,
"projectID", alias.ProjectID,
"remote", alias.Remote,
))
}
}

65
internal/cache/cache_load.go vendored Normal file
View File

@ -0,0 +1,65 @@
package cache
import (
"fmt"
"sync"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
)
func (c *Cache) LoadGitlabs() {
wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter
for _, gl := range *c.gitlabs {
c.log.Info("Loading projects for remote", c.log.Args(
"host", gl.Config.Host,
"name", gl.Config.Name,
))
opts := *remotes.DefaultListOpts
opts.Owned = &c.config.Cache.Load.OwnerOnly
projects := gl.GetTotalProjects(&opts)
// Prepare progressbar
pBar, _ := pterm.DefaultProgressbar.
WithShowPercentage(true).
WithTotal(projects).
WithWriter(writer.NewWriter()).
WithMaxWidth(100).
Start(gl.Config.Host)
wg.Add(1)
go c.LoadGitlab(gl, wg, pBar, projects)
}
fmt.Println("")
writer.Start()
wg.Wait()
writer.Stop()
fmt.Println("")
}
func (c *Cache) LoadGitlab(client *remotes.Client, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, projects int) {
defer wg.Done()
progressInfo := client.StreamProjects(c.config.Cache.Load.OwnerOnly, projects)
for {
select {
case p := <-progressInfo.ProgressChan:
pBar.Add(p.Projects)
case p := <-progressInfo.ProjectsChan:
c.AddProjects(p...)
case e := <-progressInfo.ErrorChan:
c.log.Error("Fetch GitLab projects error", c.log.Args("error", e, "gitlab", client.Config.Name))
case <-client.Ctx.Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", client.Ctx.Err()))
return
case <-progressInfo.DoneChan:
return
}
}
}

76
internal/cache/cache_projects.go vendored Normal file
View File

@ -0,0 +1,76 @@
package cache
import (
"strings"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
)
func (c *Cache) AddProjects(gitProjects ...*projects.Project) {
c.contentLock.Lock()
defer c.contentLock.Unlock()
for _, p := range gitProjects {
var existing *projects.Project
sameID := c.GetProjectsByID(p.ID)
// If there is only one by ID, we don't
// have to worry about matching remotes.
// If there are more than one, either match
// remotes or update the remote if it was never
// set due to being build from old code
// New caches should never have empty remotes.
if len(sameID) == 1 {
existing = sameID[0]
} else if len(sameID) > 1 {
for _, pr := range sameID {
if pr.Remote == p.Remote || pr.Remote == "" {
existing = pr
break
}
}
}
// Add a new one, or update if changed
if existing == nil {
c.Projects = append(c.Projects, p)
} else if *existing != *p {
*existing = *p
}
}
}
// This command will only dump projects that have
// been cloned locally. Setting all to true will list all projects
func (c *Cache) DumpString(all bool, search string, gitlabs ...string) string {
str := strings.Builder{}
var term string
if all {
term = pterm.Bold.Sprint("Projects")
} else {
term = pterm.Bold.Sprint("Local Projects")
}
str.WriteString(c.String() + "\n\n" + term + ":\n")
projects := c.FuzzyFindProjects(search)
for _, project := range projects {
if !all && !c.IsProjectCloned(project) {
continue
} else if len(gitlabs) > 0 && !slices.Contains(gitlabs, project.Remote) {
continue
}
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")
str.WriteString(project.PathWithNamespace + ")\n")
aliases := c.GetProjectAliases(project)
if len(aliases) > 0 {
str.WriteString(pterm.FgLightGreen.Sprint(" aliases:"))
for _, a := range aliases {
str.WriteString(" [" + pterm.FgCyan.Sprint(a.Alias) + "]")
}
str.WriteRune('\n')
}
str.WriteString(pterm.FgLightMagenta.Sprint(" remote: "))
str.WriteString(project.Remote + "\n")
}
return str.String()
}

57
internal/cache/fuzz.go vendored Normal file
View File

@ -0,0 +1,57 @@
package cache
import (
"strings"
"github.com/lithammer/fuzzysearch/fuzzy"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
)
// Performs a fuzzy find on the input string, returning the closest
// matched based on its Levenshtein distance, along with an integer
// indicating number of matches found
func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias {
ranks := fuzzy.RankFindFold(name, c.AliasStrings(""))
if ranks.Len() == 1 {
c.log.Debug("Fuzzy found alias result",
c.log.Args(
"searchTerm", ranks[0].Source,
"foundAlias", ranks[0].Target,
"levenshteinDistance", ranks[0].Distance,
))
} else if ranks.Len() > 1 {
found := make([]string, ranks.Len())
for i, r := range ranks {
found[i] = r.Target
}
c.log.Debug("Fuzzy found multiple aliases, try being more specific",
c.log.Args("foundAliases", strings.Join(found, ", ")))
}
var aliases []*ProjectAlias
if ranks.Len() > 0 {
aliases = make([]*ProjectAlias, ranks.Len())
for i, r := range ranks {
aliases[i] = c.GetAliasByName(r.Target)
}
}
return aliases
}
// Returns all matching projects by fuzzy find term
// Matches NameWithNamespace and Aliases
func (c *Cache) FuzzyFindProjects(search string) []*projects.Project {
projects := make([]*projects.Project, 0, len(c.Projects))
for _, p := range c.Projects {
if fuzzy.MatchFold(search, p.NameWithNamespace) {
projects = append(projects, p)
continue
}
for _, a := range c.GetProjectAliases(p) {
if fuzzy.MatchFold(search, a.Alias) {
projects = append(projects, p)
continue
}
}
}
return projects
}

83
internal/cache/projects.go vendored Normal file
View File

@ -0,0 +1,83 @@
package cache
import (
"strings"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
)
func (c *Cache) ProjectString(p *projects.Project) string {
info := strings.Builder{}
info.WriteString(pterm.LightGreen("\n--------------\n"))
info.WriteString(pterm.Bold.Sprint(p.Name))
info.WriteRune('\n')
if p.Description != "" {
info.WriteString(p.Description)
info.WriteRune('\n')
}
info.WriteString("\nPath: " + pterm.LightGreen(p.PathWithNamespace))
info.WriteString("\nProjectID: " + pterm.LightGreen(p.ID))
info.WriteString("\nURL: " + pterm.LightGreen(p.WebURL))
info.WriteString("\nLastActivity: " + pterm.LightMagenta(p.LastActivityAt.String()))
info.WriteString("\nAliases: ")
aliases := c.GetProjectAliases(p)
info.WriteString(ProjectAliasesString(aliases))
info.WriteString(pterm.LightGreen("\n--------------\n"))
return info.String()
}
func (c *Cache) ProjectStrings(prefix string) []string {
projects := make([]string, 0, len(c.Projects))
for _, p := range c.Projects {
if strings.HasPrefix(p.NameWithNamespace, prefix) {
projects = append(projects, p.NameWithNamespace)
}
}
return slices.Clip(projects)
}
func (c *Cache) GetProjectByPath(path string) *projects.Project {
for _, p := range c.Projects {
if p.PathWithNamespace == path {
return p
}
}
return nil
}
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *projects.Project {
for _, p := range c.Projects {
if p.ID == id && p.Remote == remote {
return p
}
}
return nil
}
func (c *Cache) GetProjectByID(id int) *projects.Project {
for _, p := range c.Projects {
if p.ID == id {
return p
}
}
return nil
}
// Plural form of GetProjectByID
// Since multiple remotes may have the same project ID,
// this will return all matching
func (c *Cache) GetProjectsByID(id int) []*projects.Project {
projects := make([]*projects.Project, 0)
for _, p := range c.Projects {
if p.ID == id {
projects = append(projects, p)
}
}
return projects
}

106
internal/cache/projects_alias.go vendored Normal file
View File

@ -0,0 +1,106 @@
package cache
import (
"bytes"
"fmt"
"strings"
"text/tabwriter"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
)
type ProjectAlias struct {
Alias string
ProjectID int
Remote string
}
func (c *Cache) GetProjectAliasStrings(project *projects.Project) []string {
aliases := c.GetProjectAliases(project)
strings := make([]string, len(aliases))
for i, a := range c.GetProjectAliases(project) {
strings[i] = a.Alias
}
return strings
}
func (c *Cache) GetProjectStringWithAliases(project *projects.Project) string {
aliases := c.GetProjectAliasStrings(project)
return fmt.Sprintf("%s (%s) -> %s",
project.Name,
strings.Join(aliases, ", "),
project.PathWithNamespace,
)
}
func ProjectAliasesString(aliases []*ProjectAlias) string {
var str string
for _, a := range aliases {
str += "[" + pterm.LightCyan(a.Alias) + "] "
}
return strings.Trim(str, " ")
}
func (c *Cache) AliasesByProjectString() string {
var str bytes.Buffer
w := new(tabwriter.Writer)
w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight)
for _, p := range c.GetProjectsWithAliases() {
var pa string
pa += pterm.LightBlue("- ")
pa += fmt.Sprint(pterm.Bold.Sprint(p.String()) + " \t ")
pa += fmt.Sprint(ProjectAliasesString(c.GetProjectAliases(p)))
fmt.Fprintln(w, pa)
}
w.Flush()
return str.String()
}
func (c *Cache) AliasStrings(prefix string) []string {
aliases := make([]string, 0, len(c.Aliases))
for _, a := range c.Aliases {
if strings.HasPrefix(a.Alias, prefix) {
aliases = append(aliases, a.Alias)
}
}
return aliases
}
func (c *Cache) GetAliasByName(name string, gitlabs ...string) *ProjectAlias {
for _, a := range c.Aliases {
if len(gitlabs) > 0 && !slices.Contains(gitlabs, a.Remote) {
continue
}
if name == a.Alias {
return a
}
}
return nil
}
func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *projects.Project {
if alias == nil {
return nil
}
for _, p := range c.Projects {
if p.ID == alias.ProjectID && p.Remote == alias.Remote {
return p
}
}
return nil
}
func (c *Cache) GetProjectAliases(project *projects.Project) []*ProjectAlias {
aliases := make([]*ProjectAlias, 0)
for _, alias := range c.Aliases {
if alias.ProjectID == project.ID {
aliases = append(aliases, alias)
}
}
return aliases
}

68
internal/cache/projects_fs.go vendored Normal file
View File

@ -0,0 +1,68 @@
package cache
import (
"errors"
"os"
"path/filepath"
"strings"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
)
func (c *Cache) GoTo(project *projects.Project) {
pPath := c.GetProjectPath(project)
c.log.Debug("Going to project", c.log.Args(
"project", project.String(),
"path", pPath,
))
if _, err := os.Stat(pPath); err != nil {
c.log.Info("Preparing project path")
c.PrepProjectPath(pPath)
}
os.Chdir(filepath.Dir(pPath))
}
func (c *Cache) GetProjectFromCwd() (*projects.Project, error) {
var project *projects.Project
cwd, err := os.Getwd()
if err != nil {
return project, err
} else if !strings.HasPrefix(cwd, c.path) {
return project, errors.New("Not in any project path")
}
// Strip projects dir from path
pathWithNs := cwd[len(c.path)+1:]
c.log.Debug("Fetching project from current path", c.log.Args(
"cwd", cwd, "pathWithNamespace", pathWithNs,
))
project = c.GetProjectByPath(pathWithNs)
return project, nil
}
func (c *Cache) IsProjectCloned(p *projects.Project) bool {
_, err := os.Stat(c.GetProjectPath(p) + "/.git")
if err == nil {
return true
}
return false
}
func (c *Cache) PrepProjectPath(path string) {
if err := os.MkdirAll(path, 0750); err != nil {
c.log.Fatal("Failed to prepare project path", c.log.Args(
"path", path,
"error", err,
))
}
}
func (c *Cache) GetProjectPath(p *projects.Project) string {
return c.path + "/" + p.SanitizedPath()
}

60
internal/cache/projects_git.go vendored Normal file
View File

@ -0,0 +1,60 @@
package cache
import (
"context"
"time"
git "github.com/go-git/go-git/v5"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
)
const gitCloneTimeoutSecs = 30
// Will either read in the current repo, preparing a report
// on its current state, or will clone the project if it has not
// already been cloned in its path
func (c *Cache) OpenProject(ctx context.Context, project *projects.Project) *git.Repository {
path := c.GetProjectPath(project)
cloneCtx, cncl := context.WithDeadline(ctx, time.Now().Add(gitCloneTimeoutSecs*time.Second))
defer cncl()
var repo *git.Repository
var err error
if repo, err = git.PlainOpen(path); err != nil {
if err == git.ErrRepositoryNotExists {
c.log.Debug("Project not yet cloned")
}
}
if repo == nil {
// Check to make sure we can connect before we time out
// shouldn't be necessary, but go-git does not properly
// honor its context
if err := project.CheckHost(projects.GitlabProtoSSH); err != nil {
c.log.Fatal("Git remote unreachable, giving up", c.log.Args("error", err))
}
c.log.Info("Cloning project from remote", c.log.Args("remote", project.SSHURLToRepo))
repo, err = git.PlainCloneContext(cloneCtx, path, false, &git.CloneOptions{
URL: project.SSHURLToRepo,
})
if err == git.ErrRepositoryAlreadyExists {
c.log.Debug("Skipping clone, already exists")
} else if err != nil {
c.log.Fatal("Failed to open git project", c.log.Args(
"error", err,
))
}
}
head, err := repo.Head()
if err != nil {
c.log.Fatal("Something went wrong, unable to fetch HEAD from repo", c.log.Args(
"error", err,
))
}
c.log.Debug("Repository ready", c.log.Args("branch", head.Name().String()))
return repo
}