This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
abdfnx/resto
package, build the version checker
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"net/http" | ||
"io/ioutil" | ||
|
||
"github.com/tidwall/gjson" | ||
"github.com/briandowns/spinner" | ||
httpClient "github.com/abdfnx/resto/client" | ||
) | ||
|
||
func GetLatest() string { | ||
url := "https://api.github.com/repos/abdfnx/gomo/releases/latest" | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
|
||
if err != nil { | ||
fmt.Errorf("Error creating request: %s", err.Error()) | ||
} | ||
|
||
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) | ||
s.Suffix = " 🔍 Checking for updates..." | ||
s.Start() | ||
|
||
client := httpClient.HttpClient() | ||
res, err := client.Do(req) | ||
|
||
if err != nil { | ||
fmt.Printf("Error sending request: %s", err.Error()) | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
b, err := ioutil.ReadAll(res.Body) | ||
|
||
if err != nil { | ||
fmt.Printf("Error reading response: %s", err.Error()) | ||
} | ||
|
||
body := string(b) | ||
|
||
tag_name := gjson.Get(body, "tag_name") | ||
|
||
latestVersion := tag_name.String() | ||
|
||
s.Stop() | ||
|
||
return latestVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package checker | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mgutz/ansi" | ||
"github.com/abdfnx/looker" | ||
"github.com/abdfnx/gomo/api" | ||
"github.com/abdfnx/gomo/cmd/factory" | ||
) | ||
|
||
func Check(buildVersion string) { | ||
cmdFactory := factory.New() | ||
stderr := cmdFactory.IOStreams.ErrOut | ||
|
||
latestVersion := api.GetLatest() | ||
isFromHomebrewTap := isUnderHomebrew() | ||
isFromUsrBinDir := isUnderUsr() | ||
isFromAppData := isUnderAppData() | ||
|
||
var command = func() string { | ||
if isFromHomebrewTap { | ||
return "brew upgrade gomo" | ||
} else if isFromUsrBinDir { | ||
return "curl -fsSL https://bit.ly/gomo-cli | bash" | ||
} else if isFromAppData { | ||
return "iwr -useb https://bit.ly/gomo-win | iex" | ||
} | ||
|
||
return "" | ||
} | ||
|
||
if buildVersion != latestVersion { | ||
fmt.Fprintf(stderr, "%s %s → %s\n", | ||
ansi.Color("There's a new version of ", "yellow") + ansi.Color("gomo", "cyan") + ansi.Color(" is avalaible:", "yellow"), | ||
ansi.Color(buildVersion, "cyan"), | ||
ansi.Color(latestVersion, "cyan")) | ||
|
||
if command() != "" { | ||
fmt.Fprintf(stderr, ansi.Color("To upgrade, run: %s\n", "yellow"), ansi.Color(command(), "black:white")) | ||
} | ||
} | ||
} | ||
|
||
var gomoExe, _ = looker.LookPath("gomo") | ||
|
||
func isUnderHomebrew() bool { | ||
return strings.Contains(gomoExe, "brew") | ||
} | ||
|
||
func isUnderUsr() bool { | ||
return strings.Contains(gomoExe, "usr") | ||
} | ||
|
||
func isUnderAppData() bool { | ||
return strings.Contains(gomoExe, "AppData") | ||
} |