/
repository.go
50 lines (42 loc) · 1.24 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package baur
import (
"fmt"
"path/filepath"
"github.com/simplesurance/baur/v2/internal/fs"
"github.com/simplesurance/baur/v2/pkg/cfg"
)
// Repository represents a baur repository.
type Repository struct {
Path string
CfgPath string
Cfg *cfg.Repository
SearchDepth int
}
// FindRepositoryCfg searches for a repository config file. The search starts
// in dir and traverses the parent directory down to the root.
// It returns the path to the first found repository configuration file.
func FindRepositoryCfg(dir string) (string, error) {
return fs.FindFileInParentDirs(dir, RepositoryCfgFile)
}
// NewRepository parses the repository configuration file cfgPath and returns a
// Repository.
func NewRepository(cfgPath string) (*Repository, error) {
repoCfg, err := cfg.RepositoryFromFile(cfgPath)
if err != nil {
return nil, fmt.Errorf(
"reading repository config %s failed: %w", cfgPath, err)
}
err = repoCfg.Validate()
if err != nil {
return nil, fmt.Errorf(
"validating repository config %q failed: %w", cfgPath, err)
}
repoPath := filepath.Dir(cfgPath)
r := Repository{
Cfg: repoCfg,
CfgPath: cfgPath,
Path: repoPath,
SearchDepth: repoCfg.Discover.SearchDepth,
}
return &r, nil
}