Merge pull request #1046 from harshavardhana/update-json-error-container

Updates now handle release and experimental updates
This commit is contained in:
Harshavardhana
2015-08-25 00:34:56 -07:00
6 changed files with 182 additions and 59 deletions
+4 -8
View File
@@ -64,17 +64,14 @@ EXAMPLES:
// AccessMessage is container for access command on bucket success and failure messages
type AccessMessage struct {
Status string `json:"status"`
Bucket string `json:"bucket"`
Acl string `json:"acl"`
Error *probe.Error `json:"error,omitempty"`
Status string `json:"status"`
Bucket string `json:"bucket"`
Acl string `json:"acl"`
}
func (s AccessMessage) String() string {
if !globalJSONFlag {
if s.Status == "success" {
return console.Colorize("Access", "Set access permission ‘"+s.Acl+"’ updated successfully for ‘"+s.Bucket+"’")
}
return console.Colorize("Access", "Set access permission ‘"+s.Acl+"’ updated successfully for ‘"+s.Bucket+"’")
}
accessJSONBytes, err := json.Marshal(s)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.")
@@ -121,7 +118,6 @@ func mainAccess(ctx *cli.Context) {
Status: "success",
Bucket: targetURL,
Acl: acl.String(),
Error: nil,
})
}
}
+10 -2
View File
@@ -49,7 +49,11 @@ func fatalIf(err *probe.Error, msg string) {
if globalDebugFlag {
errorMessage.CallTrace = err.CallTrace
}
json, err := json.Marshal(errorMessage)
json, err := json.Marshal(struct {
Error ErrorMessage `json:"error"`
}{
Error: errorMessage,
})
if err != nil {
console.Fatalln(probe.NewError(err))
}
@@ -77,7 +81,11 @@ func errorIf(err *probe.Error, msg string) {
if globalDebugFlag {
errorMessage.CallTrace = err.CallTrace
}
json, err := json.Marshal(errorMessage)
json, err := json.Marshal(struct {
Error ErrorMessage `json:"error"`
}{
Error: errorMessage,
})
if err != nil {
console.Fatalln(probe.NewError(err))
}
+3 -7
View File
@@ -58,16 +58,13 @@ EXAMPLES:
// MakeBucketMessage is container for make bucket success and failure messages
type MakeBucketMessage struct {
Status string `json:"status"`
Bucket string `json:"bucket"`
Error *probe.Error `json:"error,omitempty"`
Status string `json:"status"`
Bucket string `json:"bucket"`
}
func (s MakeBucketMessage) String() string {
if !globalJSONFlag {
if s.Status == "success" {
return console.Colorize("MakeBucket", "Bucket created successfully ‘"+s.Bucket+"’")
}
return console.Colorize("MakeBucket", "Bucket created successfully ‘"+s.Bucket+"’")
}
makeBucketJSONBytes, err := json.Marshal(s)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.")
@@ -103,7 +100,6 @@ func mainMakeBucket(ctx *cli.Context) {
console.Println(MakeBucketMessage{
Status: "success",
Bucket: targetURL,
Error: nil,
})
}
}
+5 -4
View File
@@ -27,13 +27,13 @@ import (
"github.com/olekukonko/ts"
)
// printUpdateNotify - inspired from Yeoman project npm package https://github.com/yeoman/update-notifier
func printUpdateNotify(updateString, latestVersion, currentVersion string) (string, *probe.Error) {
// colorizeUpdateMessage - inspired from Yeoman project npm package https://github.com/yeoman/update-notifier
func colorizeUpdateMessage(updateString string) (string, *probe.Error) {
// TODO - make this configurable
//
// initialize coloring
blue := color.New(color.FgBlue).SprintFunc()
yellow := color.New(color.FgYellow).SprintfFunc()
blue := color.New(color.FgBlue, color.Bold).SprintFunc()
yellow := color.New(color.FgYellow, color.Bold).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
@@ -55,6 +55,7 @@ func printUpdateNotify(updateString, latestVersion, currentVersion string) (stri
if err != nil {
return "", probe.NewError(err)
}
var message string
switch {
case len(line2Str) > terminal.Col():
+33 -8
View File
@@ -17,6 +17,7 @@
package main
import (
"encoding/json"
"os"
"sort"
"strings"
@@ -82,12 +83,33 @@ func listSessions() *probe.Error {
return nil
}
// ClearSessionMessage container for clearing session messages
type ClearSessionMessage struct {
Status string `json:"success"`
SessionID string `json:"session-id"`
}
func (c ClearSessionMessage) String() string {
if !globalJSONFlag {
return console.Colorize("ClearSession", "Session ‘"+c.SessionID+"’ cleared successfully")
}
clearSessionJSONBytes, err := json.Marshal(c)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.")
return string(clearSessionJSONBytes)
}
func clearSession(sid string) {
if sid == "all" {
for _, sid := range getSessionIDs() {
session, err := loadSessionV2(sid)
fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.")
fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.")
console.Println(ClearSessionMessage{
Status: "success",
SessionID: sid,
})
}
return
}
@@ -101,6 +123,10 @@ func clearSession(sid string) {
if session != nil {
fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.")
console.Println(ClearSessionMessage{
Status: "success",
SessionID: sid,
})
}
}
@@ -128,13 +154,11 @@ func checkSessionSyntax(ctx *cli.Context) {
fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.")
}
case "clear":
if len(ctx.Args()) == 2 {
if strings.TrimSpace(ctx.Args().Tail().First()) == "" {
fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.")
}
if strings.TrimSpace(ctx.Args().Tail().First()) == "" {
fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.")
}
default:
fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.")
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
}
@@ -146,9 +170,10 @@ func mainSession(ctx *cli.Context) {
}
console.SetCustomTheme(map[string]*color.Color{
"Command": color.New(color.FgWhite, color.Bold),
"SessionID": color.New(color.FgYellow, color.Bold),
"SessionTime": color.New(color.FgGreen),
"Command": color.New(color.FgWhite, color.Bold),
"SessionID": color.New(color.FgYellow, color.Bold),
"SessionTime": color.New(color.FgGreen),
"ClearSession": color.New(color.FgGreen, color.Bold),
})
switch strings.TrimSpace(ctx.Args().First()) {
+127 -30
View File
@@ -20,23 +20,15 @@ import (
"encoding/json"
"net/http"
"runtime"
"strings"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe"
)
// Updates container to hold updates json
type Updates struct {
BuildDate string
Platforms map[string]string
}
const (
mcUpdateURL = "https://dl.minio.io:9000/updates/updates.json"
)
// Help message.
var updateCmd = cli.Command{
Name: "update",
@@ -49,17 +41,124 @@ USAGE:
mc {{.Name}}{{if .Flags}} [ARGS...]{{end}}
EXAMPLES:
1. Check for new updates
$ mc update
1. Check for new official releases
$ mc update release
2. Check for new experimental releases
$ mc update experimental
`,
}
// mainUpdate -
func mainUpdate(ctx *cli.Context) {
if ctx.Args().First() == "help" {
// Updates container to hold updates json
type Updates struct {
BuildDate string
Platforms map[string]string
}
// UpdateMessage container to hold update messages
type UpdateMessage struct {
Update bool `json:"update"`
Download string `json:"download-url"`
Version time.Time `json:"version"`
}
func (u UpdateMessage) String() string {
if !globalJSONFlag {
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"
}
msg, err := colorizeUpdateMessage(msg)
fatalIf(err.Trace(msg), "Unable to colorize experimental update notification string ‘"+msg+"’.")
return msg
}
return console.Colorize("UpdateMessage", "You are already running the most recent version of ‘mc’.")
}
updateMessageJSONBytes, err := json.Marshal(u)
fatalIf(probe.NewError(err), "Unable to marshal into JSON.")
return string(updateMessageJSONBytes)
}
const (
mcUpdateURL = "https://dl.minio.io:9000/updates/updates.json"
mcExperimentalURL = "https://dl.minio.io:9000/updates/experimental.json"
)
func checkUpdateSyntax(ctx *cli.Context) {
if ctx.Args().First() == "help" || !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.")
}
}
// mainUpdate -
func mainUpdate(ctx *cli.Context) {
checkUpdateSyntax(ctx)
console.SetCustomTheme(map[string]*color.Color{
"UpdateMessage": color.New(color.FgGreen, color.Bold),
})
arg := strings.TrimSpace(ctx.Args().First())
switch arg {
case "release":
getReleaseUpdate()
case "experimental":
getExperimentalUpdate()
}
}
func getExperimentalUpdate() {
clnt, err := url2Client(mcExperimentalURL)
fatalIf(err.Trace(mcExperimentalURL), "Unable to initalize experimental URL.")
data, _, err := clnt.GetObject(0, 0)
fatalIf(err.Trace(mcExperimentalURL), "Unable to read from experimental URL ‘"+mcExperimentalURL+"’.")
current, e := time.Parse(time.RFC3339Nano, Version)
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://dl.minio.io:9000")
}
var experimentals Updates
decoder := json.NewDecoder(data)
e = decoder.Decode(&experimentals)
fatalIf(probe.NewError(e), "Unable to decode experimental update notification.")
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.URL()
if latest.After(current) {
console.Println(UpdateMessage{
Update: true,
Download: mcExperimentalURLParse.Scheme + "://" + mcExperimentalURLParse.Host + string(mcExperimentalURLParse.Separator) + experimentals.Platforms[runtime.GOOS],
Version: latest,
})
return
}
console.Println(UpdateMessage{
Update: false,
Download: mcExperimentalURLParse.Scheme + "://" + mcExperimentalURLParse.Host + string(mcExperimentalURLParse.Separator) + experimentals.Platforms[runtime.GOOS],
Version: latest,
})
}
func getReleaseUpdate() {
clnt, err := url2Client(mcUpdateURL)
fatalIf(err.Trace(mcUpdateURL), "Unable to initalize update URL.")
@@ -70,8 +169,7 @@ func mainUpdate(ctx *cli.Context) {
fatalIf(probe.NewError(e), "Unable to parse Version string as time.")
if current.IsZero() {
console.Infoln("Updates not supported for custom build. Version field is empty. Please download official releases from https://dl.minio.io:9000")
return
fatalIf(errDummy().Trace(), "Updates not supported for custom build. Version field is empty. Please download official releases from https://dl.minio.io:9000")
}
var updates Updates
@@ -83,23 +181,22 @@ func mainUpdate(ctx *cli.Context) {
fatalIf(probe.NewError(e), "Unable to parse BuildDate.")
if latest.IsZero() {
console.Infoln("No update available at this time.")
return
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.URL()
if latest.After(current) {
var updateString string
if runtime.GOOS == "windows" {
updateString = "mc.exe cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " .\\mc.exe"
} else {
updateString = "mc cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " ./mc"
}
msg, err := printUpdateNotify(updateString, "new", "old")
fatalIf(err.Trace(updateString), "Unable to print update notification string ‘"+updateString+"’.")
console.Println(msg)
console.Println(UpdateMessage{
Update: true,
Download: mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS],
Version: latest,
})
return
}
console.Infoln("You are already running the most recent version of ‘mc’.")
console.Println(UpdateMessage{
Update: false,
Download: mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS],
Version: latest,
})
return
}