diff --git a/tools/please_go/BUILD b/tools/please_go/BUILD index a65616b6..2604f7c5 100644 --- a/tools/please_go/BUILD +++ b/tools/please_go/BUILD @@ -13,7 +13,6 @@ go_binary( "//tools/please_go/embed", "//tools/please_go/filter", "//tools/please_go/generate", - "//tools/please_go/goget", "//tools/please_go/install", "//tools/please_go/modinfo", "//tools/please_go/packageinfo", @@ -41,7 +40,6 @@ genrule( "//tools/please_go/filter:srcs", "//tools/please_go/generate:srcs", "//tools/please_go/generate/gomoddeps:srcs", - "//tools/please_go/goget:srcs", "//tools/please_go/install:srcs", "//tools/please_go/install/exec:srcs", "//tools/please_go/install/toolchain:srcs", diff --git a/tools/please_go/goget/BUILD b/tools/please_go/goget/BUILD deleted file mode 100644 index 304c2929..00000000 --- a/tools/please_go/goget/BUILD +++ /dev/null @@ -1,16 +0,0 @@ -subinclude("///go//build_defs:go") - -filegroup( - name = "srcs", - srcs = glob(["*.go"]), - visibility = ["//tools/please_go:bootstrap"], -) - -go_library( - name = "goget", - srcs = ["go_get.go"], - visibility = ["//tools/..."], - deps = [ - "//third_party/go:mod", - ], -) diff --git a/tools/please_go/goget/go_get.go b/tools/please_go/goget/go_get.go deleted file mode 100644 index 30b8cffe..00000000 --- a/tools/please_go/goget/go_get.go +++ /dev/null @@ -1,155 +0,0 @@ -package goget - -import ( - "errors" - "fmt" - "io" - "log" - "net/http" - "os" - "strings" - - "golang.org/x/mod/modfile" - "golang.org/x/mod/semver" -) - -var client = http.DefaultClient - -type moduleVersion struct { - mod, ver string -} - -type getter struct { - queryResults map[moduleVersion]*modfile.File - proxyUrl string -} - -func newGetter() *getter { - return &getter{ - queryResults: map[moduleVersion]*modfile.File{}, - proxyUrl: "https://proxy.golang.org", - } -} - -func (g *getter) getGoMod(mod, ver string) (*modfile.File, error) { - modVer := moduleVersion{mod, ver} - if modFile, ok := g.queryResults[modVer]; ok { - return modFile, nil - } - - file := fmt.Sprintf("%s/%s/@v/%s.mod", g.proxyUrl, mod, ver) - resp, err := client.Get(file) - if err != nil { - return nil, err - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != 200 { - return nil, fmt.Errorf("%v %v: \n%v", file, resp.StatusCode, string(body)) - } - - modFile, err := modfile.Parse(file, body, nil) - if err != nil { - return nil, err - } - - g.queryResults[modVer] = modFile - return modFile, nil -} - -// getGoModWithFallback attempts to get a go.mod for the given module and -// version with fallback for supporting modules with case insensitivity. -func (g *getter) getGoModWithFallback(mod, version string) (*modfile.File, error) { - modVersionsToAttempt := map[string]string{ - mod: version, - } - - // attempt lowercasing entire mod string for packages like: - // - `github.com/Sirupsen/logrus` -> `github.com/sirupsen/logrus`. - // https://github.com/sirupsen/logrus/issues/543 - modVersionsToAttempt[strings.ToLower(mod)] = version - - var errs error - for mod, version := range modVersionsToAttempt { - modFile, err := g.getGoMod(mod, version) - if err != nil { - errs = errors.Join(errs, err) - continue - } - - return modFile, nil - } - - return nil, errs -} - -func (g *getter) getDeps(deps map[string]string, mod, version string) error { - modFile, err := g.getGoModWithFallback(mod, version) - if err != nil { - return err - } - - for _, req := range modFile.Require { - oldVer, ok := deps[req.Mod.Path] - if !ok || semver.Compare(oldVer, req.Mod.Version) < 0 { - deps[req.Mod.Path] = req.Mod.Version - if err := g.getDeps(deps, req.Mod.Path, req.Mod.Version); err != nil { - return err - } - } - } - - return nil -} - -func (g *getter) goGet(mods []string) error { - deps := map[string]string{} - for _, mod := range mods { - path, version, ok := strings.Cut(mod, "@") - if !ok { - log.Fatalf("Module spec %s is missing a version; must be in the format golang.org/x/sys@v1.0.0", mod) - } - deps[path] = version - } - - for mod, ver := range deps { - if err := g.getDeps(deps, mod, ver); err != nil { - return err - } - } - - for mod, ver := range deps { - fmt.Printf("go_repo(module=\"%s\", version=\"%s\")\n", mod, ver) - } - return nil -} - -// GoGet is used to spit out a new go_get rule. The plan is to build this out into a tool to add new third party -// modules to the repo. -func GoGet(mods []string) error { - return newGetter().goGet(mods) -} - -func GetMod(path string) error { - g := newGetter() - bs, err := os.ReadFile(path) - if err != nil { - return err - } - - modFile, err := modfile.ParseLax(path, bs, nil) - if err != nil { - return err - } - - paths := make([]string, len(modFile.Require)) - for i, req := range modFile.Require { - paths[i] = fmt.Sprintf("%v@%v", req.Mod.Path, req.Mod.Version) - } - - return g.goGet(paths) -} diff --git a/tools/please_go/please_go.go b/tools/please_go/please_go.go index 25500f0a..d2ae3aca 100644 --- a/tools/please_go/please_go.go +++ b/tools/please_go/please_go.go @@ -13,7 +13,6 @@ import ( "github.com/please-build/go-rules/tools/please_go/embed" "github.com/please-build/go-rules/tools/please_go/filter" "github.com/please-build/go-rules/tools/please_go/generate" - "github.com/please-build/go-rules/tools/please_go/goget" "github.com/please-build/go-rules/tools/please_go/install" "github.com/please-build/go-rules/tools/please_go/modinfo" "github.com/please-build/go-rules/tools/please_go/packageinfo" @@ -181,18 +180,6 @@ var subCommands = map[string]func() int{ } return 0 }, - "get": func() int { - if opts.GoGet.ModFile != "" { - if err := goget.GetMod(opts.GoGet.ModFile); err != nil { - log.Fatalf("failed to generate go rules: %v", err) - } - return 0 - } - if err := goget.GoGet(opts.GoGet.Args.Requirements); err != nil { - log.Fatalf("failed to generate go rules: %v", err) - } - return 0 - }, "package_info": func() int { pi := opts.PackageInfo if err := packageinfo.WritePackageInfo(pi.ImportPath, pi.Pkg, "", pi.ImportMap, nil, pi.Subrepo, pi.Module, os.Stdout); err != nil {