Move colors to console package, now implement colored

- Fatal, Fatalf, Fatalln
 - Error, Errorln
 - Info, Infof, Infoln
This commit is contained in:
Harshavardhana
2015-04-10 01:14:47 -07:00
parent 495e0f7511
commit ad35fe72d0
11 changed files with 140 additions and 104 deletions
+1 -2
View File
@@ -32,8 +32,7 @@ func isValidAliasName(aliasName string) bool {
return validAliasName.MatchString(aliasName)
}
// TODO fkautz: is this still relevant?
// aliasExpand expands aliased (name:/path) to full URL
// aliasExpand expands aliased (name:/path) to full URL, used by url-parser
func aliasExpand(aliasedURL string, aliases map[string]string) (newURL string, err error) {
url, err := url.Parse(aliasedURL)
if err != nil {
+2 -1
View File
@@ -25,6 +25,7 @@ import (
"github.com/cheggaaa/pb"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/client/s3"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
)
@@ -37,7 +38,7 @@ func startBar(size int64) *pb.ProgressBar {
bar.ShowSpeed = true
bar.Callback = func(s string) {
// Colorize
infoCallback(s)
console.Info("\r" + s)
}
// Feels like wget
bar.Format("[=> ]")
+9 -8
View File
@@ -16,6 +16,7 @@ import (
"path/filepath"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -53,7 +54,7 @@ func getMcConfigDir() string {
u, err := user.Current()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to obtain user's home directory")
console.Fatalln("Unable to obtain user's home directory")
}
var p string
// For windows the path is slightly differently
@@ -299,7 +300,7 @@ func getHostConfig(requestURL string) (*hostConfig, error) {
func getBashCompletionCmd() {
var b bytes.Buffer
if os.Getenv("SHELL") != "/bin/bash" {
fatal("Unsupported shell for bash completion detected.. exiting")
console.Fatalln("Unsupported shell for bash completion detected.. exiting")
}
b.WriteString(mcBashCompletion)
f := getMcBashCompletionFilename()
@@ -307,17 +308,17 @@ func getBashCompletionCmd() {
defer fl.Close()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to create bash completion file")
console.Fatalln("Unable to create bash completion file")
}
_, err = fl.Write(b.Bytes())
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to write bash completion file")
console.Fatalln("Unable to write bash completion file")
}
msg := "\nConfiguration written to " + f
msg = msg + "\n\n$ source ${HOME}/.mc/mc.bash_completion\n"
msg = msg + "$ echo 'source ${HOME}/.mc/mc.bash_completion' >> ${HOME}/.bashrc"
info(msg)
console.Infoln(msg)
}
// saveConfigCmd writes config file to disk
@@ -325,14 +326,14 @@ func saveConfigCmd(ctx *cli.Context) {
err := saveConfig(ctx)
if os.IsExist(iodine.ToError(err)) {
log.Debug.Println(iodine.New(err, nil))
fatal("mc: Configuration file", getMcConfigFilename(), "already exists")
console.Fatalf("mc: Configuration file %s already exists\n", getMcConfigFilename())
}
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("mc: Unable to generate config file", getMcConfigFilename())
console.Fatalf("mc: Unable to generate config file %s\n", getMcConfigFilename())
}
info("Configuration written to " + getMcConfigFilename() + ". Please update your access credentials.")
console.Infof("Configuration written to %s. Please update your access credentials.\n", getMcConfigFilename())
}
// doConfigCmd is the handler for "mc config" sub-command.
+5 -7
View File
@@ -24,6 +24,7 @@ import (
"github.com/cheggaaa/pb"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -117,9 +118,9 @@ func multiCopy(targetURLs []string, sourceURL string) (err error) {
for msg := range doPutMultiTarget(targetURLs, md5Hex, sourceSize, targetReaders) {
if msg.err != nil {
log.Debug.Println(msg.err)
fatal(msg.err)
console.Fatalln(msg.err)
}
info("Done")
console.Infoln("Done")
}
return nil
}
@@ -133,8 +134,7 @@ func doCopyCmd(ctx *cli.Context) {
urlList, err := parseURLs(ctx)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
return
console.Fatalln(err)
}
sourceURL := urlList[0] // First arg is source
targetURLs := urlList[1:] // 1 or more targets
@@ -142,8 +142,6 @@ func doCopyCmd(ctx *cli.Context) {
err = multiCopy(targetURLs, sourceURL)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
return
console.Fatalln(err)
}
return
}
+15 -10
View File
@@ -23,6 +23,7 @@ import (
"github.com/cheggaaa/pb"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -31,15 +32,17 @@ const (
printDate = "2006-01-02 15:04:05 MST"
)
// printBuckets lists buckets and its meta-dat
// printBuckets lists buckets and its metadata
func printBuckets(v []*client.Bucket) {
for _, b := range v {
// TODO: escape sequences internally in color package clobber the output, format this prior
// find a way out
msg := fmt.Sprintf("%23s %13s %s", b.CreationDate.Local().Format(printDate), "", b.Name)
info(msg)
console.Infoln(msg)
}
}
// printObjects prints a meta-data of a list of objects
// printObjects prints a metadata of a list of objects
func printObjects(v []*client.Item) {
if len(v) > 0 {
// Items are already sorted
@@ -51,8 +54,10 @@ func printObjects(v []*client.Item) {
// printObject prints object meta-data
func printObject(date time.Time, v int64, key string) {
// TODO: escape sequences internally in color package clobber the output, format this prior
// find a way out
msg := fmt.Sprintf("%23s %13s %s", date.Local().Format(printDate), pb.FormatBytes(v), key)
info(msg)
console.Infoln(msg)
}
// doListCmd lists objects inside a bucket
@@ -66,38 +71,38 @@ func doListCmd(ctx *cli.Context) {
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to get config")
console.Fatalln("Unable to get config")
}
urlStr, err := parseURL(ctx.Args().First(), config.Aliases)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
bucketName, objectName, err := url2Object(urlStr)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
client, err := getNewClient(globalDebugFlag, urlStr)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
if bucketName == "" { // List all buckets
buckets, err := client.ListBuckets()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
printBuckets(buckets)
} else {
items, err = client.ListObjects(bucketName, objectName)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
printObjects(items)
}
+7 -6
View File
@@ -19,6 +19,7 @@ package main
import (
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client/s3"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -32,34 +33,34 @@ func doMakeBucketCmd(ctx *cli.Context) {
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to get config")
console.Fatalln("Unable to get config")
}
urlStr, err := parseURL(ctx.Args().First(), config.Aliases)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
bucket, err := url2Bucket(urlStr)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
clnt, err := getNewClient(globalDebugFlag, urlStr)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
if !s3.IsValidBucketName(bucket) {
log.Debug.Println(iodine.New(err, nil))
fatal(errInvalidbucket)
console.Fatalln(errInvalidbucket)
}
err = clnt.PutBucket(bucket)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
}
+6 -5
View File
@@ -27,6 +27,7 @@ import (
"net/http"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -55,28 +56,28 @@ func doUpdateCmd(ctx *cli.Context) {
req, err := getReq(mcUpdateURL)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
res, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(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)
console.Fatalln(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)
console.Fatalln(err)
}
latest, err := time.Parse(time.RFC3339Nano, ures.LatestBuild)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal(err)
console.Fatalln(err)
}
if latest.After(ctx.App.Compiled) {
// FIXME : find some proper versioning scheme here
-60
View File
@@ -1,60 +0,0 @@
/*
* 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 (
"os"
"github.com/fatih/color"
)
func fatal(msgs ...interface{}) {
red := color.New(color.FgRed)
boldRed := red.Add(color.Bold)
colorPrintln(boldRed, msgs)
os.Exit(1)
}
func info(msgs ...interface{}) {
if !globalQuietFlag {
green := color.New(color.FgGreen)
boldGreen := green.Add(color.Bold)
colorPrintln(boldGreen, msgs)
}
}
func infoCallback(msg string) {
if !globalQuietFlag {
green := color.New(color.FgGreen)
boldGreen := green.Add(color.Bold)
boldGreen.Print("\r" + msg)
}
}
func colorPrint(c *color.Color, msgs []interface{}) {
for i, msg := range msgs {
if i != 0 {
c.Print(" ")
}
c.Print(msg)
}
}
func colorPrintln(c *color.Color, msgs []interface{}) {
colorPrint(c, msgs)
c.Println()
}
+5 -4
View File
@@ -27,6 +27,7 @@ import (
"github.com/cheggaaa/pb"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -36,7 +37,7 @@ func checkConfig() {
_, err := user.Current()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to obtain user's home directory")
console.Fatalln("Unable to obtain user's home directory")
}
if !isMcConfigExist() {
@@ -48,13 +49,13 @@ func checkConfig() {
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to read config file")
console.Fatalf("Unable to read config file: %s\n", getMcConfigFilename())
}
err = checkMcConfig(config)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Error in config file:", getMcConfigFilename())
console.Fatalf("Error in config file: %s\n", getMcConfigFilename())
}
}
@@ -105,7 +106,7 @@ func main() {
}
app.After = func(ctx *cli.Context) error {
if !isMcConfigExist() && ctx.Command.Name != "config" {
fatal("Error: mc is not configured. Please run \"mc config\".")
console.Fatalln("Error: mc is not configured. Please run \"mc config\".")
}
return nil
+88
View File
@@ -0,0 +1,88 @@
/*
* 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 console
import (
"os"
"github.com/fatih/color"
)
// Fatal - print in red and exit
func Fatal(msgs ...interface{}) {
red := color.New(color.FgRed, color.Bold)
colorPrint(red, msgs)
os.Exit(1)
}
// Fatalln - print in red with newline and exit
func Fatalln(msgs ...interface{}) {
red := color.New(color.FgRed, color.Bold)
colorPrintln(red, msgs)
os.Exit(1)
}
// Fatalf - format in red and exit
func Fatalf(format string, msgs ...interface{}) {
red := color.New(color.FgRed, color.Bold)
red.Printf(format, msgs)
os.Exit(1)
}
// Error - print in yellow
func Error(msgs ...interface{}) {
yellow := color.New(color.FgYellow, color.Bold)
colorPrint(yellow, msgs)
}
// Errorln - print in yellow with newline
func Errorln(msgs ...interface{}) {
yellow := color.New(color.FgYellow, color.Bold)
colorPrintln(yellow, msgs)
}
// Info - print in green
func Info(msgs ...interface{}) {
green := color.New(color.FgGreen, color.Bold)
colorPrint(green, msgs)
}
// Infoln - print in green with newline
func Infoln(msgs ...interface{}) {
green := color.New(color.FgGreen, color.Bold)
colorPrintln(green, msgs)
}
// Infof - format in green
func Infof(format string, msgs ...interface{}) {
green := color.New(color.FgGreen, color.Bold)
green.Printf(format, msgs)
}
func colorPrint(c *color.Color, msgs []interface{}) {
for i, msg := range msgs {
if i != 0 {
c.Print(" ")
}
c.Print(msg)
}
}
func colorPrintln(c *color.Color, msgs []interface{}) {
colorPrint(c, msgs)
c.Println()
}
+2 -1
View File
@@ -22,6 +22,7 @@ import (
"net/url"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
@@ -158,7 +159,7 @@ func parseURLs(c *cli.Context) (urlStr []string, err error) {
config, err := getMcConfig()
if err != nil {
log.Debug.Println(iodine.New(err, nil))
fatal("Unable to get config")
console.Fatalln("Unable to get config")
}
for _, arg := range c.Args() {
u, err := parseURL(arg, config.Aliases)