From 029e70c0b386052bb64bfa53ef140ee7a5ca7d35 Mon Sep 17 00:00:00 2001 From: Scott Nichols Date: Wed, 23 Sep 2020 08:08:00 -0700 Subject: [PATCH] adding a float subcommand for future proofing --- README.md | 42 +++++++++++++++++++++++++++++++--- buoy.go | 57 +++++++++++++--------------------------------- pkg/float/float.go | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 44 deletions(-) create mode 100644 pkg/float/float.go diff --git a/README.md b/README.md index 34b8a97..21bce1d 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,30 @@ go get github.com/n3wscott/buoy ## Usage +```shell +Usage: + buoy [command] + +Available Commands: + float Find latest versions of dependencies based on a release. + help Help about any command + +Flags: + -h, --help help for buoy + +Use "buoy [command] --help" for more information about a command. ``` -buoy go.mod $release + +### Float + +```shell +Usage: + buoy float go.mod [flags] + +Flags: + -d, --domain string domain filter (default "knative.dev") + -h, --help help for float + -r, --release string release should be '.' (i.e.: 1.23 or v1.23) [required] ``` Example: @@ -26,7 +48,20 @@ knative.dev/serving@v0.15.3 knative.dev/test-infra@release-0.15 ``` -Note: the following are equivalent: +Or set the domain to and target release of that dependency: + +```shell script +$ buoy float go.mod --release 0.18 --domain k8s.io +k8s.io/api@v0.18.10 +k8s.io/apiextensions-apiserver@v0.18.10 +k8s.io/apimachinery@v0.18.10 +k8s.io/client-go@v0.18.10 +k8s.io/code-generator@v0.18.10 +k8s.io/gengo@master +k8s.io/klog@master +``` + +Note: the following are equivalent releases: - `v0.1` - `v0.1.0` @@ -34,7 +69,7 @@ Note: the following are equivalent: - `0.1.0` -## Rules +### Float Rules Buoy will select a `ref` for a found dependency, in this order: @@ -45,3 +80,4 @@ Buoy will select a `ref` for a found dependency, in this order: ## TODO: - Support `go-import` with more than one import on a single page. +- Support release branch templates. For now, hardcoded to Knative style. diff --git a/buoy.go b/buoy.go index f0a20a4..ead0bf8 100644 --- a/buoy.go +++ b/buoy.go @@ -2,66 +2,41 @@ package main import ( "fmt" - "io/ioutil" - "strings" - - "github.com/blang/semver/v4" - "github.com/n3wscott/buoy/pkg/git" - "github.com/n3wscott/buoy/pkg/golang" + "github.com/n3wscott/buoy/pkg/float" "github.com/spf13/cobra" - "golang.org/x/mod/modfile" ) func main() { var domain string + var release string - var buoy = &cobra.Command{ - Use: "buoy go.mod v0.10", - Args: cobra.MinimumNArgs(2), + var floatCmd = &cobra.Command{ + Use: "float go.mod", + Short: "Find latest versions of dependencies based on a release.", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { gomod := args[0] - b, err := ioutil.ReadFile(gomod) - if err != nil { - return err - } - file, err := modfile.Parse(gomod, b, nil) + refs, err := float.Float(gomod, release, domain) if err != nil { return err } - packages := make([]string, 0) - for _, r := range file.Require { - if strings.Contains(r.Mod.Path, domain) { - packages = append(packages, r.Mod.Path) - } - } - - this, err := semver.ParseTolerant(args[1]) - for _, p := range packages { - meta, err := golang.GetMetaImport(p) - if err != nil { - panic(err) - } - - if meta.VCS != "git" { - panic(fmt.Errorf("unknown VCS: %s", meta.VCS)) - } - - repo, err := git.GetRepo(p, meta.RepoRoot) - if err != nil { - panic(err) - } - - fmt.Printf("%s\n", repo.BestRefFor(this)) + for _, r := range refs { + fmt.Printf("%s\n", r) } return nil }, } - buoy.Flags().StringVarP(&domain, "domain", "d", "knative.dev", "domain filter") + floatCmd.Flags().StringVarP(&domain, "domain", "d", "knative.dev", "domain filter") + floatCmd.Flags().StringVarP(&release, "release", "r", "", "release should be '.' (i.e.: 1.23 or v1.23) [required]") + _ = floatCmd.MarkFlagRequired("release") + + var buoyCmd = &cobra.Command{Use: "buoy"} + buoyCmd.AddCommand(floatCmd) - if err := buoy.Execute(); err != nil { + if err := buoyCmd.Execute(); err != nil { panic(err) } } diff --git a/pkg/float/float.go b/pkg/float/float.go new file mode 100644 index 0000000..edbc830 --- /dev/null +++ b/pkg/float/float.go @@ -0,0 +1,53 @@ +package float + +import ( + "fmt" + "github.com/blang/semver/v4" + "github.com/n3wscott/buoy/pkg/git" + "github.com/n3wscott/buoy/pkg/golang" + "golang.org/x/mod/modfile" + "io/ioutil" + "strings" +) + +func Float(gomod, release, domain string) ([]string, error) { + b, err := ioutil.ReadFile(gomod) + if err != nil { + return nil, err + } + + file, err := modfile.Parse(gomod, b, nil) + if err != nil { + return nil, err + } + + packages := make([]string, 0) + for _, r := range file.Require { + // Look for requirements that have the prefix of domain. + if strings.HasPrefix(r.Mod.Path, domain) { + packages = append(packages, r.Mod.Path) + } + } + + this, err := semver.ParseTolerant(release) + + refs := make([]string, 0) + for _, p := range packages { + meta, err := golang.GetMetaImport(p) + if err != nil { + panic(err) + } + + if meta.VCS != "git" { + return nil, fmt.Errorf("unknown VCS: %s", meta.VCS) + } + + repo, err := git.GetRepo(p, meta.RepoRoot) + if err != nil { + return nil, err + } + + refs = append(refs, repo.BestRefFor(this)) + } + return refs, nil +}