Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ func RunCommand(dir string, command string, args ...string) ([]byte, error) {
return out, err
}

// RunCommandStdout executes a command and returns only stdout (ignoring stderr)
// This is useful for queries where stderr contains informational messages
// that should not be included in the result (e.g., "go: finding module" messages)
func RunCommandStdout(dir string, command string, args ...string) ([]byte, error) {
cmd := exec.Command(command, args...)
cmd.Env = append(os.Environ(), "GOFLAGS=-mod=mod", "GOWORK=off")
cmd.Dir = dir
out, err := cmd.Output()
return out, err
}

// Result represents the result structure for CLI command operations
type Result struct {
IsVulnerable string `json:"IsVulnerable"`
Expand Down
14 changes: 7 additions & 7 deletions pkg/cmd/cg/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ func getCurrentVersion(pkg string, dir string, result *Result) string {

cmd := "go"
args := []string{"list", "-f", "{{if .Module}}{{.Module.Version}}{{end}}", pkg}
out, err := cli.RunCommand(dir, cmd, args...)
out, err := cli.RunCommandStdout(dir, cmd, args...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", cmd, strings.Join(args, " "), dir, strings.TrimSpace(string(out)))
result.Errors = append(result.Errors, errMsg)
Expand All @@ -861,7 +861,7 @@ func getCurrentVersion(pkg string, dir string, result *Result) string {
func getGoToolchainVersion(dir string, result *Result) string {
cmd := "go"
args := []string{"mod", "edit", "-json"}
out, err := cli.RunCommand(dir, cmd, args...)
out, err := cli.RunCommandStdout(dir, cmd, args...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", cmd, strings.Join(args, " "), dir, strings.TrimSpace(string(out)))
result.Errors = append(result.Errors, errMsg)
Expand Down Expand Up @@ -892,7 +892,7 @@ func getGoToolchainVersion(dir string, result *Result) string {
func getReplaceVersion(pkg string, dir string, result *Result) string {
cmd := "go"
args := []string{"mod", "edit", "-json"}
out, err := cli.RunCommand(dir, cmd, args...)
out, err := cli.RunCommandStdout(dir, cmd, args...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", cmd, strings.Join(args, " "), dir, strings.TrimSpace(string(out)))
result.Errors = append(result.Errors, errMsg)
Expand Down Expand Up @@ -961,7 +961,7 @@ func getFixedVersion(id, pkg string, result *Result) []string {
func getModPath(pkg, dir string, result *Result) string {
cmd := "go"
args := []string{"list", "-f", "{{if .Module}}{{.Module.Path}}{{end}}", pkg}
out, err := cli.RunCommand(dir, cmd, args...)
out, err := cli.RunCommandStdout(dir, cmd, args...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", cmd, strings.Join(args, " "), dir, strings.TrimSpace(string(out)))
result.Errors = append(result.Errors, errMsg)
Expand All @@ -973,7 +973,7 @@ func getModPath(pkg, dir string, result *Result) string {
func getGitBranch(result *Result) {
cmd := "git"
args := []string{"rev-parse", "--abbrev-ref", "HEAD"}
out, err := cli.RunCommand(result.Directory, cmd, args...)
out, err := cli.RunCommandStdout(result.Directory, cmd, args...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", cmd, strings.Join(args, " "), result.Directory, strings.TrimSpace(string(out)))
result.Errors = append(result.Errors, errMsg)
Expand All @@ -987,7 +987,7 @@ func getGitBranch(result *Result) {
if branchName == "HEAD" {
commitCmd := "git"
commitArgs := []string{"rev-parse", "HEAD"}
commitOut, err := cli.RunCommand(result.Directory, commitCmd, commitArgs...)
commitOut, err := cli.RunCommandStdout(result.Directory, commitCmd, commitArgs...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", commitCmd, strings.Join(commitArgs, " "), result.Directory, strings.TrimSpace(string(commitOut)))
result.Errors = append(result.Errors, errMsg)
Expand All @@ -1003,7 +1003,7 @@ func getGitBranch(result *Result) {
func getGitURL(result *Result) {
cmd := "git"
args := []string{"remote", "get-url", "origin"}
out, err := cli.RunCommand(result.Directory, cmd, args...)
out, err := cli.RunCommandStdout(result.Directory, cmd, args...)
if err != nil {
errMsg := fmt.Sprintf("Failed to run %s %s in %s: %s", cmd, strings.Join(args, " "), result.Directory, strings.TrimSpace(string(out)))
result.Errors = append(result.Errors, errMsg)
Expand Down