Skip to content

Commit 074ef1f

Browse files
committed
internal/govulncheck: change Run to return Result struct
The function signature for Run is changed to: func Run(ctx context.Context, cfg Config) (*Result, error) {...} At the moment, Result is an empty struct. Fields will be added in subsequent CLs. For golang/go#56042 Change-Id: I4568ca8147809c0c4e26aa83c96440d16ca32ffb Reviewed-on: https://go-review.googlesource.com/c/vuln/+/439075 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Julie Qiu <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent acb1657 commit 074ef1f

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

cmd/govulncheck/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ For details, see https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck.
7676
}
7777

7878
ctx := context.Background()
79-
err := govulncheck.Run(ctx, govulncheck.Config{
79+
_, err := govulncheck.Run(ctx, govulncheck.Config{
8080
AnalysisType: mode,
8181
OutputType: outputType,
8282
Patterns: patterns,

exp/govulncheck/govulncheck.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ type Config = govulncheck.Config
1717
// Main is the main function for the govulncheck command line tool.
1818
func Main(cfg Config) error {
1919
ctx := context.Background()
20-
return govulncheck.Run(ctx, cfg)
20+
_, err := govulncheck.Run(ctx, cfg)
21+
return err
2122
}

internal/govulncheck/result.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
package govulncheck
5+
6+
// Result is contains output information for govulncheck.
7+
//
8+
// TODO(https://go.dev/issue/56042): this API is a work in progress.
9+
type Result struct {
10+
}

internal/govulncheck/run.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
// Run is the main function for the govulncheck command line tool.
24-
func Run(ctx context.Context, cfg Config) error {
24+
func Run(ctx context.Context, cfg Config) (*Result, error) {
2525
dbs := []string{vulndbHost}
2626
if db := os.Getenv(envGOVULNDB); db != "" {
2727
dbs = strings.Split(db, ",")
@@ -30,7 +30,7 @@ func Run(ctx context.Context, cfg Config) error {
3030
HTTPCache: DefaultCache(),
3131
})
3232
if err != nil {
33-
return err
33+
return nil, err
3434
}
3535
vcfg := &vulncheck.Config{Client: dbClient, SourceGoVersion: internal.GoVersion()}
3636

@@ -47,61 +47,67 @@ func Run(ctx context.Context, cfg Config) error {
4747
case AnalysisTypeBinary:
4848
f, err := os.Open(cfg.Patterns[0])
4949
if err != nil {
50-
return err
50+
return nil, err
5151
}
5252
defer f.Close()
5353
r, err = binary(ctx, f, vcfg)
5454
if err != nil {
55-
return err
55+
return nil, err
5656
}
5757
case AnalysisTypeSource:
5858
pkgs, err = loadPackages(cfg)
5959
if err != nil {
6060
// Try to provide a meaningful and actionable error message.
6161
if !fileExists(filepath.Join(cfg.SourceLoadConfig.Dir, "go.mod")) {
62-
return ErrNoGoMod
62+
return nil, ErrNoGoMod
6363
}
6464
if !fileExists(filepath.Join(cfg.SourceLoadConfig.Dir, "go.sum")) {
65-
return ErrNoGoSum
65+
return nil, ErrNoGoSum
6666
}
6767
if isGoVersionMismatchError(err) {
68-
return fmt.Errorf("%v\n\n%v", ErrGoVersionMismatch, err)
68+
return nil, fmt.Errorf("%v\n\n%v", ErrGoVersionMismatch, err)
6969
}
70-
return err
70+
return nil, err
7171
}
7272

7373
// Sort pkgs so that the PkgNodes returned by vulncheck.Source will be
7474
// deterministic.
7575
sortPackages(pkgs)
7676
r, err = vulncheck.Source(ctx, pkgs, vcfg)
7777
if err != nil {
78-
return err
78+
return nil, err
7979
}
8080
unaffected = filterUnaffected(r)
8181
r.Vulns = filterCalled(r)
8282
default:
83-
return fmt.Errorf("%w: %s", ErrInvalidAnalysisType, cfg.AnalysisType)
83+
return nil, fmt.Errorf("%w: %s", ErrInvalidAnalysisType, cfg.AnalysisType)
8484
}
8585

8686
switch cfg.OutputType {
8787
case OutputTypeJSON:
8888
// Following golang.org/x/tools/go/analysis/singlechecker,
8989
// return 0 exit code in -json mode.
90-
return writeJSON(r)
90+
if err := writeJSON(r); err != nil {
91+
return nil, err
92+
}
93+
return &Result{}, nil
9194
case OutputTypeText, OutputTypeVerbose:
9295
// set of top-level packages, used to find representative symbols
9396
ci := getCallInfo(r, pkgs)
9497
writeText(r, ci, unaffected, cfg.OutputType == OutputTypeVerbose)
9598
case OutputTypeSummary:
9699
ci := getCallInfo(r, pkgs)
97-
return writeJSON(summary(ci, unaffected))
100+
if err := writeJSON(summary(ci, unaffected)); err != nil {
101+
return nil, err
102+
}
103+
return &Result{}, nil
98104
default:
99-
return fmt.Errorf("%w: %s", ErrInvalidOutputType, cfg.OutputType)
105+
return nil, fmt.Errorf("%w: %s", ErrInvalidOutputType, cfg.OutputType)
100106
}
101107
if len(r.Vulns) > 0 {
102-
return ErrContainsVulnerabilties
108+
return nil, ErrContainsVulnerabilties
103109
}
104-
return nil
110+
return &Result{}, nil
105111
}
106112

107113
func writeJSON(r any) error {

0 commit comments

Comments
 (0)