mirror of
https://github.com/golang/go
synced 2024-11-18 14:14:46 -07:00
c5cec6710e
Over time we have accumulated a disturbing number of ways to run the Go command, each of which supported slightly different features and workarounds. Combine all of them. This unavoidably introduces some changes in debug logging behavior; I think we should try to fix them incrementally and consistently. Updates golang/go#37368. Change-Id: I664ca8685bf247a226be3cb807789c2fcddf233d Reviewed-on: https://go-review.googlesource.com/c/tools/+/220737 Run-TryBot: Heschi Kreinick <heschi@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package lsp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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))
|
|
snapshot, _, ok, err := s.beginFileRequest(uri, source.UnknownKind)
|
|
if !ok {
|
|
return nil, err
|
|
}
|
|
dep := params.Arguments[1].(string)
|
|
// Run "go get" on the dependency to upgrade it to the latest version.
|
|
inv := gocommand.Invocation{
|
|
Verb: "get",
|
|
Args: []string{dep},
|
|
Env: snapshot.Config(ctx).Env,
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
}
|
|
if _, err := inv.Run(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|