Cmd update now has a standard URL

This commit is contained in:
Harshavardhana
2015-05-01 00:23:15 -07:00
parent 0de32c8453
commit d35e75ef4b
4 changed files with 31 additions and 63 deletions
+1 -1
View File
@@ -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:
-1
View File
@@ -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 {
+24 -52
View File
@@ -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")
}
}
+6 -9
View File
@@ -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)