2017-08-09 16:12:24 +02:00
package iris
import (
"bufio"
2017-08-10 14:21:42 +02:00
"encoding/json"
2017-08-09 16:12:24 +02:00
"io/ioutil"
2017-08-10 14:21:42 +02:00
"net/url"
2017-08-09 16:12:24 +02:00
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/kataras/golog"
2017-08-10 14:21:42 +02:00
"github.com/kataras/iris/core/netutil"
2017-08-09 16:12:24 +02:00
)
const (
2017-08-10 14:21:42 +02:00
versionURL = "http://iris-go.com/version"
2017-08-09 16:12:24 +02:00
updateCmd = "go get -u -f -v github.com/kataras/iris"
)
var checkVersionOnce = sync . Once { }
// CheckVersion checks for any available updates.
func CheckVersion ( ) {
checkVersionOnce . Do ( func ( ) {
checkVersion ( )
} )
}
2017-08-10 14:21:42 +02:00
type versionInfo struct {
Version string ` json:"version" `
ChangelogURL string ` json:"changelog_url" `
UpdateAvailable bool ` json:"update_available" `
}
2017-08-09 16:12:24 +02:00
2017-08-10 14:21:42 +02:00
func checkVersion ( ) {
2017-08-10 20:34:05 +02:00
client := netutil . Client ( time . Duration ( 20 * time . Second ) )
2017-08-10 14:21:42 +02:00
r , err := client . PostForm ( versionURL , url . Values { "current_version" : { Version } } )
2017-08-09 16:12:24 +02:00
if err != nil {
golog . Debugf ( "%v" , err )
return
}
defer r . Body . Close ( )
if r . StatusCode >= 400 {
return
}
b , err := ioutil . ReadAll ( r . Body )
if len ( b ) == 0 || err != nil {
golog . Debugf ( "%v" , err )
return
}
2017-08-10 14:21:42 +02:00
v := new ( versionInfo )
if err := json . Unmarshal ( b , v ) ; err != nil {
golog . Debugf ( "error while unmarshal the response body: %v" , err )
2017-08-09 16:12:24 +02:00
return
}
2017-08-10 14:21:42 +02:00
if ! v . UpdateAvailable {
2017-08-09 16:12:24 +02:00
return
}
2017-08-10 20:34:05 +02:00
format := "A new version is available online[%s > %s].\n"
2017-08-09 16:12:24 +02:00
2017-08-10 14:21:42 +02:00
if v . ChangelogURL != "" {
format += "Release notes: %s\n"
2017-08-09 16:12:24 +02:00
}
2017-08-10 14:21:42 +02:00
format += "Update now?[%s]: "
2017-08-09 16:12:24 +02:00
2017-08-10 14:21:42 +02:00
// currentVersion.LessThan(latestVersion)
updaterYesInput := [ ... ] string { "y" , "yes" }
2017-08-09 16:12:24 +02:00
2017-08-10 14:21:42 +02:00
golog . Warnf ( format , v . Version , Version ,
v . ChangelogURL ,
2017-08-09 16:12:24 +02:00
updaterYesInput [ 0 ] + "/n" )
silent := false
sc := bufio . NewScanner ( os . Stdin )
shouldUpdate := silent
if ! silent {
if sc . Scan ( ) {
inputText := sc . Text ( )
2017-08-10 14:21:42 +02:00
2017-08-09 16:12:24 +02:00
for _ , s := range updaterYesInput {
if inputText == s {
shouldUpdate = true
}
}
}
}
if shouldUpdate {
goget := strings . Split ( updateCmd , " " )
// go get -u github.com/:owner/:repo
cmd := exec . Command ( goget [ 0 ] , goget [ 1 : ] ... )
cmd . Stdout = os . Stdout
cmd . Stderr = os . Stdout
if err := cmd . Run ( ) ; err != nil {
2017-08-10 20:34:05 +02:00
golog . Warnf ( "unexpected message while trying to go get,\nif you edited the original source code then you've to remove the whole $GOPATH/src/github.com/kataras folder and execute `go get github.com/kataras/iris` manually\n%v" , err )
2017-08-09 16:12:24 +02:00
return
}
2017-08-10 14:21:42 +02:00
golog . Infof ( "Update process finished.\nManual restart is required to apply the changes.\n" )
2017-08-09 16:12:24 +02:00
}
}