diff --git a/cmd-update.go b/cmd-update.go index 263fa2f6..92c1565e 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -17,18 +17,27 @@ package main import ( + "encoding/json" "errors" "fmt" + "net/http" "runtime" "time" "github.com/minio/cli" + "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/iodine" ) +// Updates container to hold updates json +type Updates struct { + BuildDate string + Platforms map[string]string +} + const ( - mcUpdateURL = "http://dl.minio.io:9000/updates/2015/Jun/" + "mc" + "." + runtime.GOOS + "." + runtime.GOARCH + mcUpdateURL = "http://dl.minio.io:9000/updates/updates.json" ) // Help message. @@ -50,20 +59,37 @@ EXAMPLES: } func doUpdateCheck(config *hostConfig) (string, error) { + mcUpdateURLParse, err := client.Parse(mcUpdateURL) + if err != nil { + return "Unable to parse URL: " + mcUpdateURL, iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) + } clnt, err := getNewClient(mcUpdateURL, config) if err != nil { return "Unable to create client: " + mcUpdateURL, iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) } - latest, err := clnt.Stat() + data, _, err := clnt.GetObject(0, 0) if err != nil { - return "No new update available at this time", nil + return "Unable to read: " + mcUpdateURL, iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) } current, _ := time.Parse(time.RFC3339Nano, Version) if current.IsZero() { - return "Version is empty, must be a custom build cannot update. Please download releases from http://dl.minio.io:9000 for proper updates", nil + message := `Version is empty, must be a custom build cannot update. Please download releases from +http://dl.minio.io:9000 for continuous updates` + return message, nil } - if latest.Time.After(current) { - printUpdateNotify("new", "old") + var updates Updates + decoder := json.NewDecoder(data) + err = decoder.Decode(&updates) + if err != nil { + return "Unable to parse update fields", iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) + } + latest, _ := time.Parse(http.TimeFormat, updates.BuildDate) + if latest.IsZero() { + return "No update available at this time", nil + } + if latest.After(current) { + updateString := "mc cp " + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " ${HOME}/bin/mc" + printUpdateNotify(updateString, "new", "old") return "", nil } return "You are already running the most recent version of ‘mc’", nil diff --git a/make.go b/make.go index 42da0fbf..be4ef38d 100644 --- a/make.go +++ b/make.go @@ -40,7 +40,10 @@ func writeVersion(version Version) error { package main -import "time" +import ( + "net/http" + "time" +) // Version autogenerated var Version = {{if .Date}}"{{.Date}}"{{else}}""{{end}} @@ -51,7 +54,7 @@ func getVersion() string { if t.IsZero() { return "" } - return t.Format(time.RFC1123) + return t.Format(http.TimeFormat) } ` t := template.Must(template.New("version").Parse(versionTemplate)) diff --git a/notifier.go b/notifier.go index 62a1fb01..f6702cea 100644 --- a/notifier.go +++ b/notifier.go @@ -25,13 +25,8 @@ import ( "github.com/fatih/color" ) -// update string -const ( - updateString = "mc cp " + mcUpdateURL + " ${HOME}/bin/mc" -) - // printUpdateNotify - inspired from Yeoman project npm package https://github.com/yeoman/update-notifier -func printUpdateNotify(latestVersion, currentVersion string) { +func printUpdateNotify(updateString, latestVersion, currentVersion string) { // TODO - make this configurable // // initialize coloring diff --git a/version.go b/version.go index a58c1946..11991ea7 100644 --- a/version.go +++ b/version.go @@ -3,7 +3,10 @@ package main -import "time" +import ( + "net/http" + "time" +) // Version autogenerated var Version = "2015-06-07T02:49:54.701568725Z" @@ -14,5 +17,5 @@ func getVersion() string { if t.IsZero() { return "" } - return t.Format(time.RFC1123) + return t.Format(http.TimeFormat) }