From 3fb99cdce87c69b6d7a3e9aebce5d4e2b7cea4c3 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 8 Mar 2015 00:43:02 -0800 Subject: [PATCH] Implement color coding for all the messages - Currently implemented are info() --- "green" infoCallback() --- "green" fatal() --- "red" warning() --- "yellow" --- Godeps/Godeps.json | 4 + .../src/github.com/mgutz/ansi/.gitignore | 1 + .../src/github.com/mgutz/ansi/LICENSE | 9 + .../src/github.com/mgutz/ansi/README.md | 119 +++++++++ .../src/github.com/mgutz/ansi/ansi.go | 244 ++++++++++++++++++ .../src/github.com/mgutz/ansi/ansi_test.go | 38 +++ .../mgutz/ansi/cmd/ansi-mgutz/main.go | 135 ++++++++++ .../src/github.com/mgutz/ansi/doc.go | 65 +++++ .../src/github.com/mgutz/ansi/print.go | 42 +++ color.go | 46 ++++ configure.go | 18 +- s3-fs-cp.go | 30 ++- s3-fs-ls.go | 25 +- s3-fs-mb.go | 10 +- s3-fs-sync.go | 33 +-- 15 files changed, 764 insertions(+), 55 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/.gitignore create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/LICENSE create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/README.md create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/ansi.go create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/ansi_test.go create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/cmd/ansi-mgutz/main.go create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/doc.go create mode 100644 Godeps/_workspace/src/github.com/mgutz/ansi/print.go create mode 100644 color.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 335a6b42..a893018b 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -10,6 +10,10 @@ "ImportPath": "github.com/codegangsta/cli", "Comment": "1.2.0-42-gfbda1ce", "Rev": "fbda1ce02d5dabcee952040e5f4025753b6c9ce0" + }, + { + "ImportPath": "github.com/mgutz/ansi", + "Rev": "22ddafb24adc4786c83841aa3021349818c898e4" } ] } diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/.gitignore b/Godeps/_workspace/src/github.com/mgutz/ansi/.gitignore new file mode 100644 index 00000000..9ed3b07c --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/.gitignore @@ -0,0 +1 @@ +*.test diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/LICENSE b/Godeps/_workspace/src/github.com/mgutz/ansi/LICENSE new file mode 100644 index 00000000..06ce0c3b --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) +Copyright (c) 2013 Mario L. Gutierrez + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/README.md b/Godeps/_workspace/src/github.com/mgutz/ansi/README.md new file mode 100644 index 00000000..886851d5 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/README.md @@ -0,0 +1,119 @@ +# ansi + +Package ansi is a small, fast library to create ANSI colored strings and codes. + +## Install + +This install the color viewer and the package itself + +```sh +go get -u github.com/mgutz/ansi/cmd/ansi-mgutz +``` + +## Example + +```go +import "github.com/mgutz/ansi" + +// colorize a string, SLOW +msg := ansi.Color("foo", "red+b:white") + +// create a closure to avoid recalculating ANSI code compilation +phosphorize := ansi.ColorFunc("green+h:black") +msg = phosphorize("Bring back the 80s!") +msg2 := phospohorize("Look, I'm a CRT!") + +// cache escape codes and build strings manually +lime := ansi.ColorCode("green+h:black") +reset := ansi.ColorCode("reset") + +fmt.Println(lime, "Bring back the 80s!", reset) +``` + +Other examples + +```go +Color(s, "red") // red +Color(s, "red+b") // red bold +Color(s, "red+B") // red blinking +Color(s, "red+u") // red underline +Color(s, "red+bh") // red bold bright +Color(s, "red:white") // red on white +Color(s, "red+b:white+h") // red bold on white bright +Color(s, "red+B:white+h") // red blink on white bright +``` + +To view color combinations, from terminal. + +```sh +ansi-mgutz +``` + +## Style format + +```go +"foregroundColor+attributes:backgroundColor+attributes" +``` + +Colors + +* black +* red +* green +* yellow +* blue +* magenta +* cyan +* white +* 0...255 (256 colors) + +Attributes + +* b = bold foreground +* B = Blink foreground +* u = underline foreground +* i = inverse +* h = high intensity (bright) foreground, background + + does not work with 256 colors + +## Constants + +* ansi.Reset +* ansi.DefaultBG +* ansi.DefaultFG +* ansi.DefaultFG +* ansi.Black +* ansi.Red +* ansi.Green +* ansi.Yellow +* ansi.Blue +* ansi.Magenta +* ansi.Cyan +* ansi.White +* ansi.LightBlack +* ansi.LightRed +* ansi.LightGreen +* ansi.LightYellow +* ansi.LightBlue +* ansi.LightMagenta +* ansi.LightCyan +* ansi.LightWhite + + +## References + +Wikipedia ANSI escape codes [Colors](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) + +General [tips and formatting](http://misc.flogisoft.com/bash/tip_colors_and_formatting) + +What about support on Windows? Use [colorable by mattn](https://github.com/mattn/go-colorable). +Ansi and colorable are used by [logxi](https://github.com/mgutz/logxi) to support logging in +color on Windows. + +## MIT License + +Copyright (c) 2013 Mario Gutierrez mario@mgutz.com + +See the file LICENSE for copying permission. + diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/ansi.go b/Godeps/_workspace/src/github.com/mgutz/ansi/ansi.go new file mode 100644 index 00000000..cba723bf --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/ansi.go @@ -0,0 +1,244 @@ +package ansi + +import ( + "bytes" + "fmt" + "strconv" + "strings" +) + +const ( + black = iota + red + green + yellow + blue + magenta + cyan + white + defaultt = 9 + + normalIntensityFG = 30 + highIntensityFG = 90 + normalIntensityBG = 40 + highIntensityBG = 100 + + start = "\033[" + bold = "1;" + blink = "5;" + underline = "4;" + inverse = "7;" + + // Reset is the ANSI reset escape sequence + Reset = "\033[0m" + // DefaultBG is the default background + DefaultBG = "\033[49m" + // DefaultFG is the default foreground + DefaultFG = "\033[39m" +) + +// Black FG +var Black string + +// Red FG +var Red string + +// Green FG +var Green string + +// Yellow FG +var Yellow string + +// Blue FG +var Blue string + +// Magenta FG +var Magenta string + +// Cyan FG +var Cyan string + +// White FG +var White string + +// LightBlack FG +var LightBlack string + +// LightRed FG +var LightRed string + +// LightGreen FG +var LightGreen string + +// LightYellow FG +var LightYellow string + +// LightBlue FG +var LightBlue string + +// LightMagenta FG +var LightMagenta string + +// LightCyan FG +var LightCyan string + +// LightWhite FG +var LightWhite string + +var ( + plain = false + // Colors maps common color names to their ANSI color code. + Colors = map[string]int{ + "black": black, + "red": red, + "green": green, + "yellow": yellow, + "blue": blue, + "magenta": magenta, + "cyan": cyan, + "white": white, + "default": defaultt, + } +) + +func init() { + for i := 0; i < 256; i++ { + Colors[strconv.Itoa(i)] = i + } + + Black = ColorCode("black") + Red = ColorCode("red") + Green = ColorCode("green") + Yellow = ColorCode("yellow") + Blue = ColorCode("blue") + Magenta = ColorCode("magenta") + Cyan = ColorCode("cyan") + White = ColorCode("white") + LightBlack = ColorCode("black+h") + LightRed = ColorCode("red+h") + LightGreen = ColorCode("green+h") + LightYellow = ColorCode("yellow+h") + LightBlue = ColorCode("blue+h") + LightMagenta = ColorCode("magenta+h") + LightCyan = ColorCode("cyan+h") + LightWhite = ColorCode("white+h") +} + +// ColorCode returns the ANSI color color code for style. +func ColorCode(style string) string { + return colorCode(style).String() +} + +// Gets the ANSI color code for a style. +func colorCode(style string) *bytes.Buffer { + buf := bytes.NewBufferString("") + if plain || style == "" { + return buf + } + if style == "reset" { + buf.WriteString(Reset) + return buf + } + + foregroundBackground := strings.Split(style, ":") + foreground := strings.Split(foregroundBackground[0], "+") + fgKey := foreground[0] + fg := Colors[fgKey] + fgStyle := "" + if len(foreground) > 1 { + fgStyle = foreground[1] + } + + bg, bgStyle := "", "" + + if len(foregroundBackground) > 1 { + background := strings.Split(foregroundBackground[1], "+") + bg = background[0] + if len(background) > 1 { + bgStyle = background[1] + } + } + + buf.WriteString(start) + base := normalIntensityFG + if len(fgStyle) > 0 { + if strings.Contains(fgStyle, "b") { + buf.WriteString(bold) + } + if strings.Contains(fgStyle, "B") { + buf.WriteString(blink) + } + if strings.Contains(fgStyle, "u") { + buf.WriteString(underline) + } + if strings.Contains(fgStyle, "i") { + buf.WriteString(inverse) + } + if strings.Contains(fgStyle, "h") { + base = highIntensityFG + } + } + + // if 256-color + n, err := strconv.Atoi(fgKey) + if err == nil { + fmt.Fprintf(buf, "38;5;%d;", n) + } else { + fmt.Fprintf(buf, "%d;", base+fg) + } + + base = normalIntensityBG + if len(bg) > 0 { + if strings.Contains(bgStyle, "h") { + base = highIntensityBG + } + // if 256-color + n, err := strconv.Atoi(bg) + if err == nil { + fmt.Fprintf(buf, "48;5;%d;", n) + } else { + fmt.Fprintf(buf, "%d;", base+Colors[bg]) + } + } + + // remove last ";" + buf.Truncate(buf.Len() - 1) + buf.WriteRune('m') + return buf +} + +// Color colors a string based on the ANSI color code for style. +func Color(s, style string) string { + if plain || len(style) < 1 { + return s + } + buf := colorCode(style) + buf.WriteString(s) + buf.WriteString(Reset) + return buf.String() +} + +// ColorFunc creates a closureto avoid ANSI color code calculation. +func ColorFunc(style string) func(string) string { + if style == "" { + return func(s string) string { + return s + } + } + color := ColorCode(style) + return func(s string) string { + if plain || s == "" { + return s + } + buf := bytes.NewBufferString(color) + buf.WriteString(s) + buf.WriteString(Reset) + result := buf.String() + return result + } +} + +// DisableColors disables ANSI color codes. On by default. +func DisableColors(disable bool) { + plain = disable +} diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/ansi_test.go b/Godeps/_workspace/src/github.com/mgutz/ansi/ansi_test.go new file mode 100644 index 00000000..a1caea87 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/ansi_test.go @@ -0,0 +1,38 @@ +package ansi + +import ( + "fmt" + "testing" +) + +func TestPlain(t *testing.T) { + DisableColors(true) + bgColors := []string{ + "", + ":black", + ":red", + ":green", + ":yellow", + ":blue", + ":magenta", + ":cyan", + ":white", + } + for fg := range Colors { + for _, bg := range bgColors { + println(padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg})) + println(padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"})) + println(padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"})) + } + } +} + +func TestStyles(t *testing.T) { + PrintStyles() + DisableColors(false) +} + +func ExampleColorFunc() { + brightGreen := ColorFunc("green+h") + fmt.Println(brightGreen("lime")) +} diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/cmd/ansi-mgutz/main.go b/Godeps/_workspace/src/github.com/mgutz/ansi/cmd/ansi-mgutz/main.go new file mode 100644 index 00000000..66ab7797 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/cmd/ansi-mgutz/main.go @@ -0,0 +1,135 @@ +package main + +import ( + "fmt" + "sort" + "strconv" + + "github.com/mattn/go-colorable" + "github.com/mgutz/ansi" +) + +func main() { + printColors() + print256Colors() + printConstants() +} + +func pad(s string, length int) string { + for len(s) < length { + s += " " + } + return s +} + +func padColor(s string, styles []string) string { + buffer := "" + for _, style := range styles { + buffer += ansi.Color(pad(s+style, 20), s+style) + } + return buffer +} + +func printPlain() { + ansi.DisableColors(true) + bgColors := []string{ + "", + ":black", + ":red", + ":green", + ":yellow", + ":blue", + ":magenta", + ":cyan", + ":white", + } + for fg := range ansi.Colors { + for _, bg := range bgColors { + println(padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg})) + println(padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"})) + println(padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"})) + } + } +} + +func printColors() { + ansi.DisableColors(false) + stdout := colorable.NewColorableStdout() + + bgColors := []string{ + "", + ":black", + ":red", + ":green", + ":yellow", + ":blue", + ":magenta", + ":cyan", + ":white", + } + + keys := []string{} + for fg := range ansi.Colors { + _, err := strconv.Atoi(fg) + if err != nil { + keys = append(keys, fg) + } + } + sort.Strings(keys) + + for _, fg := range keys { + for _, bg := range bgColors { + fmt.Fprintln(stdout, padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg})) + fmt.Fprintln(stdout, padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"})) + fmt.Fprintln(stdout, padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"})) + } + } +} + +func print256Colors() { + ansi.DisableColors(false) + stdout := colorable.NewColorableStdout() + + bgColors := []string{""} + for i := 0; i < 256; i++ { + key := fmt.Sprintf(":%d", i) + bgColors = append(bgColors, key) + } + + keys := []string{} + for fg := range ansi.Colors { + n, err := strconv.Atoi(fg) + if err == nil { + keys = append(keys, fmt.Sprintf("%3d", n)) + } + } + sort.Strings(keys) + + for _, fg := range keys { + for _, bg := range bgColors { + fmt.Fprintln(stdout, padColor(fg, []string{"" + bg, "+b" + bg, "+u" + bg})) + fmt.Fprintln(stdout, padColor(fg, []string{"+B" + bg, "+Bb" + bg})) + } + } +} + +func printConstants() { + stdout := colorable.NewColorableStdout() + fmt.Fprintln(stdout, ansi.DefaultFG, "ansi.DefaultFG", ansi.Reset) + fmt.Fprintln(stdout, ansi.Black, "ansi.Black", ansi.Reset) + fmt.Fprintln(stdout, ansi.Red, "ansi.Red", ansi.Reset) + fmt.Fprintln(stdout, ansi.Green, "ansi.Green", ansi.Reset) + fmt.Fprintln(stdout, ansi.Yellow, "ansi.Yellow", ansi.Reset) + fmt.Fprintln(stdout, ansi.Blue, "ansi.Blue", ansi.Reset) + fmt.Fprintln(stdout, ansi.Magenta, "ansi.Magenta", ansi.Reset) + fmt.Fprintln(stdout, ansi.Cyan, "ansi.Cyan", ansi.Reset) + fmt.Fprintln(stdout, ansi.White, "ansi.White", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightBlack, "ansi.LightBlack", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightRed, "ansi.LightRed", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightGreen, "ansi.LightGreen", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightYellow, "ansi.LightYellow", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightBlue, "ansi.LightBlue", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightMagenta, "ansi.LightMagenta", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightCyan, "ansi.LightCyan", ansi.Reset) + fmt.Fprintln(stdout, ansi.LightWhite, "ansi.LightWhite", ansi.Reset) +} diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/doc.go b/Godeps/_workspace/src/github.com/mgutz/ansi/doc.go new file mode 100644 index 00000000..43c217e1 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/doc.go @@ -0,0 +1,65 @@ +/* +Package ansi is a small, fast library to create ANSI colored strings and codes. + +Installation + + # this installs the color viewer and the package + go get -u github.com/mgutz/ansi/cmd/ansi-mgutz + +Example + + // colorize a string, SLOW + msg := ansi.Color("foo", "red+b:white") + + // create a closure to avoid recalculating ANSI code compilation + phosphorize := ansi.ColorFunc("green+h:black") + msg = phosphorize("Bring back the 80s!") + msg2 := phospohorize("Look, I'm a CRT!") + + // cache escape codes and build strings manually + lime := ansi.ColorCode("green+h:black") + reset := ansi.ColorCode("reset") + + fmt.Println(lime, "Bring back the 80s!", reset) + +Other examples + + Color(s, "red") // red + Color(s, "red+b") // red bold + Color(s, "red+B") // red blinking + Color(s, "red+u") // red underline + Color(s, "red+bh") // red bold bright + Color(s, "red:white") // red on white + Color(s, "red+b:white+h") // red bold on white bright + Color(s, "red+B:white+h") // red blink on white bright + +To view color combinations, from terminal + + ansi-mgutz + +Style format + + "foregroundColor+attributes:backgroundColor+attributes" + +Colors + + black + red + green + yellow + blue + magenta + cyan + white + +Attributes + + b = bold foreground + B = Blink foreground + u = underline foreground + h = high intensity (bright) foreground, background + i = inverse + +Wikipedia ANSI escape codes [Colors](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) +*/ +package ansi diff --git a/Godeps/_workspace/src/github.com/mgutz/ansi/print.go b/Godeps/_workspace/src/github.com/mgutz/ansi/print.go new file mode 100644 index 00000000..044c9a68 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mgutz/ansi/print.go @@ -0,0 +1,42 @@ +package ansi + +// PrintStyles prints all style combinations to the terminal. +func PrintStyles() { + oldPlain := plain + plain = false + + bgColors := []string{ + "", + ":black", + ":red", + ":green", + ":yellow", + ":blue", + ":magenta", + ":cyan", + ":white", + } + for fg := range Colors { + for _, bg := range bgColors { + println(padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg})) + println(padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"})) + println(padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"})) + } + } + plain = oldPlain +} + +func pad(s string, length int) string { + for len(s) < length { + s += " " + } + return s +} + +func padColor(s string, styles []string) string { + buffer := "" + for _, style := range styles { + buffer += Color(pad(s+style, 20), s+style) + } + return buffer +} diff --git a/color.go b/color.go new file mode 100644 index 00000000..5cd4f251 --- /dev/null +++ b/color.go @@ -0,0 +1,46 @@ +/* + * Mini 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 ( + "fmt" + "os" + + "github.com/mgutz/ansi" +) + +// Color coding format "foregroundColor+attributes:backgroundColor+attributes" +func fatal(msg string) { + red := ansi.ColorFunc("red+B") + fmt.Println(red(msg)) + os.Exit(1) +} + +func warning(msg string) { + yellow := ansi.ColorFunc("yellow") + fmt.Println(yellow(msg)) +} + +func info(msg string) { + green := ansi.ColorFunc("green") + fmt.Println(green(msg)) +} + +func infoCallback(msg string) { + green := ansi.ColorFunc("green") + fmt.Print("\r" + green(msg)) +} diff --git a/configure.go b/configure.go index 7edaaffd..86464e3c 100644 --- a/configure.go +++ b/configure.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "os" "os/user" @@ -30,7 +29,8 @@ type mcConfig struct { func getMcConfigDir() string { u, err := user.Current() if err != nil { - log.Fatalf("mc: Unable to obtain user's home directory. \nERROR[%v]\n", err) + msg := fmt.Sprintf("mc: Unable to obtain user's home directory. \nERROR[%v]", err) + fatal(msg) } return path.Join(u.HomeDir + "/" + mcConfigDir) @@ -75,37 +75,37 @@ func parseConfigureInput(c *cli.Context) (config *mcConfig, err error) { }, }, } - return config, nil } func doConfigure(c *cli.Context) { configData, err := parseConfigureInput(c) if err != nil { - log.Fatal(err) + fatal(err.Error()) } jsonConfig, err := json.MarshalIndent(configData, "", "\t") if err != nil { - log.Fatal(err) + fatal(err.Error()) } err = os.MkdirAll(getMcConfigDir(), 0755) if err != nil { - log.Fatal(err) + fatal(err.Error()) } configFile, err := os.OpenFile(getMcConfigFilename(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) defer configFile.Close() if err != nil { - log.Fatal(err) + fatal(err.Error()) } _, err = configFile.Write(jsonConfig) if err != nil { - log.Fatal(err) + fatal(err.Error()) } - fmt.Println("\nConfiguration written to ", getMcConfigFilename()) + msg := "\nConfiguration written to " + getMcConfigFilename() + info(msg) } func getNewClient(config *mcConfig) (*s3.Client, error) { diff --git a/s3-fs-cp.go b/s3-fs-cp.go index 40b1bd69..485b4fb4 100644 --- a/s3-fs-cp.go +++ b/s3-fs-cp.go @@ -19,7 +19,6 @@ package main import ( "fmt" "io" - "log" "os" "time" @@ -35,6 +34,7 @@ func startBar(size int64) *pb.ProgressBar { bar := pb.StartNew(int(size)) bar.SetUnits(pb.U_BYTES) bar.SetRefreshRate(time.Millisecond * 10) + bar.NotPrint = true bar.ShowSpeed = true return bar } @@ -42,32 +42,32 @@ func startBar(size int64) *pb.ProgressBar { func doFsCopy(c *cli.Context) { mcConfig, err := getMcConfig() if err != nil { - log.Fatal(err) + fatal(err.Error()) } s3c, err := getNewClient(mcConfig) if err != nil { - log.Fatal(err) + fatal(err.Error()) } var fsoptions *fsOptions fsoptions, err = parseOptions(c) if err != nil { - log.Fatal(err) + fatal(err.Error()) } if fsoptions.isput { stat, err := os.Stat(fsoptions.body) if os.IsNotExist(err) { - log.Fatal(err) + fatal(err.Error()) } if stat.IsDir() { - log.Fatal("Is a directory") + fatal("Is a directory") } size := stat.Size() bodyFile, err := os.Open(fsoptions.body) defer bodyFile.Close() if err != nil { - log.Fatal(err) + fatal(err.Error()) } // s3:// is specified without key @@ -77,31 +77,35 @@ func doFsCopy(c *cli.Context) { err = s3c.Put(fsoptions.bucket, fsoptions.key, size, bodyFile) if err != nil { - log.Fatal(err) + fatal(err.Error()) } - fmt.Printf("%s uploaded -- to bucket:%s\n", fsoptions.key, fsoptions.bucket) + msg := fmt.Sprintf("%s uploaded -- to bucket:(%s)", fsoptions.key, fsoptions.bucket) + info(msg) } else if fsoptions.isget { var objectReader io.ReadCloser var objectSize int64 objectReader, objectSize, err = s3c.Get(fsoptions.bucket, fsoptions.key) if err != nil { - log.Fatal(err) + fatal(err.Error()) } bodyFile, err := os.Create(fsoptions.body) defer bodyFile.Close() // start progress bar bar := startBar(objectSize) + bar.Callback = func(s string) { + infoCallback(s) + } // create multi writer to feed data writer := io.MultiWriter(bodyFile, bar) - _, err = io.CopyN(writer, objectReader, objectSize) if err != nil { - log.Fatal(err) + fatal(err.Error()) } - bar.FinishPrint(fsoptions.body + " downloaded!") + bar.Finish() + info("Success!") } } diff --git a/s3-fs-ls.go b/s3-fs-ls.go index 81da614a..5326d9ff 100644 --- a/s3-fs-ls.go +++ b/s3-fs-ls.go @@ -18,7 +18,6 @@ package main import ( "fmt" - "log" "sort" "time" @@ -47,7 +46,8 @@ func parseLastModified(t string) string { func printBuckets(v []*s3.Bucket) { for _, b := range v { - fmt.Printf("%s %s\n", parseTime(b.CreationDate), b.Name) + msg := fmt.Sprintf("%s %s", parseTime(b.CreationDate), b.Name) + info(msg) } } @@ -55,7 +55,8 @@ func printObjects(v []*s3.Item) { if len(v) > 0 { sort.Sort(s3.BySize(v)) for _, b := range v { - fmt.Printf("%s %d %s\n", parseTime(b.LastModified), b.Size, b.Key) + msg := fmt.Sprintf("%s %d %s", parseTime(b.LastModified), b.Size, b.Key) + info(msg) } } } @@ -63,13 +64,15 @@ func printObjects(v []*s3.Item) { func printPrefixes(v []*s3.Prefix) { if len(v) > 0 { for _, b := range v { - fmt.Printf(" DIR %s\n", b.Prefix) + msg := fmt.Sprintf(" DIR %s", b.Prefix) + info(msg) } } } func printObject(v int64, date, key string) { - fmt.Printf("%s %d %s\n", parseLastModified(date), v, key) + msg := fmt.Sprintf("%s %d %s", parseLastModified(date), v, key) + info(msg) } func doFsList(c *cli.Context) { @@ -78,28 +81,28 @@ func doFsList(c *cli.Context) { config, err := getMcConfig() if err != nil { - log.Fatal(err) + fatal(err.Error()) } s3c, err := getNewClient(config) if err != nil { - log.Fatal(err) + fatal(err.Error()) } fsoptions, err := parseOptions(c) if err != nil { - log.Fatal(err) + fatal(err.Error()) } switch true { case fsoptions.bucket == "": buckets, err := s3c.Buckets() if err != nil { - log.Fatal(err) + fatal(err.Error()) } printBuckets(buckets) case fsoptions.key == "": items, prefixes, err = s3c.GetBucket(fsoptions.bucket, "", "", "", s3.MaxKeys) if err != nil { - log.Fatal(err) + fatal(err.Error()) } printPrefixes(prefixes) printObjects(items) @@ -110,7 +113,7 @@ func doFsList(c *cli.Context) { if err != nil { items, prefixes, err = s3c.GetBucket(fsoptions.bucket, "", fsoptions.key, string(delimiter), s3.MaxKeys) if err != nil { - log.Fatal(err) + fatal(err.Error()) } printPrefixes(prefixes) printObjects(items) diff --git a/s3-fs-mb.go b/s3-fs-mb.go index 24095742..6be2ff74 100644 --- a/s3-fs-mb.go +++ b/s3-fs-mb.go @@ -17,8 +17,6 @@ package main import ( - "log" - "github.com/codegangsta/cli" "github.com/minio-io/mc/pkg/s3" ) @@ -27,20 +25,20 @@ func doFsMb(c *cli.Context) { switch len(c.Args()) { case 1: if !s3.IsValidBucket(c.Args().Get(0)) { - log.Fatalf("%s: (%s)", errInvalidbucket, c.Args().Get(0)) + fatal(errInvalidbucket.Error()) } default: - log.Fatal("Needs an argument ") + fatal("Needs an argument ") } bucketName := c.Args().Get(0) var err error auth, err := getMcConfig() if err != nil { - log.Fatal(err) + fatal(err.Error()) } s3c, err := getNewClient(auth) err = s3c.PutBucket(bucketName) if err != nil { - log.Fatal(err) + fatal(err.Error()) } } diff --git a/s3-fs-sync.go b/s3-fs-sync.go index 512e6485..2c786dbc 100644 --- a/s3-fs-sync.go +++ b/s3-fs-sync.go @@ -18,7 +18,6 @@ package main import ( "fmt" - "log" "os" "path" "path/filepath" @@ -30,13 +29,13 @@ import ( func isValidBucketName(p string) { if path.IsAbs(p) { - log.Fatal("directory bucketname cannot be absolute") + fatal("directory bucketname cannot be absolute") } if strings.HasPrefix(p, "..") { - log.Fatal("Relative directory references not supported") + fatal("Relative directory references not supported") } if !s3.IsValidBucket(p) { - log.Fatal(errInvalidbucket) + fatal(errInvalidbucket.Error()) } } @@ -44,11 +43,11 @@ type walk struct { s3 *s3.Client } -func (w *walk) putWalk(p string, info os.FileInfo, err error) error { - if info.IsDir() { +func (w *walk) putWalk(p string, i os.FileInfo, err error) error { + if i.IsDir() { return nil } - if !info.Mode().IsRegular() { + if !i.Mode().IsRegular() { return nil } parts := strings.SplitN(p, "/", 2) @@ -57,11 +56,13 @@ func (w *walk) putWalk(p string, info os.FileInfo, err error) error { bodyFile, err := os.Open(p) defer bodyFile.Close() - err = w.s3.Put(bucketname, key, info.Size(), bodyFile) + err = w.s3.Put(bucketname, key, i.Size(), bodyFile) if err != nil { return err } - fmt.Printf("%s uploaded -- to bucket:%s\n", key, bucketname) + msg := fmt.Sprintf("%s uploaded -- to bucket:%s", key, bucketname) + info(msg) + return nil } @@ -70,11 +71,11 @@ func doFsSync(c *cli.Context) { var err error config, err := getMcConfig() if err != nil { - log.Fatal(err) + fatal(err.Error()) } s3c, err = getNewClient(config) if err != nil { - log.Fatal(err) + fatal(err.Error()) } p := &walk{s3c} @@ -85,21 +86,21 @@ func doFsSync(c *cli.Context) { fl, err := os.Stat(input) if os.IsNotExist(err) { - log.Fatal(err) + fatal(err.Error()) } if !fl.IsDir() { - log.Fatal("Should be a directory") + fatal("Should be a directory") } // Create bucketname, before uploading files err = s3c.PutBucket(input) if err != nil { - log.Fatal(err) + fatal(err.Error()) } err = filepath.Walk(input, p.putWalk) if err != nil { - log.Fatal(err) + fatal(err.Error()) } default: - log.Fatal("Requires a directory name ") + fatal("Requires a directory name ") } }