Skip to content

Commit

Permalink
feat: Added stdout and stderr to write out to file specified in switc…
Browse files Browse the repository at this point in the history
…h command for resultsfile

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>
  • Loading branch information
jimrobison committed Jun 19, 2023
1 parent 77919b0 commit fcc2758
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
21 changes: 21 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"bufio"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -90,6 +91,21 @@ func rootCmd(o *options.Options) error {

ctx := context.Background()
logger := sclog.NewLogger(sclog.ParseLevel(o.LogLevel))

var outFile *os.File

if o.ResultsFile != "" {
outFile, err = os.Create(o.ResultsFile)
if err != nil {
panic(err)

Check warning on line 100 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L94-L100

Added lines #L94 - L100 were not covered by tests
}
defer outFile.Close()
os.Stdout = outFile
os.Stderr = outFile

fmt.Fprintf(os.Stderr, "Repo Queried: %s\n\n", o.Repo)

Check warning on line 106 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L102-L106

Added lines #L102 - L106 were not covered by tests
}

repoURI, repoClient, ossFuzzRepoClient, ciiClient, vulnsClient, err := checker.GetClients(
ctx, o.Repo, o.Local, logger) // MODIFIED
if err != nil {
Expand Down Expand Up @@ -170,5 +186,10 @@ func rootCmd(o *options.Options) error {
return sce.WithMessage(sce.ErrorCheckRuntime, fmt.Sprintf("%s: %v", result.Name, result.Error))
}
}

if outFile != nil {
writer := bufio.NewWriter(outFile)
defer writer.Flush()
}

Check warning on line 193 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L190-L193

Added lines #L190 - L193 were not covered by tests
return nil
}
10 changes: 10 additions & 0 deletions options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const (
FlagFormat = "format"

FlagCommitDepth = "commit-depth"

// FlagResultsFile is the flag name for storing stdout and stderr output

Check failure on line 68 in options/flags.go

View workflow job for this annotation

GitHub Actions / check-linter

Comment should end in a period (godot)
FlagResultsFile = "resultsfile"
)

// Command is an interface for handling options for command-line utilities.
Expand Down Expand Up @@ -162,6 +165,13 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
fmt.Sprintf("Checks to run. Possible values are: %s", strings.Join(checkNames, ",")),
)

cmd.Flags().StringVar(
&o.ResultsFile,
FlagResultsFile,
o.ResultsFile,
"Output results and errors to file",
)

// TODO(options): Extract logic
allowedFormats := []string{
FormatDefault,
Expand Down
6 changes: 6 additions & 0 deletions options/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestOptions_AddFlags(t *testing.T) {
ChecksToRun: []string{"check1", "check2"},
PolicyFile: "policy-file",
Format: "json",
ResultsFile: "resultsFile.log",
},
},
}
Expand Down Expand Up @@ -89,6 +90,11 @@ func TestOptions_AddFlags(t *testing.T) {
t.Errorf("expected FlagRubyGems to be %q, but got %q", tt.opts.RubyGems, cmd.Flag(FlagRubyGems).Value.String())
}

// check ResultsFile
if cmd.Flag(FlagResultsFile).Value.String() != tt.opts.ResultsFile {
t.Errorf("expected ResultsFile to be %q, but got %q", tt.opts.ResultsFile, cmd.Flag(FlagResultsFile).Value.String())
}

var e1 []string
for _, f := range strings.Split(cmd.Flag(FlagChecks).Value.String(), ",") {
f = strings.TrimPrefix(f, "[")
Expand Down
21 changes: 10 additions & 11 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ import (

// Options define common options for configuring scorecard.
type Options struct {
Repo string
Local string
Commit string
LogLevel string
Format string
NPM string
PyPI string
RubyGems string
Nuget string
PolicyFile string
// TODO(action): Add logic for writing results to file
Repo string
Local string
Commit string
LogLevel string
Format string
NPM string
PyPI string
RubyGems string
Nuget string
PolicyFile string
ResultsFile string
ChecksToRun []string
Metadata []string
Expand Down

0 comments on commit fcc2758

Please sign in to comment.