Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cbuild setup should suppress additional messages before detecting compatible layers #275

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions pkg/builder/csolution/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,18 @@ func (b CSolutionBuilder) generateBuildFiles() (err error) {
args = append(args, "--cbuild2cmake")
}

_, err = b.runCSolution(args, !(b.Options.Debug || b.Options.Verbose))
var stdErr string
if b.Setup {
var csolutionBin string
csolutionBin, err = b.getCSolutionPath()
if err != nil {
return
}
_, stdErr, err = utils.ExecuteCommand(csolutionBin, args...)
} else {
_, err = b.runCSolution(args, !(b.Options.Debug || b.Options.Verbose))
}

log.Debug("csolution command: csolution " + strings.Join(args, " "))

// Execute this code exclusively upon invocation of the 'setup' command.
Expand All @@ -190,8 +201,23 @@ func (b CSolutionBuilder) generateBuildFiles() (err error) {
if listCmdErr != nil {
err = listCmdErr
} else {
utils.LogStdMsg("To resolve undefined variables, copy the settings from cbuild-idx.yml to csolution.yml")
reader := strings.NewReader(stdErr)
scanner := bufio.NewScanner(reader)

var errMsg string
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "undefined variables in") || strings.HasSuffix(line, "-Layer$") {
errMsg += (line + "\n")
}
}
if errMsg != "" {
errMsg += "To resolve undefined variables, copy the settings from cbuild-idx.yml to csolution.yml"
utils.LogStdMsg(errMsg)
}
}
} else {
utils.LogStdMsg(stdErr)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package utils

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -397,3 +398,15 @@ func isFileSystemCaseInsensitive() bool {
// On Linux, file systems are typically case sensitive
return filepath.Separator == '\\' || strings.Contains(strings.ToLower(os.Getenv("OS")), "darwin")
}

// This exclusive function returns the standard output and standard error as strings
func ExecuteCommand(program string, args ...string) (string, string, error) {
soumeh01 marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.Command(program, args...)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()

return stdout.String(), stderr.String(), err
}
17 changes: 17 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,20 @@ func TestComparePaths(t *testing.T) {
}
})
}

func TestExecuteCommandEx(t *testing.T) {
assert := assert.New(t)
t.Run("execute command normal verbosity", func(t *testing.T) {
outStr, errStr, err := ExecuteCommand("go", "version")
assert.Nil(err)
assert.Empty(errStr)
assert.Regexp("(go\\sversion\\sgo([\\d.]+).*)", outStr)
})

t.Run("execute invalid command", func(t *testing.T) {
outStr, errStr, err := ExecuteCommand("go", "invalid")
assert.Error(err)
assert.Empty(outStr)
assert.Equal("go invalid: unknown command\nRun 'go help' for usage.\n", errStr)
})
}