Skip to content

Commit 029e70c

Browse files
author
Scott Nichols
committed
adding a float subcommand for future proofing
1 parent 46a9884 commit 029e70c

3 files changed

Lines changed: 108 additions & 44 deletions

File tree

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,30 @@ go get github.com/n3wscott/buoy
1212

1313
## Usage
1414

15+
```shell
16+
Usage:
17+
buoy [command]
18+
19+
Available Commands:
20+
float Find latest versions of dependencies based on a release.
21+
help Help about any command
22+
23+
Flags:
24+
-h, --help help for buoy
25+
26+
Use "buoy [command] --help" for more information about a command.
1527
```
16-
buoy go.mod $release
28+
29+
### Float
30+
31+
```shell
32+
Usage:
33+
buoy float go.mod [flags]
34+
35+
Flags:
36+
-d, --domain string domain filter (default "knative.dev")
37+
-h, --help help for float
38+
-r, --release string release should be '<major>.<minor>' (i.e.: 1.23 or v1.23) [required]
1739
```
1840

1941
Example:
@@ -26,15 +48,28 @@ knative.dev/serving@v0.15.3
2648
knative.dev/test-infra@release-0.15
2749
```
2850

29-
Note: the following are equivalent:
51+
Or set the domain to and target release of that dependency:
52+
53+
```shell script
54+
$ buoy float go.mod --release 0.18 --domain k8s.io
55+
k8s.io/api@v0.18.10
56+
k8s.io/apiextensions-apiserver@v0.18.10
57+
k8s.io/apimachinery@v0.18.10
58+
k8s.io/client-go@v0.18.10
59+
k8s.io/code-generator@v0.18.10
60+
k8s.io/gengo@master
61+
k8s.io/klog@master
62+
```
63+
64+
Note: the following are equivalent releases:
3065

3166
- `v0.1`
3267
- `v0.1.0`
3368
- `0.1`
3469
- `0.1.0`
3570

3671

37-
## Rules
72+
### Float Rules
3873

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

@@ -45,3 +80,4 @@ Buoy will select a `ref` for a found dependency, in this order:
4580
## TODO:
4681

4782
- Support `go-import` with more than one import on a single page.
83+
- Support release branch templates. For now, hardcoded to Knative style.

buoy.go

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,41 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
"strings"
7-
8-
"github.com/blang/semver/v4"
9-
"github.com/n3wscott/buoy/pkg/git"
10-
"github.com/n3wscott/buoy/pkg/golang"
5+
"github.com/n3wscott/buoy/pkg/float"
116
"github.com/spf13/cobra"
12-
"golang.org/x/mod/modfile"
137
)
148

159
func main() {
1610
var domain string
11+
var release string
1712

18-
var buoy = &cobra.Command{
19-
Use: "buoy go.mod v0.10",
20-
Args: cobra.MinimumNArgs(2),
13+
var floatCmd = &cobra.Command{
14+
Use: "float go.mod",
15+
Short: "Find latest versions of dependencies based on a release.",
16+
Args: cobra.ExactArgs(1),
2117
RunE: func(cmd *cobra.Command, args []string) error {
2218
gomod := args[0]
23-
b, err := ioutil.ReadFile(gomod)
24-
if err != nil {
25-
return err
26-
}
2719

28-
file, err := modfile.Parse(gomod, b, nil)
20+
refs, err := float.Float(gomod, release, domain)
2921
if err != nil {
3022
return err
3123
}
3224

33-
packages := make([]string, 0)
34-
for _, r := range file.Require {
35-
if strings.Contains(r.Mod.Path, domain) {
36-
packages = append(packages, r.Mod.Path)
37-
}
38-
}
39-
40-
this, err := semver.ParseTolerant(args[1])
41-
for _, p := range packages {
42-
meta, err := golang.GetMetaImport(p)
43-
if err != nil {
44-
panic(err)
45-
}
46-
47-
if meta.VCS != "git" {
48-
panic(fmt.Errorf("unknown VCS: %s", meta.VCS))
49-
}
50-
51-
repo, err := git.GetRepo(p, meta.RepoRoot)
52-
if err != nil {
53-
panic(err)
54-
}
55-
56-
fmt.Printf("%s\n", repo.BestRefFor(this))
25+
for _, r := range refs {
26+
fmt.Printf("%s\n", r)
5727
}
5828
return nil
5929
},
6030
}
6131

62-
buoy.Flags().StringVarP(&domain, "domain", "d", "knative.dev", "domain filter")
32+
floatCmd.Flags().StringVarP(&domain, "domain", "d", "knative.dev", "domain filter")
33+
floatCmd.Flags().StringVarP(&release, "release", "r", "", "release should be '<major>.<minor>' (i.e.: 1.23 or v1.23) [required]")
34+
_ = floatCmd.MarkFlagRequired("release")
35+
36+
var buoyCmd = &cobra.Command{Use: "buoy"}
37+
buoyCmd.AddCommand(floatCmd)
6338

64-
if err := buoy.Execute(); err != nil {
39+
if err := buoyCmd.Execute(); err != nil {
6540
panic(err)
6641
}
6742
}

pkg/float/float.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package float
2+
3+
import (
4+
"fmt"
5+
"github.com/blang/semver/v4"
6+
"github.com/n3wscott/buoy/pkg/git"
7+
"github.com/n3wscott/buoy/pkg/golang"
8+
"golang.org/x/mod/modfile"
9+
"io/ioutil"
10+
"strings"
11+
)
12+
13+
func Float(gomod, release, domain string) ([]string, error) {
14+
b, err := ioutil.ReadFile(gomod)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
file, err := modfile.Parse(gomod, b, nil)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
packages := make([]string, 0)
25+
for _, r := range file.Require {
26+
// Look for requirements that have the prefix of domain.
27+
if strings.HasPrefix(r.Mod.Path, domain) {
28+
packages = append(packages, r.Mod.Path)
29+
}
30+
}
31+
32+
this, err := semver.ParseTolerant(release)
33+
34+
refs := make([]string, 0)
35+
for _, p := range packages {
36+
meta, err := golang.GetMetaImport(p)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
if meta.VCS != "git" {
42+
return nil, fmt.Errorf("unknown VCS: %s", meta.VCS)
43+
}
44+
45+
repo, err := git.GetRepo(p, meta.RepoRoot)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
refs = append(refs, repo.BestRefFor(this))
51+
}
52+
return refs, nil
53+
}

0 commit comments

Comments
 (0)