-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers.go
223 lines (179 loc) · 5.06 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package command
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/fatih/color"
"github.com/simplesurance/baur"
"github.com/simplesurance/baur/format"
"github.com/simplesurance/baur/log"
"github.com/simplesurance/baur/storage"
"github.com/simplesurance/baur/storage/postgres"
)
// envVarPSQLURL contains the name of an environment variable in that the
// postgresql URI can be stored
const envVarPSQLURL = "BAUR_POSTGRESQL_URL"
var (
greenHighlight = color.New(color.FgGreen).SprintFunc()
redHighlight = color.New(color.FgRed).SprintFunc()
yellowHighlight = color.New(color.FgYellow).SprintFunc()
underline = color.New(color.Underline).SprintFunc()
// highlight is a function that highlights parts of strings in the cli output
highlight = greenHighlight
)
func findRepository() (*baur.Repository, error) {
log.Debugln("searching for repository root...")
repo, err := baur.FindRepositoryCwd()
if err != nil {
return nil, err
}
log.Debugf("repository root found: %s", repo.Path)
return repo, nil
}
// MustFindRepository must find repo
func MustFindRepository() *baur.Repository {
repo, err := 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",
baur.RepositoryCfgFile)
}
log.Fatalln(err)
}
return repo
}
func isAppDir(arg string) bool {
cfgPath := filepath.Join(arg, baur.AppCfgFile)
_, err := os.Stat(cfgPath)
return err == nil
}
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", 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'", arg)
}
log.Fatalln(err)
}
return app
}
// getPostgresCltWithEnv returns a new postresql storage client,
// if the environment variable BAUR_PSQL_URI is set, this uri is used instead of
// the configuration specified in the baur.Repository object
func getPostgresCltWithEnv(psqlURI string) (*postgres.Client, error) {
uri := psqlURI
if envURI := os.Getenv(envVarPSQLURL); len(envURI) != 0 {
log.Debugf("using postgresql connection URL from $%s environment variable",
envVarPSQLURL)
uri = envURI
} else {
log.Debugf("environment variable $%s not set", envVarPSQLURL)
}
return postgres.New(uri)
}
//mustHavePSQLURI calls log.Fatalf if neither envVarPSQLURL nor the postgres_url
//in the repository config is set
func mustHavePSQLURI(r *baur.Repository) {
if len(r.PSQLURL) != 0 {
return
}
if len(os.Getenv(envVarPSQLURL)) == 0 {
log.Fatalf("PostgreSQL connection information is missing.\n"+
"- set postgres_url in your repository config or\n"+
"- set the $%s environment variable", envVarPSQLURL)
}
}
// MustGetPostgresClt must return the PG client
func MustGetPostgresClt(r *baur.Repository) *postgres.Client {
mustHavePSQLURI(r)
clt, err := getPostgresCltWithEnv(r.PSQLURL)
if err != nil {
log.Fatalf("could not establish connection to postgreSQL db: %s", err)
}
return clt
}
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
}
func mustArgToApps(repo *baur.Repository, args []string) []*baur.App {
if len(args) == 0 {
apps, err := repo.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",
repo.CfgPath, baur.AppCfgFile)
}
return apps
}
dedupMap := make(map[string]struct{}, len(args))
apps := make([]*baur.App, 0, len(args))
for _, arg := range args {
app := mustArgToApp(repo, arg)
if _, exist := dedupMap[app.Path]; exist {
continue
}
dedupMap[app.Path] = struct{}{}
apps = append(apps, mustArgToApp(repo, arg))
}
return apps
}
func mustWriteRow(fmt format.Formatter, row []interface{}) {
err := fmt.WriteRow(row)
if err != nil {
log.Fatalln(err)
}
}
func coloredBuildStatus(status baur.BuildStatus) string {
switch status {
case baur.BuildStatusInputsUndefined:
return yellowHighlight(status.String())
case baur.BuildStatusBuildCommandUndefined:
return yellowHighlight(status.String())
case baur.BuildStatusExist:
return greenHighlight(status.String())
case baur.BuildStatusPending:
return redHighlight(status.String())
default:
return status.String()
}
}
func bytesToMib(bytes int) string {
return fmt.Sprintf("%.3f", float64(bytes)/1024/1024)
}
func durationToStrSeconds(duration time.Duration) string {
return fmt.Sprintf("%.3f", duration.Seconds())
}