forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
63 lines (49 loc) · 1.31 KB
/
app.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
package baur
import (
"fmt"
"path/filepath"
"sort"
"github.com/simplesurance/baur/v1/cfg"
)
// App represents an application
type App struct {
RelPath string
Path string
Name string
repositoryRootPath string
cfg *cfg.App
}
// NewApp reads the configuration file and returns a new App
func NewApp(appCfg *cfg.App, repositoryRootPath string) (*App, error) {
appDir := filepath.Dir(appCfg.FilePath())
appRelPath, err := filepath.Rel(repositoryRootPath, appDir)
if err != nil {
return nil, fmt.Errorf("%s: resolving repository relative application path failed: %w", appCfg.Name, err)
}
app := App{
cfg: appCfg,
Path: appDir,
RelPath: appRelPath,
Name: appCfg.Name,
repositoryRootPath: repositoryRootPath,
}
return &app, nil
}
// String returns the string representation of an app
func (a *App) String() string {
return a.Name
}
func (a *App) Tasks() []*Task {
result := make([]*Task, 0, len(a.cfg.Tasks))
for _, taskCfg := range a.cfg.Tasks {
task := NewTask(taskCfg, a.Name, a.repositoryRootPath, a.Path)
result = append(result, task)
}
return result
}
// SortAppsByName sorts the apps in the slice by Name
func SortAppsByName(apps []*App) {
sort.Slice(apps, func(i int, j int) bool {
return apps[i].Name < apps[j].Name
})
}