update command rewrite

This commit is contained in:
Anand Babu (AB) Periasamy
2015-11-15 20:54:56 -08:00
parent f2aa4a112d
commit 44f03bede1
+55 -109
View File
@@ -20,7 +20,6 @@ import (
"encoding/json"
"net/http"
"runtime"
"strings"
"time"
"github.com/fatih/color"
@@ -29,31 +28,42 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
// command specific flags.
var (
updateFlagExperimental = cli.BoolFlag{
Name: "experimental, E",
Usage: "Check experimental update.",
}
)
// Check for new software updates.
var updateCmd = cli.Command{
Name: "update",
Usage: "Check for new software updates.",
Usage: "Check for a new software update.",
Action: mainUpdate,
Flags: []cli.Flag{updateFlagExperimental},
CustomHelpTemplate: `Name:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} release
mc {{.Name}} experimental
mc {{.Name}} [FLAGS]
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Check for new official releases
$ mc {{.Name}} release
1. Check for any new official release.
$ mc {{.Name}}
2. Check for new experimental releases
$ mc {{.Name}} experimental
2. Check for any new experimental release.
$ mc {{.Name}} --experimental
`,
}
// update URL endpoints.
const (
mcUpdateURL = "https://dl.minio.io:9000/updates/updates.json"
mcExperimentalURL = "https://dl.minio.io:9000/updates/experimental.json"
mcUpdateStableURL = "https://dl.minio.io:9000/updates/updates.json"
mcUpdateExperimentalURL = "https://dl.minio.io:9000/updates/experimental.json"
)
// mcUpdates container to hold updates json.
@@ -71,18 +81,19 @@ type updateMessage struct {
// String colorized update message.
func (u updateMessage) String() string {
if u.Update {
var msg string
if runtime.GOOS == "windows" {
msg = "mc.exe cp " + u.Download + " .\\mc.exe"
} else {
msg = "mc cp " + u.Download + " ./mc.new; chmod 755 ./mc.new"
}
msg, err := colorizeUpdateMessage(msg)
fatalIf(err.Trace(msg), "Unable to colorize experimental update notification string ‘"+msg+"’.")
return msg
if !u.Update {
return console.Colorize("Update", "You are already running the most recent version of ‘mc’.")
}
return console.Colorize("UpdateMessage", "You are already running the most recent version of ‘mc’.")
var msg string
if runtime.GOOS == "windows" {
msg = "mc.exe cp " + u.Download + " .\\mc.exe"
} else {
msg = "mc cp " + u.Download + " ./mc.new; chmod 755 ./mc.new"
}
msg, err := colorizeUpdateMessage(msg)
fatalIf(err.Trace(msg), "Unable to colorize experimental update notification string ‘"+msg+"’.")
return msg
}
// JSON jsonified update message.
@@ -93,101 +104,23 @@ func (u updateMessage) JSON() string {
return string(updateMessageJSONBytes)
}
// check valid input arguments.
func checkUpdateSyntax(ctx *cli.Context) {
if !ctx.Args().Present() {
cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code
}
arg := strings.TrimSpace(ctx.Args().First())
if arg != "release" && arg != "experimental" {
fatalIf(errInvalidArgument().Trace(arg), "Unrecognized argument provided.")
}
}
// main for update command.
func mainUpdate(ctx *cli.Context) {
// check input arguments
checkUpdateSyntax(ctx)
// Additional command speific theme customization.
console.SetColor("UpdateMessage", color.New(color.FgGreen, color.Bold))
arg := strings.TrimSpace(ctx.Args().First())
switch arg {
case "release":
getReleaseUpdate()
case "experimental":
getExperimentalUpdate()
}
}
// verify new updates for experimentals.
func getExperimentalUpdate() {
clnt, err := url2Client(mcExperimentalURL)
fatalIf(err.Trace(mcExperimentalURL), "Unable to initalize experimental URL.")
data, _, err := clnt.Get(0, 0)
fatalIf(err.Trace(mcExperimentalURL), "Unable to read from experimental URL ‘"+mcExperimentalURL+"’.")
if mcVersion == "UNOFFICIAL.GOGET" {
fatalIf(errDummy().Trace(mcVersion), "Update mechanism is not supported for ‘go get’ based binary builds. Please download official releases from https://minio.io/#mc")
}
current, e := time.Parse(time.RFC3339, mcVersion)
fatalIf(probe.NewError(e), "Unable to parse Version string as time.")
if current.IsZero() {
fatalIf(errDummy().Trace(), "Experimental updates not supported for custom build. Version field is empty. Please download official releases from https://minio.io/#mc")
}
var experimentals mcUpdates
decoder := json.NewDecoder(data)
e = decoder.Decode(&experimentals)
fatalIf(probe.NewError(e), "Unable to decode experimental update notification.")
latest, e := time.Parse(time.RFC3339, experimentals.BuildDate)
if e != nil {
latest, e = time.Parse(http.TimeFormat, experimentals.BuildDate)
fatalIf(probe.NewError(e), "Unable to parse BuildDate.")
}
if latest.IsZero() {
fatalIf(errDummy().Trace(), "Unable to validate any experimental update available at this time. Please open an issue at https://github.com/minio/mc/issues")
}
mcExperimentalURLParse := clnt.GetURL()
downloadURL := (mcExperimentalURLParse.Scheme +
string(mcExperimentalURLParse.SchemeSeparator) +
mcExperimentalURLParse.Host + string(mcExperimentalURLParse.Separator) +
experimentals.Platforms[runtime.GOOS+"-"+runtime.GOARCH])
updateMsg := updateMessage{
Download: downloadURL,
Version: mcVersion,
}
if latest.After(current) {
updateMsg.Update = true
}
printMsg(updateMsg)
}
// verify updates for releases.
func getReleaseUpdate() {
clnt, err := url2Client(mcUpdateURL)
fatalIf(err.Trace(mcUpdateURL), "Unable to initalize update URL.")
func getReleaseUpdate(updateURL string) {
clnt, err := url2Client(updateURL)
fatalIf(err.Trace(updateURL), "Unable to initalize update URL.")
data, _, err := clnt.Get(0, 0)
fatalIf(err.Trace(mcUpdateURL), "Unable to read from update URL ‘"+mcUpdateURL+"’.")
fatalIf(err.Trace(updateURL), "Unable to read from update URL ‘"+updateURL+"’.")
if mcVersion == "UNOFFICIAL.GOGET" {
fatalIf(errDummy().Trace(mcVersion), "Update mechanism is not supported for ‘go get’ based binary builds. Please download official releases from https://minio.io/#mc")
}
current, e := time.Parse(time.RFC3339, mcVersion)
fatalIf(probe.NewError(e), "Unable to parse Version string as time.")
fatalIf(probe.NewError(e), "Unable to parse version string as time.")
if current.IsZero() {
fatalIf(errDummy().Trace(), "Updates not supported for custom build. Version field is empty. Please download official releases from https://minio.io/#mc")
fatalIf(errDummy().Trace(), "Updates not supported for custom builds. Version field is empty. Please download official releases from https://minio.io/#mc")
}
var updates mcUpdates
@@ -205,10 +138,10 @@ func getReleaseUpdate() {
fatalIf(errDummy().Trace(), "Unable to validate any update available at this time. Please open an issue at https://github.com/minio/mc/issues")
}
mcUpdateURLParse := clnt.GetURL()
downloadURL := mcUpdateURLParse.Scheme +
string(mcUpdateURLParse.SchemeSeparator) +
mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) +
updateURLParse := clnt.GetURL()
downloadURL := updateURLParse.Scheme +
string(updateURLParse.SchemeSeparator) +
updateURLParse.Host + string(updateURLParse.Separator) +
updates.Platforms[runtime.GOOS+"-"+runtime.GOARCH]
updateMsg := updateMessage{
@@ -220,3 +153,16 @@ func getReleaseUpdate() {
}
printMsg(updateMsg)
}
// main entry point for update command.
func mainUpdate(ctx *cli.Context) {
// Additional command speific theme customization.
console.SetColor("Update", color.New(color.FgGreen, color.Bold))
// Check for update.
if ctx.Bool("experimental") {
getReleaseUpdate(mcUpdateExperimentalURL)
} else {
getReleaseUpdate(mcUpdateStableURL)
}
}