support config clone timeout, do upgrades

This commit is contained in:
2025-04-23 11:01:52 -04:00
parent d291489247
commit 5f0db640df
7 changed files with 178 additions and 148 deletions

View File

@ -122,7 +122,7 @@ func (c *Cache) Read() error {
d.Decode(c)
// Perform migrations
err, migrated := c.doMigrations()
migrated, err := c.doMigrations()
if err != nil {
c.log.Error("Failed to run cache migrations",
c.log.Args(

View File

@ -11,7 +11,7 @@ import (
// Migrations funcs should return errors along with
// number of records updated
type migrationFunc func(c *Cache) (error, int)
type migrationFunc func(c *Cache) (int, error)
// Registry of migrations by version
var migrations = map[string]map[string]migrationFunc{
@ -24,20 +24,20 @@ var migrations = map[string]map[string]migrationFunc{
// of cache read from disk.
// Does not check to ensure migrations were successful,
// only checks if a version has been achieved
func (c *Cache) DoMigrations() (error, int) {
func (c *Cache) DoMigrations() (int, error) {
c.lock.Lock()
defer c.lock.Unlock()
return c.doMigrations()
}
func (c *Cache) doMigrations() (error, int) {
func (c *Cache) doMigrations() (int, error) {
var errs error
var migrated int
for version, migrationFuncs := range migrations {
var funcMigrated int
if semver.Compare(c.CacheVersion, version) < 0 {
for name, migration := range migrationFuncs {
err, numMigrated := migration(c)
numMigrated, err := migration(c)
if err != nil {
errs = errors.Join(
errs,
@ -57,10 +57,10 @@ func (c *Cache) doMigrations() (error, int) {
migrated += funcMigrated
}
return errs, migrated
return migrated, errs
}
func v010_aliases(c *Cache) (error, int) {
func v010_aliases(c *Cache) (int, error) {
var aliasesMigrated int
var errs error
for i, a := range c.Aliases {
@ -74,5 +74,5 @@ func v010_aliases(c *Cache) (error, int) {
aliasesMigrated++
}
}
return errs, aliasesMigrated
return aliasesMigrated, errs
}

View File

@ -9,14 +9,12 @@ import (
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
)
const gitCloneTimeoutSecs = 60
// 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))
cloneCtx, cncl := context.WithDeadline(ctx, time.Now().Add(c.config.GetCloneTimeout()))
defer cncl()
var repo *git.Repository

View File

@ -1,6 +1,7 @@
package config
import (
"sync"
"time"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
@ -9,14 +10,17 @@ import (
type Config struct {
// Named keys above maintained for backwards compatibility
// Ideally only Gitlabs is used
Remotes []info.RemoteInfo
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
ProjectPath string `yaml:"projectPath" json:"projectPath"`
Cache cacheConfig `yaml:"cache" json:"cache"`
Dump struct {
Full bool `yaml:"full" json:"full"`
Remotes []info.RemoteInfo `yaml:"remotes" json:"remotes,omitempty"`
LogLevel string `yaml:"logLevel" json:"logLevel,omitempty" enum:"info,warn,debug,error"`
ProjectPath string `yaml:"projectPath" json:"projectPath,omitempty"`
Cache cacheConfig `yaml:"cache" json:"cache"`
CloneTimeout string `yaml:"cloneTimeout" json:"cloneTimeout,omitempty" default:"120s"`
cloneTimeout time.Duration
Dump struct {
Full bool `yaml:"full" json:"full,omitempty"`
} `yaml:"dump" json:"dump"`
Editor editorConfig `yaml:"editor" json:"editor"`
lock *sync.RWMutex
}
type editorConfig struct {
@ -39,12 +43,13 @@ type cacheConfig struct {
} `yaml:"unlock" json:"unlock"`
Clear struct {
ClearAliases bool `yaml:"clearAliases,omitempty" json:"clearAliases,omitempty"`
} `yaml:"clear,omitempty" json:"clear,omitempty"`
} `yaml:"clear" json:"clear"`
}
var DefaultConfig = Config{
LogLevel: "warn",
ProjectPath: "~/work/projects",
LogLevel: "warn",
ProjectPath: "~/work/projects",
CloneTimeout: "120s",
Remotes: []info.RemoteInfo{{
Host: "https://gitlab.com",
Token: "yourtokenhere",
@ -59,3 +64,29 @@ var DefaultConfig = Config{
},
},
}
func NewConfig() *Config {
return &Config{
lock: &sync.RWMutex{},
}
}
// Perform updates to config
func (c *Config) Prepare() (err error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.CloneTimeout == "" {
c.CloneTimeout = DefaultConfig.CloneTimeout
}
c.cloneTimeout, err = time.ParseDuration(c.CloneTimeout)
return err
}
func (c *Config) GetCloneTimeout() time.Duration {
c.lock.RLock()
defer c.lock.RUnlock()
return c.cloneTimeout
}