@@ -3,7 +3,7 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "io/ioutil "
6
+ "io"
7
7
"os"
8
8
"strings"
9
9
"time"
@@ -98,6 +98,7 @@ func (b *buildCmd) run(ctx context.Context) error {
98
98
startTime := time .Now ()
99
99
ctx , span := ybtrace .Start (ctx , "Build" , trace .WithNewRoot ())
100
100
defer span .End ()
101
+ ctx = withStdoutLogs (ctx )
101
102
102
103
log .Infof (ctx , "Build started at %s" , startTime .Format (longTimeFormat ))
103
104
@@ -117,10 +118,10 @@ func (b *buildCmd) run(ctx context.Context) error {
117
118
showDockerWarningsIfNeeded (ctx , b .mode , buildTargets )
118
119
119
120
// Do the build!
120
- startSection ("BUILD" )
121
121
log .Debugf (ctx , "Building package %s in %s..." , targetPackage .Name , targetPackage .Path )
122
122
123
123
buildError := doTargetList (ctx , targetPackage , buildTargets , & doOptions {
124
+ output : os .Stdout ,
124
125
executionMode : b .mode ,
125
126
dockerClient : dockerClient ,
126
127
dataDirs : dataDirs ,
@@ -132,27 +133,27 @@ func (b *buildCmd) run(ctx context.Context) error {
132
133
})
133
134
if buildError != nil {
134
135
span .SetStatus (codes .Unknown , buildError .Error ())
136
+ log .Errorf (ctx , "%v" , buildError )
135
137
}
136
138
span .End ()
137
139
endTime := time .Now ()
138
140
buildTime := endTime .Sub (startTime )
139
141
140
- log .Infof (ctx , "" )
141
- log .Infof (ctx , "Build finished at %s, taking %s" , endTime .Format (longTimeFormat ), buildTime )
142
- log .Infof (ctx , "" )
143
-
144
- log .Infof (ctx , "%s" , buildTraces .dump ())
142
+ fmt .Printf ("\n Build finished at %s, taking %v\n \n " , endTime .Format (longTimeFormat ), buildTime .Truncate (time .Millisecond ))
143
+ fmt .Println (buildTraces .dump ())
145
144
145
+ style := termStylesFromEnv ()
146
146
if buildError != nil {
147
- subSection ( "BUILD FAILED" )
148
- return buildError
147
+ fmt . Printf ( "%sBUILD FAILED%s ❌ \n " , style . buildResult ( false ), style . reset () )
148
+ return alreadyLoggedError { buildError }
149
149
}
150
150
151
- subSection ( "BUILD SUCCEEDED" )
151
+ fmt . Printf ( "%sBUILD PASSED%s ️✔️ \n " , style . buildResult ( true ), style . reset () )
152
152
return nil
153
153
}
154
154
155
155
type doOptions struct {
156
+ output io.Writer
156
157
dataDirs * ybdata.Dirs
157
158
downloader * ybdata.Downloader
158
159
executionMode executionMode
@@ -198,6 +199,11 @@ func doTargetList(ctx context.Context, pkg *yb.Package, targets []*yb.Target, op
198
199
}
199
200
200
201
func doTarget (ctx context.Context , pkg * yb.Package , target * yb.Target , opts * doOptions ) error {
202
+ style := termStylesFromEnv ()
203
+ fmt .Printf ("\n 🎯 %sTarget: %s%s\n " , style .target (), target .Name , style .reset ())
204
+
205
+ ctx = withLogPrefix (ctx , target .Name )
206
+
201
207
bio , err := newBiome (ctx , target , newBiomeOptions {
202
208
packageDir : pkg .Path ,
203
209
dataDirs : opts .dataDirs ,
@@ -216,15 +222,17 @@ func doTarget(ctx context.Context, pkg *yb.Package, target *yb.Target, opts *doO
216
222
log .Warnf (ctx , "Clean up environment: %v" , err )
217
223
}
218
224
}()
225
+ output := newLinePrefixWriter (opts .output , target .Name )
219
226
sys := build.Sys {
220
227
Biome : bio ,
221
228
Downloader : opts .downloader ,
222
229
DockerClient : opts .dockerClient ,
223
230
DockerNetworkID : opts .dockerNetworkID ,
224
- Stdout : os .Stdout ,
225
- Stderr : os .Stderr ,
231
+
232
+ Stdout : output ,
233
+ Stderr : output ,
226
234
}
227
- execBiome , err := build .Setup (ctx , sys , target )
235
+ execBiome , err := build .Setup (withLogPrefix ( ctx , setupLogPrefix ) , sys , target )
228
236
if err != nil {
229
237
return err
230
238
}
@@ -241,34 +249,15 @@ func doTarget(ctx context.Context, pkg *yb.Package, target *yb.Target, opts *doO
241
249
return nil
242
250
}
243
251
244
- subSection (fmt .Sprintf ("Build target: %s" , target .Name ))
245
- log .Infof (ctx , "Executing build steps..." )
246
- return build .Execute (ctx , sys , target )
252
+ return build .Execute (withStdoutLogs (ctx ), sys , announceCommand , target )
247
253
}
248
254
249
- // Because, why not?
250
- // Based on https://github.com/sindresorhus/is-docker/blob/master/index.js and https://github.com/moby/moby/issues/18355
251
- // Discussion is not settled yet: https://stackoverflow.com/questions/23513045/how-to-check-if-a-process-is-running-inside-docker-container#25518538
252
- func insideTheMatrix () bool {
253
- hasDockerEnv := pathExists ("/.dockerenv" )
254
- hasDockerCGroup := false
255
- dockerCGroupPath := "/proc/self/cgroup"
256
- if pathExists (dockerCGroupPath ) {
257
- contents , _ := ioutil .ReadFile (dockerCGroupPath )
258
- hasDockerCGroup = strings .Count (string (contents ), "docker" ) > 0
259
- }
260
- return hasDockerEnv || hasDockerCGroup
255
+ func announceCommand (cmdString string ) {
256
+ style := termStylesFromEnv ()
257
+ fmt .Printf ("%s> %s%s\n " , style .command (), cmdString , style .reset ())
261
258
}
262
259
263
260
func pathExists (path string ) bool {
264
261
_ , err := os .Lstat (path )
265
262
return ! os .IsNotExist (err )
266
263
}
267
-
268
- func startSection (name string ) {
269
- fmt .Printf (" === %s ===\n " , name )
270
- }
271
-
272
- func subSection (name string ) {
273
- fmt .Printf (" -- %s -- \n " , name )
274
- }
0 commit comments