From 943a5e7c9880ad7741bcfb4f38565b006e2bef8c Mon Sep 17 00:00:00 2001 From: Vinu K Date: Tue, 20 Jan 2026 13:12:40 +0000 Subject: [PATCH] fix: Use RunCommandStdout for simple queries Reduce noise in the output Signed-off-by: Vinu K --- internal/cli/commands.go | 11 +++++++++++ pkg/cmd/cg/scanner.go | 14 +++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/internal/cli/commands.go b/internal/cli/commands.go index 73bc2ca..bb00bca 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -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"` diff --git a/pkg/cmd/cg/scanner.go b/pkg/cmd/cg/scanner.go index b00415f..d392c46 100644 --- a/pkg/cmd/cg/scanner.go +++ b/pkg/cmd/cg/scanner.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)