Skip to content

Commit

Permalink
adding a float subcommand for future proofing
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Nichols committed Sep 23, 2020
1 parent 46a9884 commit 029e70c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 44 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<major>.<minor>' (i.e.: 1.23 or v1.23) [required]
```

Example:
Expand All @@ -26,15 +48,28 @@ knative.dev/[email protected]
knative.dev/[email protected]
```

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/[email protected]
k8s.io/[email protected]
k8s.io/[email protected]
k8s.io/[email protected]
k8s.io/[email protected]
k8s.io/gengo@master
k8s.io/klog@master
```

Note: the following are equivalent releases:

- `v0.1`
- `v0.1.0`
- `0.1`
- `0.1.0`


## Rules
### Float Rules

Buoy will select a `ref` for a found dependency, in this order:

Expand All @@ -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.
57 changes: 16 additions & 41 deletions buoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<major>.<minor>' (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)
}
}
53 changes: 53 additions & 0 deletions pkg/float/float.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 029e70c

Please sign in to comment.