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

Add debug flag and logging to scan #278

Merged
merged 3 commits into from
Nov 7, 2024
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
6 changes: 6 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var callgraphGenerateTimeout int
var callgraphUploadTimeout int
var commitAuthor string
var commitName string
var debug bool
var exclusions = file.Exclusions()
var inclusions = file.Exclusions()
var integrationName string
Expand All @@ -46,6 +47,7 @@ const (
CallGraphUploadTimeoutFlag = "callgraph-upload-timeout"
CommitFlag = "commit"
CommitAuthorFlag = "author"
DebugFlag = "debug"
ExclusionFlag = "exclusion"
IntegrationFlag = "integration"
InclusionFlag = "inclusion"
Expand Down Expand Up @@ -145,6 +147,7 @@ $ debricked scan . --include '**/node_modules/**'`)
"\nExample:\n$ debricked resolve --verbose=false",
}, "\n")
cmd.Flags().BoolVar(&verbose, VerboseFlag, true, verboseDoc)
cmd.Flags().BoolVar(&debug, DebugFlag, false, "write all debug output to stderr")
cmd.Flags().BoolVarP(&passOnDowntime, PassOnTimeOut, "p", false, "pass scan if there is a service access timeout")
cmd.Flags().BoolVar(&noResolve, NoResolveFlag, false, `disables resolution of manifest files that lack lock files. Resolving manifest files enables more accurate dependency scanning since the whole dependency tree will be analysed.
For example, if there is a "go.mod" in the target path, its dependencies are going to get resolved onto a lock file, and latter scanned.`)
Expand Down Expand Up @@ -188,6 +191,8 @@ Leaving the field empty results in no SBOM generation.`,

func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
fmt.Println("Scanner started...")

path := ""
if len(args) > 0 {
path = args[0]
Expand All @@ -214,6 +219,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
SBOMOutput: viper.GetString(SBOMOutputFlag),
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Debug: viper.GetBool(DebugFlag),
Regenerate: viper.GetInt(RegenerateFlag),
VersionHint: viper.GetBool(VersionHintFlag),
RepositoryName: viper.GetString(RepositoryFlag),
Expand Down
15 changes: 15 additions & 0 deletions internal/debug/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package debug

import (
"log"
"os"

"github.com/fatih/color"
)

func Log(message string, debug bool) {
if debug {
DebugLogger := log.New(os.Stderr, "DEBUG: ", log.Ldate|log.Ltime)
DebugLogger.Println(color.BlueString(message))
}
}
38 changes: 38 additions & 0 deletions internal/debug/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package debug

import (
"io"
"os"
"testing"

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

func TestLog(t *testing.T) {
rescueStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

Log("hello", true)

_ = w.Close()
output, _ := io.ReadAll(r)
os.Stderr = rescueStderr

assert.Contains(t, string(output), "DEBUG: ")
assert.Contains(t, string(output), "hello\n")
}

func TestLogDebugDisabled(t *testing.T) {
rescueStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

Log("hello", false)

_ = w.Close()
output, _ := io.ReadAll(r)
os.Stderr = rescueStderr

assert.Empty(t, string(output))
}
11 changes: 11 additions & 0 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/debricked/cli/internal/ci"
"github.com/debricked/cli/internal/ci/env"
"github.com/debricked/cli/internal/client"
"github.com/debricked/cli/internal/debug"
"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/fingerprint"
"github.com/debricked/cli/internal/git"
Expand Down Expand Up @@ -54,6 +55,7 @@ type DebrickedOptions struct {
Exclusions []string
Inclusions []string
Verbose bool
Debug bool
Regenerate int
VersionHint bool
RepositoryName string
Expand Down Expand Up @@ -96,15 +98,18 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {
if !ok {
return BadOptsErr
}
debug.Log("Options initialized, finding CI service...", dOptions.Debug)

e, _ := dScanner.ciService.Find()

debug.Log("Mapping environment variables...", dOptions.Debug)
MapEnvToOptions(&dOptions, e)

if err := SetWorkingDirectory(&dOptions); err != nil {
return err
}

debug.Log("Setting up git objects...", dOptions.Debug)
gitMetaObject, err := git.NewMetaObject(
dOptions.Path,
dOptions.RepositoryName,
Expand All @@ -117,6 +122,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {
return err
}

debug.Log("Running scan with initialized scanner...", dOptions.Debug)
result, err := dScanner.scan(dOptions, *gitMetaObject)
if err != nil {
return dScanner.handleScanError(err, dOptions.PassOnTimeOut)
Expand Down Expand Up @@ -216,17 +222,20 @@ func (dScanner *DebrickedScanner) scanFingerprint(options DebrickedOptions) erro

func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject git.MetaObject) (*upload.UploadResult, error) {

debug.Log("Running scanResolve...", options.Debug)
err := dScanner.scanResolve(options)
if err != nil {
return nil, err
}

debug.Log("Running scanFingerprint...", options.Debug)
err = dScanner.scanFingerprint(options)
if err != nil {
return nil, err
}

if options.CallGraph {
debug.Log("Running scanFingerprint...", options.Debug)
configs := []config.IConfig{
config.NewConfig("java", []string{}, map[string]string{"pm": "maven"}, true, "maven"),
config.NewConfig("golang", []string{}, map[string]string{"pm": "go"}, true, "go"),
Expand All @@ -250,6 +259,7 @@ func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject g
}
}

debug.Log("Matching groups...", options.Debug)
fileGroups, err := dScanner.finder.GetGroups(
file.DebrickedOptions{
RootPath: options.Path,
Expand All @@ -263,6 +273,7 @@ func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject g
return nil, err
}

debug.Log("Starting upload...", options.Debug)
uploaderOptions := upload.DebrickedOptions{
FileGroups: fileGroups,
GitMetaObject: gitMetaObject,
Expand Down
Loading