Skip to content

Commit

Permalink
Merge pull request #12 from jbowes/use-my-semver
Browse files Browse the repository at this point in the history
Switch to my own semver lib
  • Loading branch information
jbowes committed Sep 11, 2021
2 parents 0be2042 + 48b0952 commit fc29d68
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -2,4 +2,4 @@ module github.com/jbowes/whatsnew

go 1.16

require golang.org/x/mod v0.5.0
require github.com/jbowes/semver v0.1.0
15 changes: 2 additions & 13 deletions go.sum
@@ -1,13 +1,2 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.5.0 h1:UG21uOlmZabA4fW5i7ZX6bjw1xELEGg/ZLgZq9auk/Q=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/jbowes/semver v0.1.0 h1:BJDfsubBdXttHLJ+Toccdx75xccsp+yffx89OT6IaZs=
github.com/jbowes/semver v0.1.0/go.mod h1:cW7tPS9OrZDbYl1wdAx9k7KY4OQTL9BAJsOkAU4nR5Q=
68 changes: 36 additions & 32 deletions whatsnew.go
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"
"time"

"golang.org/x/mod/semver"
"github.com/jbowes/semver"

"github.com/jbowes/whatsnew/impl"
)
Expand Down Expand Up @@ -163,17 +163,22 @@ func doWork(ctx context.Context, opts *Options) (string, error) {
if err != nil {
i = &impl.Info{}
}
iHasV, iVer, _ := parseV(i.Version)

now := time.Now()

nextVer := opts.Version
optHasV, optVer, _ := parseV(opts.Version)
nextVer := optVer
nextHasV := optHasV
if now.Sub(i.CheckTime) < opts.Frequency {
nextVer = i.Version
nextVer = iVer
nextHasV = iHasV
} else {
rels, etag, err := opts.Releaser.Get(ctx, i.Etag)
if err != nil {
// If we error, fall back to possibly using the value from the store
nextVer = i.Version
nextVer = iVer
nextHasV = iHasV
} else if len(rels) == 0 {
// Cached result. refresh the checktime and store.
_ = opts.Cacher.Set(ctx, &impl.Info{
Expand All @@ -182,57 +187,56 @@ func doWork(ctx context.Context, opts *Options) (string, error) {
Version: i.Version,
})

nextVer = i.Version
nextVer = iVer
nextHasV = iHasV
} else {
// find the biggest non-prerelease version in releases.
// TODO: could look at more than the first page. would only matter
// for concurrent patch releases etc.
var newVer string
var newVer *semver.Version
newHasV := ""
for _, rel := range rels {
hv, pv, err := parseV(rel.TagName)
switch {
case err != nil: // not a valid semver tag
case rel.Draft:
case !isValid(rel.TagName):
case rel.Prerelease || isPrerelease(rel.TagName):
case cmp(newVer, rel.TagName) < 0:
newVer = rel.TagName
case rel.Prerelease || pv.Prerelease() != "":
case newVer.Compare(pv) < 0:
newVer = pv
newHasV = hv
}
}

if cmp(nextVer, newVer) < 1 {
nextVer = newVer
}

// TODO: make sure newVer is set
_ = opts.Cacher.Set(ctx, &impl.Info{
CheckTime: now,
Etag: etag,
Version: newVer, // we store the latest from the remote ignoring whats installed.
Version: newHasV + newVer.String(), // we store the latest from the remote ignoring what's installed.
})

if nextVer.Compare(newVer) < 1 {
nextVer = newVer
nextHasV = newHasV
}
}
}

if cmp(opts.Version, nextVer) >= 0 {
if optVer.Compare(nextVer) >= 0 {
return "", nil
}

return nextVer, nil
return nextHasV + nextVer.String(), nil
}

// cmp is like x/mod/semver except it allows versions to not start with a v.
func cmp(v1, v2 string) int {
v1 = maybeV(v1)
v2 = maybeV(v2)

return semver.Compare(v1, v2)
}

func isValid(v1 string) bool { return semver.IsValid(maybeV(v1)) }
func isPrerelease(v1 string) bool { return semver.Prerelease(maybeV(v1)) != "" }

func maybeV(v string) string {
if v != "" && v[0] != 'v' {
return "v" + v
func parseV(s string) (string, *semver.Version, error) {
// TODO: parsing out the v and holding it isn't great.
hasV := ""
if s != "" && s[0] == 'v' {
hasV = "v"
s = s[1:]
}

return v
v, err := semver.Parse(s)

return hasV, v, err
}
8 changes: 8 additions & 0 deletions whatsnew_test.go
Expand Up @@ -66,6 +66,14 @@ func TestCheck(t *testing.T) {
},
out: "v1.1.1",
},
"no v prefix allowed": {
releases: []impl.Release{
{TagName: "v1.0.1"},
{TagName: "1.1.1"},
{TagName: "1.0.2"},
},
out: "1.1.1",
},
"cacher get err": {
releases: []impl.Release{{TagName: "v1.1.1"}},
cacheErr: errors.New("oops"),
Expand Down

0 comments on commit fc29d68

Please sign in to comment.