Add ./mc update command

This commit is contained in:
Harshavardhana
2015-04-08 21:41:59 -07:00
parent 608a1e78e0
commit e8baa6c8e9
5 changed files with 161 additions and 1 deletions
+2
View File
@@ -37,6 +37,7 @@ type hostConfig struct {
type mcConfig struct {
Version uint
MCVersion string
DefaultHost string
Hosts map[string]hostConfig
Aliases map[string]string
@@ -231,6 +232,7 @@ func parseConfigInput(c *cli.Context) (config *mcConfig, err error) {
case len(alias) == 0:
config = &mcConfig{
Version: currentConfigVersion,
MCVersion: "0.9",
DefaultHost: "https://s3.amazonaws.com",
Hosts: map[string]hostConfig{
"http*://s3*.amazonaws.com": {
+13
View File
@@ -151,6 +151,18 @@ EXAMPLES:
},
},
}
updateCmd = cli.Command{
Name: "update",
Usage: "Check if new updates are available at http://minio.io",
Description: "",
Action: doUpdateCmd,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "download and update local binary",
},
},
}
)
type object struct {
@@ -174,6 +186,7 @@ var options = []cli.Command{
lsCmd,
mbCmd,
configCmd,
updateCmd,
}
var (
+81
View File
@@ -0,0 +1,81 @@
/*
* Minimalist Object Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"errors"
"fmt"
"io"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/minio-io/cli"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
const (
mcUpdateURL = "http://minio.io/updates/mc.json"
)
type updateResults struct {
Version uint // this is config version
LatestVersion string
Signature string
}
func getReq(url string) (*http.Request, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
msg := fmt.Sprintf("s3 client; invalid URL: %v", err)
return nil, errors.New(msg)
}
req.Header.Set("User-Agent", "Minio Client")
return req, nil
}
func doUpdateCmd(ctx *cli.Context) {
req, err := getReq(mcUpdateURL)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
}
res, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
}
if res.StatusCode != http.StatusOK {
msg := fmt.Sprintf("received invalid http status: %v", res.StatusCode)
log.Debug.Println(iodine.New(errors.New(msg), nil))
fatal(msg)
}
ures := updateResults{}
err = json.NewDecoder(io.TeeReader(res.Body, ioutil.Discard)).Decode(&ures)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
}
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
}
printUpdateNotify(ures.LatestVersion, config.MCVersion)
}
-1
View File
@@ -94,7 +94,6 @@ func main() {
app.Before = func(c *cli.Context) error {
globalQuietFlag = c.GlobalBool("quiet")
globalDebugFlag = c.GlobalBool("debug")
log.Println(globalDebugFlag)
if globalDebugFlag {
app.ExtraInfo = getSystemData()
} else {
+65
View File
@@ -0,0 +1,65 @@
package main
import (
"fmt"
"strings"
"github.com/fatih/color"
)
// go get update string
const (
updateString = "go get -u github.com/minio-io/mc"
)
// intMax - return maximum value for any given integer
func intMax(x, y int) int {
if x > y {
return x
}
return y
}
// printUpdateNotify -
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, since due to
// ANSI characters it would mis-calculate the real length
line1Length := len(fmt.Sprintf(" Update available: %s (current: %s)", latestVersion, currentVersion))
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))
// calculate the rectangular box size
maxContentWidth := intMax(line1Length, line2Length)
line1Rest := maxContentWidth - line1Length
line2Rest := maxContentWidth - line2Length
// color the rectangular box
top := yellow("┏" + strings.Repeat("━", maxContentWidth) + "┓")
bottom := yellow("┗" + strings.Repeat("━", maxContentWidth) + "┛")
sideBar := yellow("┃")
// fill spaces to the rest of the area
spacePaddingLine1 := strings.Repeat(" ", line1Rest)
spacePaddingLine2 := strings.Repeat(" ", line2Rest)
// construct the final message
message := "\n\n" + top + "\n" +
sideBar + line1InColor + spacePaddingLine1 + sideBar + "\n" +
sideBar + line2InColor + spacePaddingLine2 + sideBar + "\n" +
bottom + "\n"
// finally print the message
fmt.Println(message)
}