mirror of https://github.com/golang/go.git
internal/lsp/cmd: add vulncheck command
Usage: gopls vulncheck <package_pattern> It prints json-encoded command.VulncheckResult in stdout. Change-Id: I752b3f06d7c6b2c7de68bd1221283205955c383f Reviewed-on: https://go-review.googlesource.com/c/tools/+/395676 Trust: Hyang-Ah Hana Kim <hyangah@gmail.com> Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
c62a3b326e
commit
b7db7eb670
|
|
@ -273,6 +273,7 @@ func (app *Application) featureCommands() []tool.Application {
|
|||
&symbols{app: app},
|
||||
newWorkspace(app),
|
||||
&workspaceSymbol{app: app},
|
||||
&vulncheck{app: app},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ Features
|
|||
symbols display selected file's symbols
|
||||
workspace manage the gopls workspace (experimental: under development)
|
||||
workspace_symbol search symbols in workspace
|
||||
vulncheck run experimental vulncheck analysis (experimental: under development)
|
||||
|
||||
flags:
|
||||
-debug=string
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
run experimental vulncheck analysis (experimental: under development)
|
||||
|
||||
Usage:
|
||||
gopls [flags] vulncheck
|
||||
|
||||
WARNING: this command is experimental.
|
||||
|
||||
Example:
|
||||
$ gopls vulncheck <packages>
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
// 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/internal/lsp/command"
|
||||
"golang.org/x/tools/internal/lsp/protocol"
|
||||
"golang.org/x/tools/internal/tool"
|
||||
)
|
||||
|
||||
// vulncheck implements the vulncheck command.
|
||||
type vulncheck struct {
|
||||
app *Application
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
Example:
|
||||
$ gopls vulncheck <packages>
|
||||
`)
|
||||
printFlagDefaults(f)
|
||||
}
|
||||
|
||||
func (v *vulncheck) Run(ctx context.Context, args ...string) error {
|
||||
if len(args) > 1 {
|
||||
return tool.CommandLineErrorf("vulncheck accepts at most one package pattern")
|
||||
}
|
||||
pattern := "."
|
||||
if len(args) == 1 {
|
||||
pattern = args[0]
|
||||
}
|
||||
|
||||
conn, err := v.app.connect(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.terminate(ctx)
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd, err := command.NewRunVulncheckExpCommand("", command.VulncheckArgs{
|
||||
Dir: protocol.URIFromPath(cwd),
|
||||
Pattern: pattern,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params := &protocol.ExecuteCommandParams{Command: cmd.Command, Arguments: cmd.Arguments}
|
||||
res, err := conn.ExecuteCommand(ctx, params)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing server command: %v", err)
|
||||
}
|
||||
data, err := json.MarshalIndent(res, " ", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to decode results: %v", err)
|
||||
}
|
||||
fmt.Printf("%s\n", data)
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in New Issue