Skip to content

Commit

Permalink
feat: add update checking
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed May 30, 2021
1 parent 9b8183c commit 19e035d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/pterm/pterm v0.12.17
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/tidwall/gjson v1.8.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
Expand Down
43 changes: 43 additions & 0 deletions pcli.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package pcli

import (
"io/ioutil"
"net/http"
"runtime"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tidwall/gjson"
)

var rootCmd *cobra.Command

// EnableUpdateChecking automatically checks if a new version of your application is pushed, and notifies the user.
var EnableUpdateChecking = true

// SetRootCmd sets your rootCmd.
func SetRootCmd(cmd *cobra.Command) {
rootCmd = cmd
Expand All @@ -28,6 +35,42 @@ func Setup() {
rootCmd.SetVersionTemplate(VersionTemplate())
rootCmd.SetOut(PcliOut())
rootCmd.SetErr(Err())
CheckForUpdates()
}

// CheckForUpdates checks if a new version of your application is pushed, and notifies the user, if EnableUpdateChecking is true.
func CheckForUpdates() error {
if EnableUpdateChecking {
resp, err := http.Get(pterm.Sprintf("https://api.github.com/repos/%s/releases/latest", getRepoPath()))
if err != nil {
return err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

tagName := gjson.Get(string(body), "tag_name").String()

if rootCmd.Version != tagName {
format := "A new version of %s is availble (%s)!\n"
format += "You can install the new version with: "

switch runtime.GOOS {
case "windows":
format += pterm.Magenta(pterm.Sprintf(`iwr instl.sh/%s/windows | iex`, getRepoPath()))
case "darwin":
format += pterm.Magenta(pterm.Sprintf(`curl -sSL instl.sh/%s/macos | sudo bash`, getRepoPath()))
default:
format += pterm.Magenta(pterm.Sprintf(`curl -sSL instl.sh/%s/linux | sudo bash`, getRepoPath()))
}
pterm.Info.Printfln(format, rootCmd.Name(), pterm.Magenta(tagName))
}
}

return nil
}

// GetCiCommand returns a custom crafted CI command. This must be used when using https://github.com/pterm/cli-template.
Expand Down
11 changes: 11 additions & 0 deletions pterm-ci-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ func detectOriginURL() (url string) {
return
}

func getRepo() (username, reponame string) {
projectParts := strings.Split(strings.TrimPrefix(detectOriginURL(), "https://github.com/"), "/")

return projectParts[0], projectParts[1]
}

func getRepoPath() string {
username, reponame := getRepo()
return pterm.Sprintf("%s/%s", username, reponame)
}

func walkOverExt(path, exts string, f func(path string)) {
_ = filepath.Walk(getPathTo(path), func(path string, info fs.FileInfo, err error) error {
for _, ext := range strings.Split(exts, ",") {
Expand Down

0 comments on commit 19e035d

Please sign in to comment.