File: vulncheck.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (84 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cmd

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"os"

	"golang.org/x/tools/go/packages"
	vulnchecklib "golang.org/x/tools/gopls/internal/vulncheck"
	"golang.org/x/tools/internal/tool"
)

// vulncheck implements the vulncheck command.
type vulncheck struct {
	Config    bool `flag:"config" help:"If true, the command reads a JSON-encoded package load configuration from stdin"`
	AsSummary bool `flag:"summary" help:"If true, outputs a JSON-encoded govulnchecklib.Summary JSON"`
	app       *Application
}

type pkgLoadConfig struct {
	// BuildFlags is a list of command-line flags to be passed through to
	// the build system's query tool.
	BuildFlags []string

	// If Tests is set, the loader includes related test packages.
	Tests bool
}

// TODO(hyangah): document pkgLoadConfig

func (v *vulncheck) Name() string   { return "vulncheck" }
func (v *vulncheck) Parent() string { return v.app.Name() }
func (v *vulncheck) Usage() string  { return "" }
func (v *vulncheck) ShortHelp() string {
	return "run experimental vulncheck analysis (experimental: under development)"
}
func (v *vulncheck) DetailedHelp(f *flag.FlagSet) {
	fmt.Fprint(f.Output(), `
	WARNING: this command is experimental.

	By default, the command outputs a JSON-encoded
	golang.org/x/tools/gopls/internal/lsp/command.VulncheckResult
	message.
	Example:
	$ gopls vulncheck <packages>

`)
	printFlagDefaults(f)
}

func (v *vulncheck) Run(ctx context.Context, args ...string) error {
	if vulnchecklib.Main == nil {
		return fmt.Errorf("vulncheck command is available only in gopls compiled with go1.18 or newer")
	}

	// TODO(hyangah): what's wrong with allowing multiple targets?
	if len(args) > 1 {
		return tool.CommandLineErrorf("vulncheck accepts at most one package pattern")
	}
	var cfg pkgLoadConfig
	if v.Config {
		if err := json.NewDecoder(os.Stdin).Decode(&cfg); err != nil {
			return tool.CommandLineErrorf("failed to parse cfg: %v", err)
		}
	}
	loadCfg := packages.Config{
		Context:    ctx,
		Tests:      cfg.Tests,
		BuildFlags: cfg.BuildFlags,
		// inherit the current process's cwd and env.
	}

	if err := vulnchecklib.Main(loadCfg, args...); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	return nil
}