2024-01-15 21:02:15 +00:00
|
|
|
package cache
|
2023-12-05 21:56:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-12-07 17:08:56 +00:00
|
|
|
"github.com/pterm/pterm"
|
2023-12-10 04:19:19 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
|
2024-01-15 19:57:15 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
|
2024-01-15 20:39:35 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
2023-12-05 21:56:47 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
2024-01-15 20:39:35 +00:00
|
|
|
Projects []*projects.Project
|
2023-12-07 17:08:56 +00:00
|
|
|
Aliases []*ProjectAlias
|
2023-12-05 21:56:47 +00:00
|
|
|
Updated time.Time
|
2024-01-17 15:06:20 +00:00
|
|
|
remotes *remotes.Remotes
|
2023-12-10 04:19:19 +00:00
|
|
|
config *config.Config
|
2023-12-05 21:56:47 +00:00
|
|
|
readFromFile bool
|
2024-01-14 15:33:15 +00:00
|
|
|
lock *sync.Mutex // Lock the entire cache
|
|
|
|
contentLock *sync.Mutex // Lock projects / aliases
|
2023-12-05 21:56:47 +00:00
|
|
|
ttl time.Duration
|
|
|
|
file string
|
2023-12-07 17:08:56 +00:00
|
|
|
log *pterm.Logger
|
2023-12-09 04:13:17 +00:00
|
|
|
path string
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CacheOpts struct {
|
2023-12-09 04:13:17 +00:00
|
|
|
Path string
|
|
|
|
ProjectsPath string
|
|
|
|
TTL time.Duration
|
|
|
|
Logger *pterm.Logger
|
2024-01-16 17:48:42 +00:00
|
|
|
Remotes *remotes.Remotes
|
2023-12-10 04:19:19 +00:00
|
|
|
Config *config.Config
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:08:56 +00:00
|
|
|
// 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
|
2023-12-05 21:56:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-10 05:05:39 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:08:56 +00:00
|
|
|
// Saves the current state of the cache to disk
|
|
|
|
func (c *Cache) write() {
|
2023-12-08 21:52:26 +00:00
|
|
|
file, err := os.OpenFile(c.file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
|
2023-12-05 21:56:47 +00:00
|
|
|
if err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
c.log.Error("Failed to write cache to disk", c.log.Args("error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
d := yaml.NewEncoder(file)
|
|
|
|
if err = d.Encode(*c); err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
c.log.Error("Failed to Marshal cache to yaml", c.log.Args("error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
} else {
|
2023-12-07 17:08:56 +00:00
|
|
|
c.log.Debug("Cache saved to disk")
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-07 17:08:56 +00:00
|
|
|
func (c *Cache) Write() {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2024-01-14 15:33:15 +00:00
|
|
|
c.log.Debug("Saving cache to disk", c.log.Args(
|
|
|
|
"projects", len(c.Projects),
|
|
|
|
"aliases", len(c.Aliases),
|
|
|
|
))
|
2023-12-07 17:08:56 +00:00
|
|
|
c.write()
|
|
|
|
}
|
2023-12-05 21:56:47 +00:00
|
|
|
|
2023-12-07 17:08:56 +00:00
|
|
|
// Loads and unmarshals the project cache from disk.
|
2023-12-08 21:52:26 +00:00
|
|
|
func (c *Cache) Read() error {
|
2023-12-05 21:56:47 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2023-12-07 17:08:56 +00:00
|
|
|
c.log.Debug("Reading project cache from disk", c.log.Args("file", c.file))
|
2023-12-05 21:56:47 +00:00
|
|
|
|
|
|
|
file, err := os.Open(c.file)
|
|
|
|
if err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
c.log.Error("Failed to read project cache", c.log.Args("error", err))
|
2023-12-08 21:52:26 +00:00
|
|
|
return err
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d := yaml.NewDecoder(file)
|
|
|
|
d.Decode(c)
|
|
|
|
|
|
|
|
c.readFromFile = true
|
2023-12-08 21:52:26 +00:00
|
|
|
return nil
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:08:56 +00:00
|
|
|
// 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")
|
2024-01-15 20:39:35 +00:00
|
|
|
c.Projects = make([]*projects.Project, 0)
|
2023-12-07 17:08:56 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-01-17 15:06:20 +00:00
|
|
|
func (c *Cache) refresh(remotes ...string) {
|
2023-12-09 04:13:17 +00:00
|
|
|
c.log.Info("Loading project cache, this may take a while\n")
|
2023-12-05 21:56:47 +00:00
|
|
|
defer c.setUpdated()
|
2024-01-14 15:33:15 +00:00
|
|
|
// Fix any dangling aliases
|
|
|
|
// For backwards-compatibility only
|
|
|
|
c.setAliasRemotes()
|
|
|
|
// Retrieve and add/update projects
|
2024-01-17 15:06:20 +00:00
|
|
|
c.LoadRemotes(remotes...)
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 17:08:56 +00:00
|
|
|
// Iterates through all GitLab projects the user has access to, updating
|
|
|
|
// the project cache where necessary
|
2024-01-17 15:06:20 +00:00
|
|
|
func (c *Cache) Refresh(remotes ...string) {
|
2023-12-05 21:56:47 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2024-01-17 15:06:20 +00:00
|
|
|
c.refresh(remotes...)
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) String() string {
|
2024-01-14 15:33:15 +00:00
|
|
|
cacheString := fmt.Sprintf("Cache Updated %s: Projects %d, Aliases %d\nRemotes %d:",
|
2023-12-05 21:56:47 +00:00
|
|
|
c.Updated.String(),
|
|
|
|
len(c.Projects),
|
2024-01-14 15:33:15 +00:00
|
|
|
len(c.Aliases),
|
2024-01-17 15:06:20 +00:00
|
|
|
len(*c.remotes),
|
2024-01-14 15:33:15 +00:00
|
|
|
)
|
2024-01-17 15:06:20 +00:00
|
|
|
for _, r := range *c.remotes {
|
2024-01-16 17:48:42 +00:00
|
|
|
cacheString += " " + r.GetInfo().Host
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|
2024-01-14 15:33:15 +00:00
|
|
|
return cacheString
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 21:56:47 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := &Cache{
|
2024-01-15 20:39:35 +00:00
|
|
|
Projects: make([]*projects.Project, 0),
|
2024-01-14 15:33:15 +00:00
|
|
|
Aliases: make([]*ProjectAlias, 0),
|
|
|
|
config: opts.Config,
|
|
|
|
file: opts.Path,
|
|
|
|
ttl: opts.TTL,
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
contentLock: &sync.Mutex{},
|
|
|
|
log: opts.Logger,
|
2024-01-17 15:06:20 +00:00
|
|
|
remotes: opts.Remotes,
|
2024-01-14 15:33:15 +00:00
|
|
|
path: opts.ProjectsPath,
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func createProjectCache(path string) error {
|
|
|
|
return os.WriteFile(path, nil, 0640)
|
|
|
|
}
|