93 lines
2.5 KiB
Go

package config
import (
"sync"
"time"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
)
type Config struct {
// Named keys above maintained for backwards compatibility
// Ideally only Gitlabs is used
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 {
DisplayName string `yaml:"displanName,omitempty" json:"displanName,omitempty"`
Binary string `yaml:"binary,omitempty" json:"binary,omitempty"`
OpenFlags string `yaml:"openFlags,omitempty" json:"openFlags,omitempty"`
OpenDirectory bool `yaml:"openDirectory" json:"openDirectory" description:"Don't open well-known files, open directory"`
}
type loadConfig struct {
OwnerOnly bool `yaml:"ownerOnly" json:"ownerOnly"`
}
type cacheConfig struct {
Ttl time.Duration `yaml:"ttl,omitempty" json:"ttl,omitempty"`
File string `yaml:"file,omitempty" json:"file,omitempty"`
Load loadConfig `yaml:"load" json:"load"`
Unlock struct {
Force bool `yaml:"force" json:"force"`
} `yaml:"unlock" json:"unlock"`
Clear struct {
ClearAliases bool `yaml:"clearAliases,omitempty" json:"clearAliases,omitempty"`
} `yaml:"clear" json:"clear"`
}
var DefaultConfig = Config{
LogLevel: "warn",
ProjectPath: "~/work/projects",
CloneTimeout: "120s",
Remotes: []info.RemoteInfo{{
Host: "https://gitlab.com",
Token: "yourtokenhere",
CloneProto: info.CloneProtoSSH,
Name: "GitLab",
Type: "gitlab",
}},
Cache: cacheConfig{
Ttl: 168 * time.Hour,
Load: loadConfig{
OwnerOnly: true,
},
},
}
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
}