Skip to content

Commit 1cf647f

Browse files
committed
support tags
1 parent 5dd0272 commit 1cf647f

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ test-release:
2121

2222
## toolbox - start
2323
## Location to install dependencies to
24-
LOCALBIN ?= $(shell pwd)/bin
24+
LOCALBIN ?= $(shell test -s "cygpath -m $$(pwd)" || pwd)/bin
2525
$(LOCALBIN):
2626
mkdir -p $(LOCALBIN)
2727

2828
## Tool Binaries
2929
SEMVER ?= $(LOCALBIN)/semver
3030
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
31+
DEEPCOPY_GEN ?= $(LOCALBIN)/deepcopy-gen
3132

3233
## Tool Versions
3334
SEMVER_VERSION ?= v1.1.3
3435
GOLANGCI_LINT_VERSION ?= v1.50.1
36+
DEEPCOPY_GEN_VERSION ?= v0.26.0
3537

3638
## Tool Installer
3739
.PHONY: semver
@@ -42,14 +44,20 @@ $(SEMVER): $(LOCALBIN)
4244
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
4345
$(GOLANGCI_LINT): $(LOCALBIN)
4446
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
47+
.PHONY: deepcopy-gen
48+
deepcopy-gen: $(DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
49+
$(DEEPCOPY_GEN): $(LOCALBIN)
50+
test -s $(LOCALBIN)/deepcopy-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(DEEPCOPY_GEN_VERSION)
4551

4652
## Update Tools
4753
.PHONY: update-toolbox-tools
4854
update-toolbox-tools:
4955
@rm -f \
5056
$(LOCALBIN)/semver \
51-
$(LOCALBIN)/golangci-lint
57+
$(LOCALBIN)/golangci-lint \
58+
$(LOCALBIN)/deepcopy-gen
5259
toolbox makefile -f $$(pwd)/Makefile \
5360
github.com/bakito/semver \
54-
github.com/golangci/golangci-lint/cmd/golangci-lint
61+
github.com/golangci/golangci-lint/cmd/golangci-lint \
62+
k8s.io/code-generator/cmd/[email protected]/kubernetes/code-generator
5563
## toolbox - end

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/onsi/gomega v1.24.2
1010
github.com/spf13/cobra v1.6.1
1111
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
12+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
1213
gopkg.in/yaml.v3 v3.0.1
1314
)
1415

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
2121
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2222
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
2323
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
24+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
25+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
2426
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
2527
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
2628
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=

pkg/github/client.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
const (
14-
latestURLPattern = "https://api.github.com/repos/%s/releases/latest"
14+
latestReleaseURLPattern = "https://api.github.com/repos/%s/releases/latest"
15+
latestTagURLPattern = "https://api.github.com/repos/%s/tags"
1516
)
1617

1718
func LatestRelease(client *resty.Client, repo string, quiet bool) (*types.GithubRelease, error) {
@@ -26,16 +27,37 @@ func LatestRelease(client *resty.Client, repo string, quiet bool) (*types.Github
2627
}
2728
ghc = ghc.SetAuthToken(t)
2829
}
29-
_, err := ghc.Get(latestURL(repo))
30+
_, err := ghc.Get(latestReleaseURL(repo))
3031
if err != nil {
3132
return nil, http.CheckError(err)
3233
}
34+
35+
if ghr.TagName == "" {
36+
ght := &types.GithubTags{}
37+
ghc.SetResult(ght)
38+
_, err := ghc.Get(latestTagURL(repo))
39+
if err != nil {
40+
return nil, http.CheckError(err)
41+
}
42+
43+
if latest := ght.GetLatest(); latest != nil {
44+
ghr.TagName = latest.Name
45+
}
46+
}
47+
3348
return ghr, nil
3449
}
3550

36-
func latestURL(repo string) string {
51+
func latestReleaseURL(repo string) string {
52+
if repo != "" {
53+
return fmt.Sprintf(latestReleaseURLPattern, repo)
54+
}
55+
return ""
56+
}
57+
58+
func latestTagURL(repo string) string {
3759
if repo != "" {
38-
return fmt.Sprintf(latestURLPattern, repo)
60+
return fmt.Sprintf(latestTagURLPattern, repo)
3961
}
4062
return ""
4163
}

pkg/types/types_github.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package types
22

33
import (
4+
"sort"
5+
"strings"
46
"time"
7+
8+
"golang.org/x/mod/semver"
59
)
610

711
type GithubRelease struct {
@@ -45,3 +49,29 @@ type Asset struct {
4549
UpdatedAt time.Time `json:"updated_at"`
4650
BrowserDownloadURL string `json:"browser_download_url"`
4751
}
52+
53+
type GithubTags []GithubTag
54+
55+
func (ght GithubTags) GetLatest() *GithubTag {
56+
sort.Slice(ght, func(i, j int) bool {
57+
return semver.Compare(ght[i].Name, ght[j].Name) > 0
58+
})
59+
60+
for _, tag := range ght {
61+
if semver.IsValid(tag.Name) && !strings.Contains(tag.Name, "-") {
62+
return &tag
63+
}
64+
}
65+
return nil
66+
}
67+
68+
type GithubTag struct {
69+
Name string `json:"name"`
70+
ZipballURL string `json:"zipball_url"`
71+
TarballURL string `json:"tarball_url"`
72+
Commit struct {
73+
Sha string `json:"sha"`
74+
URL string `json:"url"`
75+
} `json:"commit"`
76+
NodeID string `json:"node_id"`
77+
}

0 commit comments

Comments
 (0)