/
run.go
547 lines (456 loc) · 20.5 KB
/
run.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package cmd
import (
"context"
"fmt"
"io"
"log"
"os"
path2 "path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/bruin-data/bruin/pkg/ansisql"
"github.com/bruin-data/bruin/pkg/bigquery"
"github.com/bruin-data/bruin/pkg/config"
"github.com/bruin-data/bruin/pkg/connection"
"github.com/bruin-data/bruin/pkg/date"
"github.com/bruin-data/bruin/pkg/executor"
"github.com/bruin-data/bruin/pkg/git"
"github.com/bruin-data/bruin/pkg/ingestr"
"github.com/bruin-data/bruin/pkg/jinja"
"github.com/bruin-data/bruin/pkg/lint"
"github.com/bruin-data/bruin/pkg/mssql"
"github.com/bruin-data/bruin/pkg/path"
"github.com/bruin-data/bruin/pkg/pipeline"
"github.com/bruin-data/bruin/pkg/postgres"
"github.com/bruin-data/bruin/pkg/python"
"github.com/bruin-data/bruin/pkg/query"
"github.com/bruin-data/bruin/pkg/scheduler"
"github.com/bruin-data/bruin/pkg/snowflake"
"github.com/bruin-data/bruin/pkg/synapse"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
)
const LogsFolder = "logs"
var (
yesterday = time.Now().AddDate(0, 0, -1)
defaultStartDate = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.UTC)
defaultEndDate = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, time.UTC)
startDateFlag = &cli.StringFlag{
Name: "start-date",
Usage: "the start date of the range the pipeline will run for in YYYY-MM-DD, YYYY-MM-DD HH:MM:SS or YYYY-MM-DD HH:MM:SS.ffffff format",
DefaultText: "beginning of yesterday, e.g. " + defaultStartDate.Format("2006-01-02 15:04:05.000000"),
Value: defaultStartDate.Format("2006-01-02 15:04:05.000000"),
}
endDateFlag = &cli.StringFlag{
Name: "end-date",
Usage: "the end date of the range the pipeline will run for in YYYY-MM-DD, YYYY-MM-DD HH:MM:SS or YYYY-MM-DD HH:MM:SS.ffffff format",
DefaultText: "end of yesterday, e.g. " + defaultEndDate.Format("2006-01-02 15:04:05") + ".999999",
Value: defaultEndDate.Format("2006-01-02 15:04:05") + ".999999",
}
)
func Run(isDebug *bool) *cli.Command {
return &cli.Command{
Name: "run",
Usage: "run a Bruin pipeline",
ArgsUsage: "[path to the task file]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "downstream",
Usage: "pass this flag if you'd like to run all the downstream tasks as well",
},
&cli.IntFlag{
Name: "workers",
Usage: "number of workers to run the tasks in parallel",
Value: 16,
},
startDateFlag,
endDateFlag,
&cli.StringFlag{
Name: "environment",
Aliases: []string{"e", "env"},
Usage: "the environment to use",
},
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "force the validation even if the environment is a production environment",
},
&cli.BoolFlag{
Name: "no-log-file",
Usage: "do not create a log file for this run",
},
&cli.BoolFlag{
Name: "full-refresh",
Aliases: []string{"r"},
Usage: "truncate the table before running",
},
},
Action: func(c *cli.Context) error {
defer func() {
if err := recover(); err != nil {
log.Println("=======================================")
log.Println("Bruin encountered an unexpected error, please report the issue to the Bruin team.")
log.Println(err)
log.Println("=======================================")
}
}()
logger := makeLogger(*isDebug)
inputPath := c.Args().Get(0)
if inputPath == "" {
errorPrinter.Printf("Please give a task or pipeline path: bruin run <path to the task definition>)\n")
return cli.Exit("", 1)
}
startDate, err := date.ParseTime(c.String("start-date"))
logger.Debug("given start date: ", startDate)
if err != nil {
errorPrinter.Printf("Please give a valid start date: bruin run --start-date <start date>)\n")
errorPrinter.Printf("A valid start date can be in the YYYY-MM-DD or YYYY-MM-DD HH:MM:SS formats. \n")
errorPrinter.Printf(" e.g. %s \n", time.Now().AddDate(0, 0, -1).Format("2006-01-02"))
errorPrinter.Printf(" e.g. %s \n", time.Now().AddDate(0, 0, -1).Format("2006-01-02 15:04:05"))
return cli.Exit("", 1)
}
endDate, err := date.ParseTime(c.String("end-date"))
logger.Debug("given end date: ", endDate)
if err != nil {
errorPrinter.Printf("Please give a valid end date: bruin run --start-date <start date>)\n")
errorPrinter.Printf("A valid start date can be in the YYYY-MM-DD or YYYY-MM-DD HH:MM:SS formats. \n")
errorPrinter.Printf(" e.g. %s \n", time.Now().AddDate(0, 0, -1).Format("2006-01-02"))
errorPrinter.Printf(" e.g. %s \n", time.Now().AddDate(0, 0, -1).Format("2006-01-02 15:04:05"))
return cli.Exit("", 1)
}
pipelinePath := inputPath
repoRoot, err := git.FindRepoFromPath(inputPath)
if err != nil {
errorPrinter.Printf("Failed to find the git repository root: %v\n", err)
return cli.Exit("", 1)
}
runID := time.Now().Format("2006_01_02_15_04_05")
if !c.Bool("no-log-file") {
logPath, err := filepath.Abs(fmt.Sprintf("%s/%s/%s.log", repoRoot.Path, LogsFolder, runID))
if err != nil {
errorPrinter.Printf("Failed to create log file: %v\n", err)
return cli.Exit("", 1)
}
fn, err2 := logOutput(logPath)
if err2 != nil {
errorPrinter.Printf("Failed to create log file: %v\n", err2)
return cli.Exit("", 1)
}
defer fn()
color.Output = os.Stdout
err = git.EnsureGivenPatternIsInGitignore(afero.NewOsFs(), repoRoot.Path, LogsFolder+"/*.log")
if err != nil {
errorPrinter.Printf("Failed to add the log file to .gitignore: %v\n", err)
return cli.Exit("", 1)
}
}
runningForAnAsset := isPathReferencingAsset(inputPath)
var task *pipeline.Asset
runDownstreamTasks := false
if runningForAnAsset {
task, err = DefaultPipelineBuilder.CreateAssetFromFile(inputPath)
if err != nil {
errorPrinter.Printf("Failed to build task: %v\n", err.Error())
return cli.Exit("", 1)
}
if task == nil {
errorPrinter.Printf("The given file path doesn't seem to be a Bruin task definition: '%s'\n", inputPath)
return cli.Exit("", 1)
}
pipelinePath, err = path.GetPipelineRootFromTask(inputPath, pipelineDefinitionFile)
if err != nil {
errorPrinter.Printf("Failed to find the pipeline this task belongs to: '%s'\n", inputPath)
return cli.Exit("", 1)
}
if c.Bool("downstream") {
infoPrinter.Println("The downstream tasks will be executed as well.")
runDownstreamTasks = true
}
}
if !runningForAnAsset && c.Bool("downstream") {
infoPrinter.Println("Ignoring the '--downstream' flag since you are running the whole pipeline")
}
configFilePath := path2.Join(repoRoot.Path, ".bruin.yml")
cm, err := config.LoadOrCreate(afero.NewOsFs(), configFilePath)
if err != nil {
errorPrinter.Printf("Failed to load the config file at '%s': %v\n", configFilePath, err)
return cli.Exit("", 1)
}
err = switchEnvironment(c, cm, os.Stdin)
if err != nil {
return err
}
connectionManager, err := connection.NewManagerFromConfig(cm)
if err != nil {
errorPrinter.Printf("Failed to register connections: %v\n", err)
return cli.Exit("", 1)
}
foundPipeline, err := DefaultPipelineBuilder.CreatePipelineFromPath(pipelinePath)
if err != nil {
errorPrinter.Println("failed to build pipeline, are you sure you have referred the right path?")
errorPrinter.Println("\nHint: You need to run this command with a path to either the pipeline directory or the asset file itself directly.")
return cli.Exit("", 1)
}
infoPrinter.Printf("Analyzed the pipeline '%s' with %d assets.\n", foundPipeline.Name, len(foundPipeline.Assets))
if !runningForAnAsset {
rules, err := lint.GetRules(fs)
if err != nil {
errorPrinter.Printf("An error occurred while linting the pipelines: %v\n", err)
return cli.Exit("", 1)
}
linter := lint.NewLinter(path.GetPipelinePaths, DefaultPipelineBuilder, rules, logger)
res, err := linter.LintPipelines([]*pipeline.Pipeline{foundPipeline})
err = reportLintErrors(res, err, lint.Printer{RootCheckPath: pipelinePath}, "")
if err != nil {
return cli.Exit("", 1)
}
} else {
infoPrinter.Printf("Running only the asset '%s'\n", task.Name)
infoPrinter.Printf(" - custom checks: %d\n", len(task.CustomChecks))
}
s := scheduler.NewScheduler(logger, foundPipeline)
infoPrinter.Printf("\nStarting the pipeline execution...\n\n")
if task != nil {
logger.Debug("marking single task to run: ", task.Name)
s.MarkAll(scheduler.Succeeded)
s.MarkTask(task, scheduler.Pending, runDownstreamTasks)
}
mainExecutors, err := setupExecutors(s, cm, connectionManager, startDate, endDate, runID, c.Bool("full-refresh"))
if err != nil {
errorPrinter.Printf(err.Error())
return cli.Exit("", 1)
}
ex, err := executor.NewConcurrent(logger, mainExecutors, c.Int("workers"))
if err != nil {
errorPrinter.Printf("Failed to create executor: %v\n", err)
return cli.Exit("", 1)
}
ex.Start(s.WorkQueue, s.Results)
start := time.Now()
results := s.Run(context.Background())
duration := time.Since(start)
successPrinter.Printf("\n\nExecuted %d tasks in %s\n", len(results), duration.Truncate(time.Millisecond).String())
errorsInTaskResults := make([]*scheduler.TaskExecutionResult, 0)
for _, res := range results {
if res.Error != nil {
errorsInTaskResults = append(errorsInTaskResults, res)
}
}
if len(errorsInTaskResults) > 0 {
printErrorsInResults(errorsInTaskResults, s)
}
return nil
},
}
}
func printErrorsInResults(errorsInTaskResults []*scheduler.TaskExecutionResult, s *scheduler.Scheduler) {
errorPrinter.Printf("\nFailed tasks: %d\n", len(errorsInTaskResults))
for _, t := range errorsInTaskResults {
errorPrinter.Printf(" - %s\n", t.Instance.GetAsset().Name)
errorPrinter.Printf(" └── %s\n\n", t.Error.Error())
}
upstreamFailedTasks := s.GetTaskInstancesByStatus(scheduler.UpstreamFailed)
if len(upstreamFailedTasks) > 0 {
errorPrinter.Printf("The following tasks are skipped due to their upstream failing:\n")
skippedAssets := make(map[string]int, 0)
for _, t := range upstreamFailedTasks {
if _, ok := skippedAssets[t.GetAsset().Name]; !ok {
skippedAssets[t.GetAsset().Name] = 0
}
if t.GetType() == scheduler.TaskInstanceTypeMain {
continue
}
skippedAssets[t.GetAsset().Name] += 1
}
for asset, checkCount := range skippedAssets {
if checkCount == 0 {
errorPrinter.Printf(" - %s\n", asset)
} else {
errorPrinter.Printf(" - %s %s\n", asset, faint(fmt.Sprintf("(and %d checks)", checkCount)))
}
}
}
}
func setupExecutors(s *scheduler.Scheduler, config *config.Config, conn *connection.Manager, startDate, endDate time.Time, runID string, fullRefresh bool) (map[pipeline.AssetType]executor.Config, error) {
mainExecutors := executor.DefaultExecutorsV2
// this is a heuristic we apply to find what might be the most common type of custom check in the pipeline
// this should go away once we incorporate URIs into the assets
estimateCustomCheckType := s.FindMajorityOfTypes([]pipeline.AssetType{
pipeline.AssetTypeBigqueryQuery,
pipeline.AssetTypeSnowflakeQuery,
pipeline.AssetTypePostgresQuery,
pipeline.AssetTypeMsSQLQuery,
pipeline.AssetTypeRedshiftQuery,
pipeline.AssetTypeSynapseQuery,
}, pipeline.AssetTypeBigqueryQuery)
if s.WillRunTaskOfType(pipeline.AssetTypePython) {
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeMain] = python.NewLocalOperator(config, jinja.PythonEnvVariables(&startDate, &endDate, runID))
}
renderer := jinja.NewRendererWithStartEndDates(&startDate, &endDate)
wholeFileExtractor := &query.WholeFileExtractor{
Fs: fs,
Renderer: renderer,
}
if s.WillRunTaskOfType(pipeline.AssetTypeBigqueryQuery) || estimateCustomCheckType == pipeline.AssetTypeBigqueryQuery {
bqOperator := bigquery.NewBasicOperator(conn, wholeFileExtractor, bigquery.NewMaterializer(fullRefresh))
bqCheckRunner, err := bigquery.NewColumnCheckOperator(conn)
if err != nil {
return nil, err
}
bqCustomCheckRunner, err := bigquery.NewCustomCheckOperator(conn)
if err != nil {
return nil, err
}
mainExecutors[pipeline.AssetTypeBigqueryQuery][scheduler.TaskInstanceTypeMain] = bqOperator
mainExecutors[pipeline.AssetTypeBigqueryQuery][scheduler.TaskInstanceTypeColumnCheck] = bqCheckRunner
mainExecutors[pipeline.AssetTypeBigqueryQuery][scheduler.TaskInstanceTypeCustomCheck] = bqCustomCheckRunner
// we set the Python runners to run the checks on BigQuery assuming that there won't be many usecases where a user has both BQ and Snowflake
if estimateCustomCheckType == pipeline.AssetTypeBigqueryQuery {
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeColumnCheck] = bqCheckRunner
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeCustomCheck] = bqCustomCheckRunner
}
}
if s.WillRunTaskOfType(pipeline.AssetTypePostgresQuery) || estimateCustomCheckType == pipeline.AssetTypePostgresQuery ||
s.WillRunTaskOfType(pipeline.AssetTypeRedshiftQuery) || estimateCustomCheckType == pipeline.AssetTypeRedshiftQuery {
pgCustomCheckRunner := ansisql.NewCustomCheckOperator(conn)
pgCheckRunner := postgres.NewColumnCheckOperator(conn)
pgOperator := postgres.NewBasicOperator(conn, wholeFileExtractor, postgres.NewMaterializer(fullRefresh))
mainExecutors[pipeline.AssetTypeRedshiftQuery][scheduler.TaskInstanceTypeMain] = pgOperator
mainExecutors[pipeline.AssetTypeRedshiftQuery][scheduler.TaskInstanceTypeColumnCheck] = pgCheckRunner
mainExecutors[pipeline.AssetTypeRedshiftQuery][scheduler.TaskInstanceTypeCustomCheck] = pgCustomCheckRunner
mainExecutors[pipeline.AssetTypePostgresQuery][scheduler.TaskInstanceTypeMain] = pgOperator
mainExecutors[pipeline.AssetTypePostgresQuery][scheduler.TaskInstanceTypeColumnCheck] = pgCheckRunner
mainExecutors[pipeline.AssetTypePostgresQuery][scheduler.TaskInstanceTypeCustomCheck] = pgCustomCheckRunner
// we set the Python runners to run the checks on Snowflake assuming that there won't be many usecases where a user has both BQ and Snowflake
if estimateCustomCheckType == pipeline.AssetTypePostgresQuery || estimateCustomCheckType == pipeline.AssetTypeRedshiftQuery {
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeColumnCheck] = pgCheckRunner
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeCustomCheck] = pgCustomCheckRunner
}
}
shouldInitiateSnowflake := s.WillRunTaskOfType(pipeline.AssetTypeSnowflakeQuery) || s.WillRunTaskOfType(pipeline.AssetTypeSnowflakeQuerySensor) || estimateCustomCheckType == pipeline.AssetTypeSnowflakeQuery
if shouldInitiateSnowflake {
sfOperator := snowflake.NewBasicOperator(conn, wholeFileExtractor, snowflake.NewMaterializer(fullRefresh))
sfCheckRunner := snowflake.NewColumnCheckOperator(conn)
sfCustomCheckRunner := ansisql.NewCustomCheckOperator(conn)
sfQuerySensor := snowflake.NewQuerySensor(conn, renderer, 30)
mainExecutors[pipeline.AssetTypeSnowflakeQuery][scheduler.TaskInstanceTypeMain] = sfOperator
mainExecutors[pipeline.AssetTypeSnowflakeQuery][scheduler.TaskInstanceTypeColumnCheck] = sfCheckRunner
mainExecutors[pipeline.AssetTypeSnowflakeQuery][scheduler.TaskInstanceTypeCustomCheck] = sfCustomCheckRunner
mainExecutors[pipeline.AssetTypeSnowflakeQuerySensor][scheduler.TaskInstanceTypeMain] = sfQuerySensor
mainExecutors[pipeline.AssetTypeSnowflakeQuerySensor][scheduler.TaskInstanceTypeColumnCheck] = sfCheckRunner
mainExecutors[pipeline.AssetTypeSnowflakeQuerySensor][scheduler.TaskInstanceTypeCustomCheck] = sfCustomCheckRunner
// we set the Python runners to run the checks on Snowflake assuming that there won't be many usecases where a user has both BQ and Snowflake
if estimateCustomCheckType == pipeline.AssetTypeSnowflakeQuery {
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeColumnCheck] = sfCheckRunner
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeCustomCheck] = sfCustomCheckRunner
}
}
if s.WillRunTaskOfType(pipeline.AssetTypeMsSQLQuery) || estimateCustomCheckType == pipeline.AssetTypeMsSQLQuery ||
s.WillRunTaskOfType(pipeline.AssetTypeSynapseQuery) || estimateCustomCheckType == pipeline.AssetTypeSynapseQuery {
msOperator := mssql.NewBasicOperator(conn, wholeFileExtractor, mssql.NewMaterializer(fullRefresh))
synapseOperator := synapse.NewBasicOperator(conn, wholeFileExtractor, synapse.NewMaterializer(fullRefresh))
msCheckRunner := mssql.NewColumnCheckOperator(conn)
msCustomCheckRunner := ansisql.NewCustomCheckOperator(conn)
mainExecutors[pipeline.AssetTypeMsSQLQuery][scheduler.TaskInstanceTypeMain] = msOperator
mainExecutors[pipeline.AssetTypeMsSQLQuery][scheduler.TaskInstanceTypeColumnCheck] = msCheckRunner
mainExecutors[pipeline.AssetTypeMsSQLQuery][scheduler.TaskInstanceTypeCustomCheck] = msCustomCheckRunner
mainExecutors[pipeline.AssetTypeSynapseQuery][scheduler.TaskInstanceTypeMain] = synapseOperator
mainExecutors[pipeline.AssetTypeSynapseQuery][scheduler.TaskInstanceTypeColumnCheck] = msCheckRunner
mainExecutors[pipeline.AssetTypeSynapseQuery][scheduler.TaskInstanceTypeCustomCheck] = msCustomCheckRunner
// we set the Python runners to run the checks on MsSQL
if estimateCustomCheckType == pipeline.AssetTypeMsSQLQuery || estimateCustomCheckType == pipeline.AssetTypeSynapseQuery {
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeColumnCheck] = msCheckRunner
mainExecutors[pipeline.AssetTypePython][scheduler.TaskInstanceTypeCustomCheck] = msCustomCheckRunner
}
}
if s.WillRunTaskOfType(pipeline.AssetTypeIngestr) || estimateCustomCheckType == pipeline.AssetTypeIngestr {
ingestrOperator, err := ingestr.NewBasicOperator(conn)
if err != nil {
return nil, err
}
ingestrCheckRunner := ingestr.NewColumnCheckOperator(&mainExecutors)
ingestrCustomCheckRunner := ingestr.NewCustomCheckOperator(&mainExecutors)
mainExecutors[pipeline.AssetTypeIngestr][scheduler.TaskInstanceTypeMain] = ingestrOperator
mainExecutors[pipeline.AssetTypeIngestr][scheduler.TaskInstanceTypeColumnCheck] = ingestrCheckRunner
mainExecutors[pipeline.AssetTypeIngestr][scheduler.TaskInstanceTypeCustomCheck] = ingestrCustomCheckRunner
}
return mainExecutors, nil
}
func isPathReferencingAsset(p string) bool {
if strings.HasSuffix(p, pipelineDefinitionFile) {
return false
}
if isDir(p) {
return false
}
return true
}
func isDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
type clearFileWriter struct {
file *os.File
m sync.Mutex
}
func (c *clearFileWriter) Write(p []byte) (int, error) {
c.m.Lock()
defer c.m.Unlock()
_, err := c.file.Write([]byte(Clean(string(p))))
return len(p), err
}
func logOutput(logPath string) (func(), error) {
err := os.MkdirAll(filepath.Dir(logPath), 0o755)
if err != nil {
return nil, errors.Wrap(err, "failed to create log directory")
}
// open file read/write | create if not exist | clear file at open if exists
f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return nil, errors.Wrap(err, "failed to open log file")
}
// save existing stdout | MultiWriter writes to saved stdout and file
mw := io.MultiWriter(os.Stdout, &clearFileWriter{f, sync.Mutex{}})
// get pipe reader and writer | writes to pipe writer come out pipe reader
r, w, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create log pipe")
}
// replace stdout,stderr with pipe writer | all writes to stdout, stderr will go through pipe instead (fmt.print, log)
os.Stdout = w
os.Stderr = w
// writes with log.Print should also write to mw
log.SetOutput(mw)
// create channel to control exit | will block until all copies are finished
exit := make(chan bool)
go func() {
// copy all reads from pipe to multiwriter, which writes to stdout and file
_, err := io.Copy(mw, r)
if err != nil {
panic(err)
}
// when r or w is closed copy will finish and true will be sent to channel
exit <- true
}()
// function to be deferred in main until program exits
return func() {
// close writer then block on exit channel | this will let mw finish writing before the program exits
_ = w.Close()
<-exit
// close file after all writes have finished
_ = f.Close()
}, nil
}
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var re = regexp.MustCompile(ansi)
func Clean(str string) string {
return re.ReplaceAllString(str, "")
}