From eeb71d212a7b55986a37d2a3788c29c6346b4500 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 24 May 2015 13:06:58 -0700 Subject: [PATCH] Add missing String() method for ErrorMessage struct as well --- globals.go | 14 --------- ls.go | 16 ++--------- pkg/console/console.go | 31 ++++++++------------ print-structs.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 print-structs.go diff --git a/globals.go b/globals.go index 08e7867d..b72f205c 100644 --- a/globals.go +++ b/globals.go @@ -45,17 +45,3 @@ const ( const ( exampleHostURL = "YOUR-EXAMPLE.COM" ) - -// ErrorMessage container for message reason encapsulation -type ErrorMessage struct { - Message string - Error error -} - -// Content container for content message structure -type Content struct { - Filetype string `json:"ContentType"` - Time string `json:"LastModified"` - Size string `json:"Size"` - Name string `json:"Name"` -} diff --git a/ls.go b/ls.go index 7f1c4be0..09f09117 100644 --- a/ls.go +++ b/ls.go @@ -34,18 +34,7 @@ const ( printDate = "2006-01-02 15:04:05 MST" ) -func (c Content) String() string { - message := console.Time("[%s] ", c.Time) - message = message + console.Size("%6s ", c.Size) - message = func() string { - if c.Filetype == "inode/directory" { - return message + console.Dir("%s", c.Name) - } - return message + console.File("%s", c.Name) - }() - return message -} - +// parseContent parse client Content container into printer struct func parseContent(c *client.Content) Content { content := Content{} content.Time = c.Time.Local().Format(printDate) @@ -94,8 +83,7 @@ func doList(clnt client.Client, targetURL string, recursive bool) error { contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/") } contentCh.Content.Name = contentName - content := parseContent(contentCh.Content) - console.Println(content) + console.Println(parseContent(contentCh.Content)) } if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) diff --git a/pkg/console/console.go b/pkg/console/console.go index 900b6034..35f82c05 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -51,14 +51,13 @@ type Theme struct { Print *color.Color } -func readErrordata(data interface{}) (string, error) { +func readErrorFromdata(data interface{}) error { st := structs.New(data) if st.IsZero() { - return "", nil + return nil } - msg := st.Field("Message") msgErr := st.Field("Error") - return msg.Value().(string), msgErr.Value().(error) + return msgErr.Value().(error) } var ( @@ -107,12 +106,10 @@ var ( // Fatal prints a error message and exits Fatal = func(data interface{}) { defer os.Exit(1) - msg, err := readErrordata(data) + err := readErrorFromdata(data) if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(err).Error() - message := msg + ", " + reason - print(themesDB[currThemeName].Error, message) + print(themesDB[currThemeName].Error, data) if !NoDebugPrint { print(themesDB[currThemeName].Error, err) } @@ -125,12 +122,10 @@ var ( // Fatalln prints a error message with a new line and exits Fatalln = func(data interface{}) { defer os.Exit(1) - msg, err := readErrordata(data) + err := readErrorFromdata(data) if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(err).Error() - message := msg + ", " + reason - println(themesDB[currThemeName].Error, message) + println(themesDB[currThemeName].Error, data) if !NoDebugPrint { println(themesDB[currThemeName].Error, err) } @@ -143,12 +138,10 @@ var ( // Error prints a error message Error = func(data interface{}) { - msg, err := readErrordata(data) + err := readErrorFromdata(data) if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(err).Error() - message := msg + ", " + reason - print(themesDB[currThemeName].Error, message) + print(themesDB[currThemeName].Error, data) if !NoDebugPrint { print(themesDB[currThemeName].Error, err) } @@ -160,12 +153,10 @@ var ( } // Errorln prints a error message with a new line Errorln = func(data interface{}) { - msg, err := readErrordata(data) + err := readErrorFromdata(data) if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(err).Error() - message := msg + ", " + reason - println(themesDB[currThemeName].Error, message) + println(themesDB[currThemeName].Error, err) if !NoDebugPrint { println(themesDB[currThemeName].Error, err) } diff --git a/print-structs.go b/print-structs.go new file mode 100644 index 00000000..8a3bc42d --- /dev/null +++ b/print-structs.go @@ -0,0 +1,64 @@ +/* + * Minio Client (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. + */ + +// This package contains all the structs, their method wrappers for printer +package main + +import ( + "github.com/minio/mc/pkg/console" + "github.com/minio/minio/pkg/iodine" +) + +// ErrorMessage container for message reason encapsulation +type ErrorMessage struct { + Message string + Error error +} + +func (e ErrorMessage) String() string { + var message string + if e.Error != nil { + switch e.Error.(type) { + case iodine.Error: + reason := "Reason: " + iodine.ToError(e.Error).Error() + message = e.Message + ", " + reason + default: + reason := "Reason: " + e.Error.Error() + message = e.Message + ", " + reason + } + } + return message +} + +// Content container for content message structure +type Content struct { + Filetype string `json:"ContentType"` + Time string `json:"LastModified"` + Size string `json:"Size"` + Name string `json:"Name"` +} + +func (c Content) String() string { + message := console.Time("[%s] ", c.Time) + message = message + console.Size("%6s ", c.Size) + message = func() string { + if c.Filetype == "inode/directory" { + return message + console.Dir("%s", c.Name) + } + return message + console.File("%s", c.Name) + }() + return message +}