Skip to content

Commit 7d3d281

Browse files
committed
feature(project): Better log handling for project scripts 📝
1 parent 29c380d commit 7d3d281

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/project.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838

3939
"github.com/libgit2/git2go"
4040
"github.com/getsentry/raven-go"
41+
"strconv"
42+
"path/filepath"
4143
)
4244

4345
type scriptVariables struct {
@@ -286,7 +288,7 @@ func processBranch(config *Configuration, proj ProjectConfig, twd string, branch
286288

287289
Log.Debugf(" [%s] - processing artifacts from pick-up location...\n", proj.Path)
288290
runPreProcessArtifacts(&artifacts, &proj.Path, &branchName)
289-
processArtifacts(config.Home, artifacts, proj.Path, branchName)
291+
processArtifacts(config.Home, twd, artifacts, proj.Path, branchName)
290292
runPostProcessArtifacts(&artifacts, &proj.Path, &branchName)
291293

292294
runPostProcessBranch(&twd, &branchName, &description)
@@ -299,7 +301,7 @@ func runProjectScripts(dir string, branchName string, proj ProjectConfig) {
299301
for _, script := range proj.Scripts {
300302

301303
// Setup the variables that can be substituted in the script for this run
302-
Log.Debugf(" [%s] - preparing project script: \"%s\"...\n", proj.Path, script)
304+
Log.Debugf(" [%s] - preparing project script %d: \"%s\"...\n", proj.Path, scriptIndex, script)
303305

304306
scriptSubs := scriptVariables{proj.Path, branchName, proj.URL, proj.Artifacts}
305307

@@ -311,10 +313,10 @@ func runProjectScripts(dir string, branchName string, proj ProjectConfig) {
311313
}
312314
scriptFinalStr := scriptFinal.String()
313315

314-
Log.Debugf(" [%s] - executing project script: \"%s\"...\n", proj.Path, scriptFinalStr)
316+
Log.Debugf(" [%s] - executing project script %d: \"%s\"...\n", proj.Path, scriptIndex, scriptFinalStr)
315317

316318
stdout, stderr, err := execInDir(dir, scriptFinalStr)
317-
writeProjectLogs(stdout, stderr, scriptIndex, dir + "/" + proj.Artifacts)
319+
writeProjectLogs(stdout, stderr, scriptIndex, dir)
318320
if err != nil {
319321
Log.Debugf(" [%s] - error executing project script %d: \"%s\"...\n", proj.Path, scriptIndex, scriptFinalStr)
320322
Log.Debugf("%s\n", string(stdout))
@@ -351,7 +353,7 @@ func execInDir(dir string, command string) (string, string, error) {
351353

352354
func writeProjectLogs( stdout string, stderr string, index int, dir string ) {
353355
// Open the output log for writing
354-
soLogFile, soErr := os.Create( dir + "/go-build-stdout_" + string(index) + ".log" )
356+
soLogFile, soErr := os.Create( dir + "/go-build-stdout_" + strconv.Itoa(index) + ".log" )
355357
if soErr != nil {
356358
// Fatal error
357359
raven.CaptureErrorAndWait(soErr, nil)
@@ -369,7 +371,7 @@ func writeProjectLogs( stdout string, stderr string, index int, dir string ) {
369371
soLogFile.Close()
370372

371373
// Open the error log for writing
372-
seLogFile, seErr := os.Create( dir + "/go-build-stderr_" + string(index) + ".log" )
374+
seLogFile, seErr := os.Create( dir + "/go-build-stderr_" + strconv.Itoa(index) + ".log" )
373375
if seErr != nil {
374376
// Fatal error
375377
raven.CaptureErrorAndWait(seErr, nil)
@@ -387,7 +389,7 @@ func writeProjectLogs( stdout string, stderr string, index int, dir string ) {
387389
seLogFile.Close()
388390
}
389391

390-
func processArtifacts(home string, artifacts string, project string, branchName string) {
392+
func processArtifacts(home string, projectDir string, artifacts string, project string, branchName string) {
391393
Log.Infof(" [%s] - processing build artifacts for project \"%s\", branch \"%s\".\n", project, project, branchName)
392394

393395
destination := home + "/artifacts/" + project + "/" + branchName
@@ -420,5 +422,27 @@ func processArtifacts(home string, artifacts string, project string, branchName
420422
panic(mvErr)
421423
}
422424

425+
logGlob := projectDir + "/*.log"
426+
Log.Debugf(" [%s] - searching for build logs using glob: \"%s\"\n", project, logGlob)
427+
logFiles, lfErr := filepath.Glob(logGlob)
428+
if lfErr != nil {
429+
raven.CaptureErrorAndWait(lfErr, nil)
430+
Log.Critical(lfErr)
431+
panic(lfErr)
432+
}
433+
434+
Log.Debugf(" [%s] - project has %d log files\n", project, len(logFiles))
435+
436+
for _, f := range logFiles {
437+
lFile := filepath.Base(f)
438+
Log.Debugf(" [%s] - moving log file \"%s\" to destination\n", project, lFile)
439+
mvLfErr := os.Rename(f, destination + "/" + lFile)
440+
if mvLfErr != nil {
441+
raven.CaptureErrorAndWait(mvLfErr, nil)
442+
Log.Critical(mvLfErr)
443+
panic(mvLfErr)
444+
}
445+
}
446+
423447
Log.Debugf(" [%s] - artifact processing completed.\n", project)
424448
}

0 commit comments

Comments
 (0)