s
This commit is contained in:
+11
-7
@@ -27,6 +27,7 @@ import (
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"simplegit/common"
|
||||
@@ -76,7 +77,7 @@ var (
|
||||
|
||||
type LocalState struct {
|
||||
engine *xorm.Engine
|
||||
root string
|
||||
cfg *common.Config
|
||||
// skipAuth bypasses the DB: AccessBy* always allow, RepoPath resolves any
|
||||
// path under root. engine is nil in this mode.
|
||||
skipAuth bool
|
||||
@@ -92,17 +93,19 @@ func init() {
|
||||
sql.Register(sqliteDriver, &sqlite.Driver{})
|
||||
}
|
||||
|
||||
func OpenWithDSN(root string, driver string, dsn string) (*LocalState, error) {
|
||||
engine, err := InitEngine(driver, dsn)
|
||||
func Open(cfg *common.Config) (*LocalState, error) {
|
||||
engine, err := InitEngine(cfg.Driver(), cfg.DSN())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("state: init engine: %w", err)
|
||||
}
|
||||
if driver == sqliteDriver {
|
||||
if cfg.Driver() == sqliteDriver {
|
||||
engine.SetMaxOpenConns(1)
|
||||
}
|
||||
log.Printf("state: RepoRoot: %s", cfg.RepoRoot())
|
||||
log.Printf("state: DB engine %s opened, %s", cfg.Driver(), cfg.DSN())
|
||||
return &LocalState{
|
||||
engine: engine,
|
||||
root: root,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -164,7 +167,7 @@ func (s *LocalState) loadOrCreateHostKey(ty string) (ssh.Signer, error) {
|
||||
default:
|
||||
return nil, errors.New("loadOrCreateHostKey")
|
||||
}
|
||||
hostKeyPath := filepath.Join(s.root, fmt.Sprintf("host_key_%v.pem", ty))
|
||||
hostKeyPath := filepath.Join(s.cfg.Root, fmt.Sprintf("host_key_%v.pem", ty))
|
||||
if data, err := os.ReadFile(hostKeyPath); err == nil {
|
||||
return ssh.ParsePrivateKey(data)
|
||||
} else if !os.IsNotExist(err) {
|
||||
@@ -214,13 +217,14 @@ func (s *LocalState) findRepo(owner, name string) (*Repo, error) {
|
||||
// repo is registered in the DB -- the DB is the authority, not the filesystem.
|
||||
// In skip-auth mode the DB check is skipped (any path under root resolves).
|
||||
func (s *LocalState) RepoPath(owner, name string) (string, error) {
|
||||
name = normalizeName(name)
|
||||
rel := relPath(owner, name)
|
||||
if !s.skipAuth {
|
||||
if _, err := s.findRepo(owner, name); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return common.Resolve(s.root, rel)
|
||||
return common.Resolve(s.cfg.RepoRoot(), rel)
|
||||
}
|
||||
|
||||
// Open returns a gitcmd.Repository for owner/name. The repo must be registered
|
||||
|
||||
Reference in New Issue
Block a user