Skip to content

Commit

Permalink
feat(build): parse cmd execution for errors
Browse files Browse the repository at this point in the history
Closes #3779
  • Loading branch information
squakez committed Nov 2, 2022
1 parent 8c8b465 commit f602536
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 25 deletions.
57 changes: 37 additions & 20 deletions pkg/util/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ import (
"bufio"
"context"
"fmt"
"io"
"os/exec"

"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

// RunAndLog starts the provided command, scans its standard and error outputs line by line,
// to feed the provided handlers, and waits until the scans complete and the command returns.
func RunAndLog(ctx context.Context, cmd *exec.Cmd, stdOutF func(string), stdErrF func(string)) error {
func RunAndLog(ctx context.Context, cmd *exec.Cmd, stdOutF func(string) string, stdErrF func(string) string) error {
scanOutMsg := ""
scanErrMsg := ""
stdOutF(fmt.Sprintf("Executed command: %s", cmd.String()))

stdOut, err := cmd.StdoutPipe()
Expand All @@ -40,38 +44,51 @@ func RunAndLog(ctx context.Context, cmd *exec.Cmd, stdOutF func(string), stdErrF
return err
}
err = cmd.Start()
// if the command is in error, we try to figure it out why also by parsing the log
if err != nil {
scanOut := bufio.NewScanner(stdOut)
for scanOut.Scan() {
stdOutF(scanOut.Text())
}
scanErr := bufio.NewScanner(stdErr)
for scanErr.Scan() {
stdOutF(scanErr.Text())
}
return err
scanOutMsg = scan(stdOut, stdOutF)
scanErrMsg = scan(stdErr, stdErrF)

return errors.Wrapf(err, formatErr(scanOutMsg, scanErrMsg))
}
g, _ := errgroup.WithContext(ctx)
g.Go(func() error {
scanner := bufio.NewScanner(stdOut)
for scanner.Scan() {
stdOutF(scanner.Text())
}
scanOutMsg = scan(stdOut, stdOutF)
return nil
})
g.Go(func() error {
scanner := bufio.NewScanner(stdErr)
for scanner.Scan() {
stdErrF(scanner.Text())
}
scanErrMsg = scan(stdErr, stdErrF)
return nil
})
if err = g.Wait(); err != nil {
return err
return errors.Wrapf(err, formatErr(scanOutMsg, scanErrMsg))
}
if err = cmd.Wait(); err != nil {
return err
return errors.Wrapf(err, formatErr(scanOutMsg, scanErrMsg))
}

return nil
}

func scan(stdOut io.ReadCloser, stdOutF func(string) string) string {
scanError := ""
scanner := bufio.NewScanner(stdOut)
for scanner.Scan() {
errMsg := stdOutF(scanner.Text())
if errMsg != "" && scanError == "" {
scanError = errMsg
}
}

return scanError
}

func formatErr(stdout, stderr string) string {
if stderr == "" {
return stdout
}
if stdout == "" {
return stderr
}
return fmt.Sprintf("stdout: %s, stderr: %s", stdout, stderr)
}
60 changes: 60 additions & 0 deletions pkg/util/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"context"
"fmt"
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

var (
loggerInfo = func(s string) string {
fmt.Println("OUT:", s)
if strings.Contains(s, "invalid") {
return s
}
return ""
}
loggerError = func(s string) string {
fmt.Println("ERR:", s)
if strings.Contains(s, "invalid") {
return s
}
return ""
}
)

func TestRunAndLog(t *testing.T) {
cmd := exec.CommandContext(context.Background(), "/usr/bin/date")
err := RunAndLog(context.Background(), cmd, loggerInfo, loggerError)

assert.Nil(t, err)
}

func TestRunAndLogInvalid(t *testing.T) {
cmd := exec.CommandContext(context.Background(), "/usr/bin/date", "-dsa")
err := RunAndLog(context.Background(), cmd, loggerInfo, loggerError)

assert.NotNil(t, err)
assert.Equal(t, "/usr/bin/date: invalid date ‘sa’: exit status 1", err.Error())
}
4 changes: 2 additions & 2 deletions pkg/util/jvm/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
var (
logger = log.WithName("keytool")

loggerInfo = func(s string) { logger.Info(s) }
loggerError = func(s string) { logger.Error(nil, s) }
loggerInfo = func(s string) string { logger.Info(s); return "" }
loggerError = func(s string) string { logger.Error(nil, s); return "" }
)

func GenerateKeystore(ctx context.Context, keystoreDir, keystoreName, keystorePass string, data [][]byte) error {
Expand Down
19 changes: 16 additions & 3 deletions pkg/util/maven/maven_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package maven

import (
"encoding/json"
"strings"

"github.com/apache/camel-k/pkg/util/log"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ const (

var mavenLogger = log.WithName("maven.build")

func mavenLogHandler(s string) {
func mavenLogHandler(s string) string {
mavenLog, parseError := parseLog(s)
if parseError == nil {
normalizeLog(mavenLog)
Expand All @@ -57,6 +58,13 @@ func mavenLogHandler(s string) {
// etc). The build may still have succeeded, though.
nonNormalizedLog(s)
}

// Return the error message according to maven log
if strings.HasPrefix(s, "[ERROR]") {
return s
}

return ""
}

func parseLog(line string) (mavenLog, error) {
Expand All @@ -72,10 +80,15 @@ func normalizeLog(mavenLog mavenLog) {
case INFO, WARN:
mavenLogger.Info(mavenLog.Msg)
case ERROR, FATAL:
mavenLogger.Errorf(nil, mavenLog.Msg)
mavenLogger.Error(nil, mavenLog.Msg)
}
}

func nonNormalizedLog(rawLog string) {
mavenLogger.Info(rawLog)
// Distinguish an error message from the rest
if strings.HasPrefix(rawLog, "[ERROR]") {
mavenLogger.Error(nil, rawLog)
} else {
mavenLogger.Info(rawLog)
}
}
35 changes: 35 additions & 0 deletions pkg/util/maven/maven_log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package maven

import (
"context"
"os/exec"
"testing"

"github.com/apache/camel-k/pkg/util"
"github.com/stretchr/testify/assert"
)

func TestRunAndLogErrorMvn(t *testing.T) {
cmd := exec.CommandContext(context.Background(), "/usr/bin/mvn", "package")
err := util.RunAndLog(context.Background(), cmd, mavenLogHandler, mavenLogHandler)

assert.NotNil(t, err)
assert.ErrorContains(t, err, "[ERROR] The goal you specified requires a project to execute but there is no POM in this directory")
}

0 comments on commit f602536

Please sign in to comment.