Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
970d8c654f | |||
a5b3719d33 |
10
main.go
10
main.go
@ -22,6 +22,16 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
outOfDate, err := needUpdate()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if outOfDate {
|
||||||
|
fmt.Println("There is a new version of epochcli, please update before running")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
helpFlag bool
|
helpFlag bool
|
||||||
updateOnlyFlag bool
|
updateOnlyFlag bool
|
||||||
|
38
version.go
Normal file
38
version.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
version = "1.0.4"
|
||||||
|
versionUrl = "https://git.burkey.co/eburk/epochcli/raw/branch/master/version.go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func needUpdate() (bool, error) {
|
||||||
|
resp, err := http.Get(versionUrl)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("unable to get version file: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return false, fmt.Errorf("failed to download update, status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("unable to read response body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`const version = "([\d.]+)"`)
|
||||||
|
ver := re.FindStringSubmatch(string(b))
|
||||||
|
if len(ver) < 2 || ver[1] == "" {
|
||||||
|
return false, fmt.Errorf("unable to find version in response")
|
||||||
|
}
|
||||||
|
|
||||||
|
return ver[1] != version, nil
|
||||||
|
}
|
Reference in New Issue
Block a user