forked from simplesurance/baur
/
helpers.go
124 lines (97 loc) · 2.3 KB
/
helpers.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
package command
import (
"fmt"
"os"
"path"
"time"
"github.com/simplesurance/baur"
"github.com/simplesurance/baur/log"
"github.com/simplesurance/baur/storage"
"github.com/simplesurance/baur/storage/postgres"
)
func mustFindRepository() *baur.Repository {
log.Debugln("searching for repository root...")
rep, err := baur.FindRepository()
if err != nil {
if os.IsNotExist(err) {
log.Fatalf("could not find repository root config file "+
"ensure the file '%s' exist in the root\n",
baur.RepositoryCfgFile)
}
log.Fatalln(err)
}
log.Debugf("repository root found: %v\n", rep.Path)
return rep
}
func isAppDir(arg string) bool {
cfgPath := path.Join(arg, baur.AppCfgFile)
_, err := os.Stat(cfgPath)
if err == nil {
return true
}
return false
}
func mustArgToApp(repo *baur.Repository, arg string) *baur.App {
if isAppDir(arg) {
app, err := repo.AppByDir(arg)
if err != nil {
log.Fatalf("could not find application in dir '%s': %s\n", arg, err)
}
return app
}
app, err := repo.AppByName(arg)
if err != nil {
if os.IsNotExist(err) {
log.Fatalf("could not find application with name '%s'\n", arg)
}
log.Fatalln(err)
}
return app
}
func mustFindApps(r *baur.Repository) []*baur.App {
apps, err := r.FindApps()
if err != nil {
log.Fatalln(err)
}
if len(apps) == 0 {
log.Fatalf("could not find any applications\n"+
"- ensure the [Discover] section is correct in %s\n"+
"- ensure that you have >1 application dirs "+
"containing a %s file\n",
r.CfgPath, baur.AppCfgFile)
}
return apps
}
func mustGetPostgresClt(r *baur.Repository) *postgres.Client {
clt, err := postgres.New(r.PSQLURL)
if err != nil {
log.Fatalf("could not establish connection to postgreSQL db: %s\n", err)
}
return clt
}
func durationToStrSec(d time.Duration) string {
return fmt.Sprintf("%.2fs", d.Seconds())
}
func mustGetCommitID(r *baur.Repository) string {
commitID, err := r.GitCommitID()
if err != nil {
log.Fatalln(err)
}
return commitID
}
func mustGetGitWorktreeIsDirty(r *baur.Repository) bool {
isDirty, err := r.GitWorkTreeIsDirty()
if err != nil {
log.Fatalln(err)
}
return isDirty
}
func vcsStr(v *storage.VCSState) string {
if len(v.CommitID) == 0 {
return ""
}
if v.IsDirty {
return fmt.Sprintf("%s-dirty", v.CommitID)
}
return v.CommitID
}