1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:24:48 -07:00
go/internal/lsp/command.go
Rohan Challa c4f5635f10 internal/lsp: add an upgrade all dependencies codelens
This change adds an upgrade all dependencies codelens on the go.mod file if there are available upgrades.

Updates golang/go#36501

Change-Id: I86c1ae7e7a6dc01b7f5cd7eb18e5a11d96a3acc1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/221108
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-03-02 21:30:18 +00:00

57 lines
1.7 KiB
Go

package lsp
import (
"context"
"strings"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
errors "golang.org/x/xerrors"
)
func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
switch params.Command {
case "tidy":
if len(params.Arguments) == 0 || len(params.Arguments) > 1 {
return nil, errors.Errorf("expected one file URI for call to `go mod tidy`, got %v", params.Arguments)
}
uri := protocol.DocumentURI(params.Arguments[0].(string))
snapshot, _, ok, err := s.beginFileRequest(uri, source.Mod)
if !ok {
return nil, err
}
// Run go.mod tidy on the view.
inv := gocommand.Invocation{
Verb: "mod",
Args: []string{"tidy"},
Env: snapshot.Config(ctx).Env,
WorkingDir: snapshot.View().Folder().Filename(),
}
if _, err := inv.Run(ctx); err != nil {
return nil, err
}
case "upgrade.dependency":
if len(params.Arguments) < 2 {
return nil, errors.Errorf("expected one file URI and one dependency for call to `go get`, got %v", params.Arguments)
}
uri := protocol.DocumentURI(params.Arguments[0].(string))
deps := params.Arguments[1].(string)
snapshot, _, ok, err := s.beginFileRequest(uri, source.UnknownKind)
if !ok {
return nil, err
}
// Run "go get" on the dependency to upgrade it to the latest version.
inv := gocommand.Invocation{
Verb: "get",
Args: strings.Split(deps, " "),
Env: snapshot.Config(ctx).Env,
WorkingDir: snapshot.View().Folder().Filename(),
}
if _, err := inv.Run(ctx); err != nil {
return nil, err
}
}
return nil, nil
}