From d35e75ef4b4af40ccc3b86764b482ede6e2b47e2 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 30 Apr 2015 18:58:15 -0700 Subject: [PATCH] Cmd update now has a standard URL --- Makefile | 2 +- cmd-access.go | 1 - cmd-update.go | 76 ++++++++++++++++----------------------------------- notifier.go | 15 ++++------ 4 files changed, 31 insertions(+), 63 deletions(-) diff --git a/Makefile b/Makefile index 89e7355d..70fda5e2 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,7 @@ env: install: test-all @echo "Installing mc:" - @godep go install -a -ldflags "-X main.BuildDate `date '+%FT%T.%N%:z'`" github.com/minio-io/mc + @godep go install -a -ldflags "-X main.BuildDate `date --universal '+%FT%T.%N%:z'`" github.com/minio-io/mc @mkdir -p $(HOME)/.mc clean: diff --git a/cmd-access.go b/cmd-access.go index ff2f1f87..19204abc 100644 --- a/cmd-access.go +++ b/cmd-access.go @@ -39,7 +39,6 @@ func runAccessCmd(ctx *cli.Context) { log.Debug.Println(iodine.New(err, nil)) console.Fatalf("loading config file failed with following reason: [%s]\n", iodine.ToError(err)) } - targetURLConfigMap := make(map[string]*hostConfig) targetURLs, err := getExpandedURLs(ctx.Args(), config.Aliases) if err != nil { diff --git a/cmd-update.go b/cmd-update.go index 2e8b195d..451242c8 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -17,14 +17,9 @@ package main import ( - "errors" - "fmt" "runtime" "time" - "encoding/json" - "net/http" - "github.com/minio-io/cli" "github.com/minio-io/mc/pkg/console" "github.com/minio-io/minio/pkg/iodine" @@ -32,24 +27,27 @@ import ( ) const ( - mcUpdateURL = "http://dl.minio.io:9000/binaries/" + mcUpdateURL = "http://dl.minio.io:9000/updates/2015/Apr" + "/mc" + "." + runtime.GOOS + "." + runtime.GOARCH ) -type updateResults struct { - Version uint // this is config version - LatestBuild string - Signature string -} - -// getRequest - -func getRequest(url string) (*http.Request, error) { - req, err := http.NewRequest("GET", url, nil) +func doUpdateCheck(methods mcClientMethods, config *hostConfig) (string, error) { + clnt, err := methods.getNewClient(mcUpdateURL, config, globalDebugFlag) if err != nil { - msg := fmt.Sprintf("s3 client; invalid URL: %v", err) - return nil, errors.New(msg) + return "Unable to create client: " + mcUpdateURL, iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) } - req.Header.Set("User-Agent", mcUserAgent) - return req, nil + latest, err := clnt.Stat() + if err != nil { + return "Unable to create client: " + mcUpdateURL, iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) + } + current, _ := time.Parse(time.RFC3339Nano, BuildDate) + if current.IsZero() { + return "BuildDate is zero, must be a wrong build exiting..", iodine.New(err, map[string]string{"failedURL": mcUpdateURL}) + } + if latest.Time.After(current) { + printUpdateNotify("new", "old") + } + return "", nil + } // runUpdateCmd - @@ -60,43 +58,17 @@ func runUpdateCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".") } - mcUpdateBinaryURL := mcUpdateURL + runtime.GOOS + "/mc" + hostConfig, err := getHostConfig(mcUpdateURL) + if err != nil { + log.Debug.Println(iodine.New(err, nil)) + console.Fatalf("Unable to read configuration for host [%s]. Reason: [%s].\n", mcUpdateURL, iodine.ToError(err)) + } switch ctx.Args().First() { case "check": - req, err := getRequest(mcUpdateBinaryURL) + msg, err := doUpdateCheck(mcClientMethods{}, hostConfig) if err != nil { log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Unable to update:", mcUpdateBinaryURL) + console.Fatalf(msg) } - res, err := http.DefaultTransport.RoundTrip(req) - if err != nil { - log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Unable to retrieve:", mcUpdateBinaryURL) - } - if res.StatusCode != http.StatusOK { - msg := fmt.Sprint("Received invalid HTTP status: ", res.StatusCode) - log.Debug.Println(iodine.New(errors.New(msg), nil)) - console.Fatalln(msg) - } - results := updateResults{} - err = json.NewDecoder(res.Body).Decode(&results) - if err != nil { - log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Unable to parse JSON:", mcUpdateURL) - } - latest, err := time.Parse(time.RFC3339Nano, results.LatestBuild) - if err != nil { - log.Debug.Println(iodine.New(err, nil)) - console.Fatalln("Unable to parse update time:", results.LatestBuild) - } - current, _ := time.Parse(time.RFC3339Nano, BuildDate) - if current.IsZero() { - console.Fatalln("BuildDate is zero, must be a wrong build exiting..") - } - if latest.After(current) { - printUpdateNotify("new", "old") - } - case "install": - console.Fatalln("Functionality not implemented yet") } } diff --git a/notifier.go b/notifier.go index 18792359..350b33a1 100644 --- a/notifier.go +++ b/notifier.go @@ -24,9 +24,9 @@ import ( "github.com/fatih/color" ) -// go get update string +// update string const ( - updateString = "go get -u github.com/minio-io/mc" + updateString = "mc cp " + mcUpdateURL + " ${HOME}/bin/mc" ) // intMax - return maximum value for any given integer @@ -42,20 +42,17 @@ func printUpdateNotify(latestVersion, currentVersion string) { // TODO - make this configurable // // initialize coloring - green := color.New(color.FgGreen) - boldGreen := green.Add(color.Bold).SprintFunc() blue := color.New(color.FgBlue).SprintFunc() - blueFaint := color.New(color.FgBlue, color.BlinkSlow).SprintfFunc() yellow := color.New(color.FgYellow).SprintfFunc() // calculate length without color coding, due to ANSI color characters padded to actual // string the final length is wrong than the original string length - line1Length := len(fmt.Sprintf(" Update available: %s (current: %s)", latestVersion, currentVersion)) - line2Length := len(fmt.Sprintf(" Run %s to update. ", updateString)) + line1Length := len(fmt.Sprintf(" Update available: ")) + line2Length := len(fmt.Sprintf(" Run \"%s\" to update. ", updateString)) // populate lines with color coding - line1InColor := fmt.Sprintf(" Update available: %s (current: %s)", boldGreen(latestVersion), blueFaint(currentVersion)) - line2InColor := fmt.Sprintf(" Run %s to update. ", blue(updateString)) + line1InColor := fmt.Sprintf(" Update available: ") + line2InColor := fmt.Sprintf(" Run \"%s\" to update. ", blue(updateString)) // calculate the rectangular box size maxContentWidth := intMax(line1Length, line2Length)