Begin caching, implement commands
This commit is contained in:
16
internal/config/config.go
Normal file
16
internal/config/config.go
Normal file
@ -0,0 +1,16 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type Config struct {
|
||||
GitlabHost string `yaml:"gitlabHost" json:"gitlabHost"`
|
||||
GitlabToken string `yaml:"gitlabToken" json:"gitlabToken"`
|
||||
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
|
||||
ProjectPath string `yaml:"projectPath" json:"projectPath"`
|
||||
Cache cacheConfig `yaml:"cache" json:"cache"`
|
||||
}
|
||||
|
||||
type cacheConfig struct {
|
||||
Ttl time.Duration `yaml:"ttl" json:"ttl"`
|
||||
File string
|
||||
}
|
32
internal/gitlab/gitlab.go
Normal file
32
internal/gitlab/gitlab.go
Normal file
@ -0,0 +1,32 @@
|
||||
package gitlab
|
||||
|
||||
import "github.com/xanzy/go-gitlab"
|
||||
|
||||
type Client struct {
|
||||
gitlab *gitlab.Client
|
||||
}
|
||||
|
||||
type ProjectInfo struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Id int `yaml:"id" json:"id"`
|
||||
Path string `yaml:"path" json:"path"`
|
||||
URI string `yaml:"uri" json:"uri"`
|
||||
Description string `yaml:"description" json:"description"`
|
||||
}
|
||||
|
||||
type ProjectAlias struct {
|
||||
Alias string
|
||||
ProjectID string
|
||||
Project *ProjectInfo
|
||||
}
|
||||
|
||||
func NewGitlabClient(host, token string) (*Client, error) {
|
||||
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitlabClient := &Client{
|
||||
gitlab: client,
|
||||
}
|
||||
return gitlabClient, nil
|
||||
}
|
137
internal/projects/cache.go
Normal file
137
internal/projects/cache.go
Normal file
@ -0,0 +1,137 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"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
|
||||
Updated time.Time
|
||||
readFromFile bool
|
||||
lock *sync.Mutex
|
||||
ttl time.Duration
|
||||
file string
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
type CacheOpts struct {
|
||||
Path string
|
||||
TTL time.Duration
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
// Load cache, if already loaded and
|
||||
// up to date,
|
||||
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) 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)
|
||||
}
|
||||
d := yaml.NewEncoder(file)
|
||||
if err = d.Encode(*c); err != nil {
|
||||
c.log.Error("Failed to Marshal cache to yaml", "error", err)
|
||||
} else {
|
||||
c.log.Info("Cache saved to disk")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) Read() {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.log.Info("Reading project cache from disk", "file", c.file)
|
||||
|
||||
file, err := os.Open(c.file)
|
||||
if err != nil {
|
||||
c.log.Error("Failed to read project cache", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
d := yaml.NewDecoder(file)
|
||||
d.Decode(c)
|
||||
|
||||
if time.Since(c.Updated) > c.ttl {
|
||||
c.refresh()
|
||||
}
|
||||
|
||||
c.readFromFile = true
|
||||
c.log.Debug(c.String())
|
||||
}
|
||||
|
||||
func (c *Cache) refresh() {
|
||||
c.log.Info("Refreshing project cache, this may take a while")
|
||||
defer c.setUpdated()
|
||||
}
|
||||
|
||||
func (c *Cache) Refresh() {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.refresh()
|
||||
}
|
||||
|
||||
func (c *Cache) String() string {
|
||||
return fmt.Sprintf("Cache Updated %s: Projects %d, Aliases %d",
|
||||
c.Updated.String(),
|
||||
len(c.Projects),
|
||||
len(c.Aliases))
|
||||
}
|
||||
|
||||
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{
|
||||
Projects: make([]*gitlab.ProjectInfo, 0),
|
||||
Aliases: make([]*gitlab.ProjectAlias, 0),
|
||||
file: opts.Path,
|
||||
ttl: opts.TTL,
|
||||
lock: &sync.Mutex{},
|
||||
log: opts.Logger,
|
||||
}
|
||||
|
||||
return cache, err
|
||||
}
|
||||
|
||||
func createProjectCache(path string) error {
|
||||
return os.WriteFile(path, nil, 0640)
|
||||
}
|
Reference in New Issue
Block a user