Skip to content

Commit

Permalink
Add upgrade check, tweak goreleaser settings
Browse files Browse the repository at this point in the history
  • Loading branch information
twavv committed Mar 9, 2021
1 parent 210ba7c commit 7c48284
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 5 deletions.
11 changes: 6 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ builds:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
ldflags:
- "-X 'github.com/mynerva-io/author-cli/internal/version.Version={{ .Version }}'"
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
darwin: macos
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
Binary file added author-cli
Binary file not shown.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/mynerva-io/author-cli/cmd/auth"
"github.com/mynerva-io/author-cli/cmd/codex"
"github.com/mynerva-io/author-cli/internal/config"
"github.com/mynerva-io/author-cli/internal/version"
"github.com/spf13/cobra"
"os"

Expand All @@ -26,6 +27,7 @@ var rootCmd = &cobra.Command{
if err := setUpLog(verbose); err != nil {
panic(err)
}
version.CheckVersionAndPrintUpgradeNotice()
return nil
},
}
Expand All @@ -45,6 +47,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringVar(&config.MynervaApiHost, "api-host", config.MynervaApiHost, "Mynerva API host")

rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(auth.Cmd)
rootCmd.AddCommand(codex.Cmd)
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"fmt"
"github.com/mynerva-io/author-cli/internal/version"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "print version information",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("current version: %s\n", version.Version)
return nil
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/mynerva-io/author-cli
go 1.15

require (
github.com/coreos/go-semver v0.3.0
github.com/fatih/color v1.7.0
github.com/google/martian v2.1.0+incompatible
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
Expand Down
82 changes: 82 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package version

import (
"context"
"encoding/json"
"fmt"
"github.com/coreos/go-semver/semver"
"github.com/fatih/color"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)

var Version string

const upgradeUrl = "https://github.com/mynerva-io/author-cli/blob/main/docs/install.md#upgrade"

// TODO:
// We should cache this result for a while (a day?) so that we don't hit the GitHub API
// for every single invocation of the CLI
func getLatestVersion() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(
ctx, "GET", "https://api.github.com/repos/mynerva-io/author-cli/releases/latest", nil)
if err != nil {
return "", errors.Wrap(err, "failed to create HTTP request")
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", errors.Wrap(err, "failed to query GitHub releases endpoint")
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "failed to read response body")
}
var release gitHubReleaseResponse
if err := json.Unmarshal(data, &release); err != nil {
return "", errors.Wrap(err, "failed to unmarshal GitHub releases endpoint data")
}
return release.Name, nil
}

func CheckVersionAndPrintUpgradeNotice() {
if Version == "<development>" {
log.Debug("not checking for version upgrade in development")
return
}
latestVersionString, err := getLatestVersion()
if err != nil {
log.Warnf("error occurred while trying to determine the latest version: %v", err)
return
}
if latestVersionString == "" {
log.Warnf("failed to determine latest version")
}

// We need to trim the "v" prefix so that semver doesn't choke
current := *semver.New(strings.TrimLeft(Version, "v"))
latest := *semver.New(strings.TrimLeft(latestVersionString, "v"))
if current.LessThan(latest) {
mag := color.New(color.Bold, color.FgMagenta)
_, _ = fmt.Fprint(
os.Stderr,
mag.Sprint(" >> A new version of mynerva-author is available: "),
color.RedString("%s", current),
mag.Sprintf("%s", " => "),
color.GreenString("%s", latest),
mag.Sprintf("\n >> %s\n", upgradeUrl),
)
} else {
log.Debugf("mynerva-author is up to date: current=%s, latest=%s", current, latest)
}
}

type gitHubReleaseResponse struct {
Name string `json:"name"`
}

0 comments on commit 7c48284

Please sign in to comment.