wip
This commit is contained in:
@ -7,30 +7,33 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
"golang.org/x/exp/slog"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
Projects []*gitlab.ProjectInfo
|
||||
Aliases []*gitlab.ProjectAlias
|
||||
Projects []*gitlab.Project
|
||||
Aliases []*ProjectAlias
|
||||
Updated time.Time
|
||||
readFromFile bool
|
||||
lock *sync.Mutex
|
||||
ttl time.Duration
|
||||
file string
|
||||
log *slog.Logger
|
||||
log *pterm.Logger
|
||||
gitlab *gitlab.Client
|
||||
}
|
||||
|
||||
type CacheOpts struct {
|
||||
Path string
|
||||
TTL time.Duration
|
||||
Logger *slog.Logger
|
||||
Logger *pterm.Logger
|
||||
Gitlab *gitlab.Client
|
||||
}
|
||||
|
||||
// Load cache, if already loaded and
|
||||
// up to date,
|
||||
// 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 {
|
||||
@ -43,27 +46,34 @@ func (c *Cache) Load() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Cache) Write() {
|
||||
// Saves the current state of the cache to disk
|
||||
func (c *Cache) write() {
|
||||
file, err := os.OpenFile(c.file, os.O_RDWR, fs.ModeAppend)
|
||||
if err != nil {
|
||||
c.log.Error("Failed to write cache to disk", "error", err)
|
||||
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", "error", err)
|
||||
c.log.Error("Failed to Marshal cache to yaml", c.log.Args("error", err))
|
||||
} else {
|
||||
c.log.Info("Cache saved to disk")
|
||||
c.log.Debug("Cache saved to disk")
|
||||
}
|
||||
}
|
||||
func (c *Cache) Write() {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.write()
|
||||
}
|
||||
|
||||
// Loads and unmarshals the project cache from disk.
|
||||
func (c *Cache) Read() {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.log.Info("Reading project cache from disk", "file", c.file)
|
||||
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", "error", err)
|
||||
c.log.Error("Failed to read project cache", c.log.Args("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -78,11 +88,32 @@ func (c *Cache) Read() {
|
||||
c.log.Debug(c.String())
|
||||
}
|
||||
|
||||
// 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([]*gitlab.Project, 0)
|
||||
if clearAliases {
|
||||
c.log.Info("Clearing project alias cache")
|
||||
c.Aliases = make([]*ProjectAlias, 0)
|
||||
}
|
||||
c.setUpdated()
|
||||
c.log.Debug(c.String())
|
||||
}
|
||||
func (c *Cache) Clear(clearAliases bool) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.clear(clearAliases)
|
||||
}
|
||||
|
||||
func (c *Cache) refresh() {
|
||||
c.log.Info("Refreshing project cache, this may take a while")
|
||||
defer c.setUpdated()
|
||||
c.LoadProjects()
|
||||
}
|
||||
|
||||
// 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()
|
||||
@ -121,12 +152,13 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
|
||||
}
|
||||
|
||||
cache := &Cache{
|
||||
Projects: make([]*gitlab.ProjectInfo, 0),
|
||||
Aliases: make([]*gitlab.ProjectAlias, 0),
|
||||
Projects: make([]*gitlab.Project, 0),
|
||||
Aliases: make([]*ProjectAlias, 0),
|
||||
file: opts.Path,
|
||||
ttl: opts.TTL,
|
||||
lock: &sync.Mutex{},
|
||||
log: opts.Logger,
|
||||
gitlab: opts.Gitlab,
|
||||
}
|
||||
|
||||
return cache, err
|
||||
|
53
internal/projects/projects.go
Normal file
53
internal/projects/projects.go
Normal file
@ -0,0 +1,53 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
)
|
||||
|
||||
type ProjectAlias struct {
|
||||
Alias string
|
||||
ProjectID string
|
||||
Project *gitlab.Project
|
||||
}
|
||||
|
||||
func (c *Cache) LoadProjects() {
|
||||
progressInfo := c.gitlab.StreamProjects()
|
||||
c.Projects = make([]*gitlab.Project, 0)
|
||||
|
||||
pBar := pterm.DefaultProgressbar.
|
||||
WithShowPercentage(true).
|
||||
WithTotal(-1).
|
||||
WithTitle("Listing GitLab Projects").
|
||||
WithMaxWidth(0)
|
||||
|
||||
defer pBar.Stop()
|
||||
|
||||
var curProjects int
|
||||
for {
|
||||
select {
|
||||
case p := <-progressInfo.ProgressChan:
|
||||
if pBar.Total == -1 {
|
||||
pBar = pBar.WithTotal(p.TotalProjects)
|
||||
pBar, _ = pBar.Start()
|
||||
}
|
||||
// This sucks, has to be a better way, and why is the logger incompatible
|
||||
// with the progressbar?
|
||||
pterm.Debug.Println(fmt.Sprintf("Update received: %#v", p))
|
||||
pBar.Add(p.Projects - curProjects)
|
||||
curProjects = p.Projects
|
||||
case p := <-progressInfo.ProjectsChan:
|
||||
c.Projects = append(c.Projects, p...)
|
||||
case e := <-progressInfo.ErrorChan:
|
||||
c.log.Error("Fetch GitLab projects error", c.log.Args("error", e))
|
||||
case <-c.gitlab.Ctx.Done():
|
||||
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", c.gitlab.Ctx.Err()))
|
||||
return
|
||||
case <-progressInfo.DoneChan:
|
||||
c.log.Info("Project load complete")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user