-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrepository.go
174 lines (143 loc) · 4.41 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package baur
import (
"os"
"path"
"path/filepath"
"github.com/pkg/errors"
"github.com/simplesurance/baur/cfg"
"github.com/simplesurance/baur/fs"
"github.com/simplesurance/baur/git"
)
// Repository represents an repository containing applications
type Repository struct {
Path string
CfgPath string
AppSearchDirs []string
SearchDepth int
gitCommitID string
gitWorktreeIsDirty *bool
PSQLURL string
}
// FindRepository searches for a repository config file. The search starts in
// the passed directory and traverses the parent directory down to '/'. The first found repository
// configuration file is returned.
func FindRepository(dir string) (*Repository, error) {
rootPath, err := fs.FindFileInParentDirs(dir, RepositoryCfgFile)
if err != nil {
return nil, err
}
return NewRepository(rootPath)
}
// FindRepositoryCwd searches for a repository config file in the current directory
// and all it's parents. If a repository config file is found it returns a
// Repository
func FindRepositoryCwd() (*Repository, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
return FindRepository(cwd)
}
// NewRepository reads the configuration file and returns a Repository
func NewRepository(cfgPath string) (*Repository, error) {
cfg, err := cfg.RepositoryFromFile(cfgPath)
if err != nil {
return nil, errors.Wrapf(err,
"reading repository config %s failed", cfgPath)
}
err = cfg.Validate()
if err != nil {
return nil, errors.Wrapf(err,
"validating repository config %q failed", cfgPath)
}
r := Repository{
CfgPath: cfgPath,
Path: path.Dir(cfgPath),
AppSearchDirs: fs.PathsJoin(path.Dir(cfgPath), cfg.Discover.Dirs),
SearchDepth: cfg.Discover.SearchDepth,
PSQLURL: cfg.Database.PGSQLURL,
}
err = fs.DirsExist(r.AppSearchDirs)
if err != nil {
return nil, errors.Wrap(err, "application_dirs parameter is invalid")
}
return &r, nil
}
// FindApps searches for application config files in the AppSearchDirs of the
// repository and returns all found apps
func (r *Repository) FindApps() ([]*App, error) {
var result []*App
for _, searchDir := range r.AppSearchDirs {
appsCfgPaths, err := fs.FindFilesInSubDir(searchDir, AppCfgFile, r.SearchDepth)
if err != nil {
return nil, errors.Wrap(err, "finding application configs failed")
}
for _, appCfgPath := range appsCfgPaths {
a, err := NewApp(r, appCfgPath)
if err != nil {
return nil, err
}
result = append(result, a)
}
}
return result, nil
}
// AppByDir reads an application config file from the direcory and returns an
// App
func (r *Repository) AppByDir(appDir string) (*App, error) {
cfgPath := path.Join(appDir, AppCfgFile)
cfgPath, err := filepath.Abs(cfgPath)
if err != nil {
return nil, err
}
return NewApp(r, cfgPath)
}
// AppByName searches for an App with the given name in the repository and
// returns it. If none is found os.ErrNotExist is returned.
func (r *Repository) AppByName(name string) (*App, error) {
for _, searchDir := range r.AppSearchDirs {
appsCfgPaths, err := fs.FindFilesInSubDir(searchDir, AppCfgFile, r.SearchDepth)
if err != nil {
return nil, errors.Wrap(err, "finding application failed")
}
for _, appCfgPath := range appsCfgPaths {
a, err := NewApp(r, appCfgPath)
if err != nil {
return nil, err
}
if a.Name == name {
return a, nil
}
}
}
return nil, os.ErrNotExist
}
// GitCommitID returns the Git commit ID in the baur repository root
func (r *Repository) GitCommitID() (string, error) {
if len(r.gitCommitID) != 0 {
return r.gitCommitID, nil
}
commit, err := git.CommitID(r.Path)
if err != nil {
return "", errors.Wrap(err, "determining Git commit ID failed, "+
"ensure that the git command is in a directory in $PATH and "+
"that the .baur.toml file is part of a git repository")
}
r.gitCommitID = commit
return commit, nil
}
// GitWorkTreeIsDirty returns true if the git repository contains untracked
// changes
func (r *Repository) GitWorkTreeIsDirty() (bool, error) {
if r.gitWorktreeIsDirty != nil {
return *r.gitWorktreeIsDirty, nil
}
isDirty, err := git.WorkTreeIsDirty(r.Path)
if err != nil {
return false, errors.Wrap(err, "determining Git worktree state failed, "+
"ensure that the git command is in a directory in $PATH and "+
"that the .baur.toml file is part of a git repository")
}
r.gitWorktreeIsDirty = &isDirty
return isDirty, nil
}