Files
simplegit/common/config.go
T
2026-07-17 05:55:31 -04:00

113 lines
2.0 KiB
Go

package common
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync/atomic"
)
type Config struct {
Root string
RPCAddr string
SSHAddr string
ManageAddr string
AuthURL string
DBUri string
SkipAuth bool
preparedDbUri string
prepared atomic.Bool
driver string
dsn string
}
func (c *Config) RepoRoot() string {
return filepath.Join(c.Root, "repos")
}
func (c *Config) TemplateDir() string {
return filepath.Join(c.Root, "template")
}
func (c *Config) HookSock() string {
return filepath.Join(c.Root, "hook.sock")
}
func (c *Config) Prepare() error {
if c.prepared.Load() {
return nil
}
var err error
if c.DBUri == "" {
return errors.New("simplegit: -db is required (e.g. sqlite://state.db)")
}
if err := os.MkdirAll(c.RepoRoot(), 0o755); err != nil {
return fmt.Errorf("simplegit: Failed to create data root: %s (%v)", c.RepoRoot(), err)
}
c.driver, c.dsn, c.preparedDbUri, err = ParseDBURI(c.Root, c.DBUri)
if err != nil {
return errors.New("simplegit: ParseDBURI failed")
}
if c.RPCAddr == "" {
return errors.New("simplegit: -rpc is required (the HTTP server is this binary's only listener)")
}
if c.ManageAddr == "" {
return fmt.Errorf("*manageAddr == nil")
}
c.prepared.Store(true)
return nil
}
func (c *Config) Driver() string {
if !c.prepared.Load() {
panic("Config.Driver called before Prepare")
}
return c.driver
}
func (c *Config) DSN() string {
if !c.prepared.Load() {
panic("Config.DSN called before Prepare")
}
return c.dsn
}
func (c *Config) DBURI() string {
if !c.prepared.Load() {
panic("Config.PreparedDBUri called before Prepare")
}
return c.preparedDbUri
}
func normalizeName(name string) string {
return strings.TrimSuffix(name, ".git")
}
func relPath(owner, name string) string {
return owner + "/" + normalizeName(name) + ".git"
}
func (s *Config) RepoPath(owner, name string) (string, error) {
name = normalizeName(name)
rel := relPath(owner, name)
return Resolve(s.RepoRoot(), rel)
}