forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
65 lines (55 loc) · 1.64 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package baur
import (
"fmt"
"os"
"path/filepath"
"github.com/simplesurance/baur/v1/cfg"
"github.com/simplesurance/baur/v1/internal/fs"
)
// Repository represents an repository containing applications
type Repository struct {
Path string
CfgPath string
Cfg *cfg.Repository
SearchDepth int
// TODO: remove PSQLURL
PSQLURL string
}
// FindRepositoryCfg searches for a repository config file. The search starts in
// the passed directory and traverses the parent directory down to '/'.
// It returns the path to the first found repository configuration file.
func FindRepositoryCfg(dir string) (string, error) {
return fs.FindFileInParentDirs(dir, RepositoryCfgFile)
}
// FindRepositoryCfgCwd searches for a repository config file in the current directory
// and all it's parents.
// It returns the path to the first found repository configuration file.
func FindRepositoryCfgCwd() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return FindRepositoryCfg(cwd)
}
// NewRepository reads the configuration file 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,
PSQLURL: repoCfg.Database.PGSQLURL,
}
return &r, nil
}