From 192285c2af77c5d86fcf7005219c3082f75a66c3 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 24 May 2015 12:50:13 -0700 Subject: [PATCH] Remove console to depend on its own structures, instead let it deal with interface{} --- cmd-access.go | 12 ++-- cmd-cat.go | 10 +-- cmd-config.go | 6 +- cmd-cp.go | 8 +-- cmd-diff.go | 20 +++--- cmd-ls.go | 12 ++-- cmd-mb.go | 12 ++-- cmd-sync.go | 12 ++-- cmd-update.go | 10 +-- diff.go | 4 +- globals.go | 14 ++++ ls.go | 53 +++++++++----- main.go | 10 +-- pkg/console/console.go | 160 +++++++++++++++++++---------------------- pkg/console/themes.go | 3 + 15 files changed, 185 insertions(+), 161 deletions(-) diff --git a/cmd-access.go b/cmd-access.go index 1be010cb..6e27ac58 100644 --- a/cmd-access.go +++ b/cmd-access.go @@ -32,14 +32,14 @@ func runAccessCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) } config, err := getMcConfig() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "loading config file failed", Error: iodine.New(err, nil), }) @@ -47,14 +47,14 @@ func runAccessCmd(ctx *cli.Context) { targetURLConfigMap := make(map[string]*hostConfig) targetURLs, err := getExpandedURLs(ctx.Args(), config.Aliases) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Unknown type of URL ", Error: iodine.New(err, nil), }) } acl := bucketACL(ctx.Args().First()) if !acl.isValidBucketACL() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Valid types are [private, public, readonly].", Error: iodine.New(errors.New("Invalid ACL Type ‘"+acl.String()+"’"), nil), }) @@ -63,7 +63,7 @@ func runAccessCmd(ctx *cli.Context) { for _, targetURL := range targetURLs { targetConfig, err := getHostConfig(targetURL) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Unable to read configuration for host " + "‘" + targetURL + "’", Error: iodine.New(err, nil), }) @@ -73,7 +73,7 @@ func runAccessCmd(ctx *cli.Context) { for targetURL, targetConfig := range targetURLConfigMap { errorMsg, err := doUpdateAccessCmd(targetURL, acl.String(), targetConfig) if err != nil { - console.Errorln(console.ErrorMessage{ + console.Errorln(ErrorMessage{ Message: errorMsg, Error: iodine.New(err, nil), }) diff --git a/cmd-cat.go b/cmd-cat.go index d2ea4fa1..f70313c4 100644 --- a/cmd-cat.go +++ b/cmd-cat.go @@ -32,14 +32,14 @@ func runCatCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) } config, err := getMcConfig() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), Error: iodine.New(err, nil), }) @@ -48,7 +48,7 @@ func runCatCmd(ctx *cli.Context) { // Convert arguments to URLs: expand alias, fix format... urls, err := getExpandedURLs(ctx.Args(), config.Aliases) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", urls), Error: iodine.New(err, nil), }) @@ -57,14 +57,14 @@ func runCatCmd(ctx *cli.Context) { sourceURLs := urls sourceURLConfigMap, err := getHostConfigs(sourceURLs) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read host configuration for ‘%s’ from config file ‘%s’", sourceURLs, mustGetMcConfigPath()), Error: iodine.New(err, nil), }) } humanReadable, err := doCatCmd(sourceURLConfigMap) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: humanReadable, Error: iodine.New(err, nil), }) diff --git a/cmd-config.go b/cmd-config.go index d40652e8..e2ca9119 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -34,19 +34,19 @@ func runConfigCmd(ctx *cli.Context) { arg := ctx.Args().First() tailArgs := ctx.Args().Tail() if len(tailArgs) > 2 { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Incorrect number of arguments, please use \"mc config help\"", Error: iodine.New(errInvalidArgument{}, nil), }) } msg, err := doConfig(arg, tailArgs) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: msg, Error: err, }) } - console.Infoln(console.Message(msg)) + console.Infoln(msg) } // saveConfig writes configuration data in json format to config file. diff --git a/cmd-cp.go b/cmd-cp.go index dacc495d..fb9709dc 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -35,7 +35,7 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target } switch globalQuietFlag { case true: - console.Infoln(console.Message(fmt.Sprintf("‘%s’ -> ‘%s’", sourceURL, targetURL))) + console.Infoln(fmt.Sprintf("‘%s’ -> ‘%s’", sourceURL, targetURL)) default: // set up progress reader = bar.NewProxyReader(reader) @@ -121,7 +121,7 @@ func runCopyCmd(ctx *cli.Context) { } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) @@ -130,7 +130,7 @@ func runCopyCmd(ctx *cli.Context) { // extract URLs. URLs, err := args2URLs(ctx.Args()) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown URL types: ‘%s’", URLs), Error: iodine.New(err, nil), }) @@ -148,7 +148,7 @@ func runCopyCmd(ctx *cli.Context) { } for err := range doCopyCmd(sourceURLs, targetURL, bar) { if err != nil { - console.Errorln(console.ErrorMessage{ + console.Errorln(ErrorMessage{ Message: "Failed with", Error: iodine.New(err, nil), }) diff --git a/cmd-diff.go b/cmd-diff.go index 611610e3..b64ebc2e 100644 --- a/cmd-diff.go +++ b/cmd-diff.go @@ -31,14 +31,14 @@ func runDiffCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) } config, err := getMcConfig() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), Error: err, }) @@ -51,12 +51,12 @@ func runDiffCmd(ctx *cli.Context) { if err != nil { switch iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", firstURL), Error: err, }) default: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", firstURL), Error: err, }) @@ -65,7 +65,7 @@ func runDiffCmd(ctx *cli.Context) { _, err = getHostConfig(firstURL) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read host configuration for ‘%s’ from config file ‘%s’", firstURL, mustGetMcConfigPath()), Error: err, }) @@ -73,7 +73,7 @@ func runDiffCmd(ctx *cli.Context) { _, err = getHostConfig(secondURL) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read host configuration for ‘%s’ from config file ‘%s’", secondURL, mustGetMcConfigPath()), Error: err, }) @@ -83,12 +83,12 @@ func runDiffCmd(ctx *cli.Context) { if err != nil { switch iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", secondURL), Error: err, }) default: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", secondURL), Error: err, }) @@ -98,12 +98,12 @@ func runDiffCmd(ctx *cli.Context) { newFirstURL := stripRecursiveURL(firstURL) for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { if diff.err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: diff.message, Error: diff.err, }) } - console.Infoln(console.Message(diff.message)) + console.Infoln(diff.message) } } diff --git a/cmd-ls.go b/cmd-ls.go index ecceff6c..d82d5987 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -31,14 +31,14 @@ func runListCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) } config, err := getMcConfig() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), Error: iodine.New(err, nil), }) @@ -49,12 +49,12 @@ func runListCmd(ctx *cli.Context) { if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), Error: iodine.New(e, nil), }) default: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), Error: iodine.New(err, nil), }) @@ -62,7 +62,7 @@ func runListCmd(ctx *cli.Context) { } targetConfig, err := getHostConfig(targetURL) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read host configuration for ‘%s’ from config file ‘%s’", targetURL, mustGetMcConfigPath()), Error: iodine.New(err, nil), }) @@ -74,7 +74,7 @@ func runListCmd(ctx *cli.Context) { newTargetURL := stripRecursiveURL(targetURL) err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL)) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Failed to list ‘%s’", targetURL), Error: iodine.New(err, nil), }) diff --git a/cmd-mb.go b/cmd-mb.go index 58cfe7b3..1d332239 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -33,14 +33,14 @@ func runMakeBucketCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: errors.New("\"mc\" is not configured"), }) } config, err := getMcConfig() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Unable to read config file ‘" + mustGetMcConfigPath() + "’", Error: err, }) @@ -51,12 +51,12 @@ func runMakeBucketCmd(ctx *cli.Context) { if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), Error: e, }) default: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), Error: err, }) @@ -64,7 +64,7 @@ func runMakeBucketCmd(ctx *cli.Context) { } targetConfig, err := getHostConfig(targetURL) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read host configuration for ‘%s’ from config file ‘%s’", targetURL, mustGetMcConfigPath()), Error: err, }) @@ -74,7 +74,7 @@ func runMakeBucketCmd(ctx *cli.Context) { for targetURL, targetConfig := range targetURLConfigMap { errorMsg, err := doMakeBucketCmd(targetURL, targetConfig) if err != nil { - console.Errorln(console.ErrorMessage{ + console.Errorln(ErrorMessage{ Message: errorMsg, Error: err, }) diff --git a/cmd-sync.go b/cmd-sync.go index e7d5714c..04b9f42d 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -33,7 +33,7 @@ func runSyncCmd(ctx *cli.Context) { } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) @@ -41,7 +41,7 @@ func runSyncCmd(ctx *cli.Context) { URLs, err := args2URLs(ctx.Args()) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unknown URL types found: ‘%s’", URLs), Error: iodine.New(err, nil), }) @@ -76,7 +76,7 @@ func runSyncCmd(ctx *cli.Context) { for syncURLs := range prepareSyncURLs(sourceURL, targetURLs) { if syncURLs.Error != nil { - console.Errorln(console.ErrorMessage{ + console.Errorln(ErrorMessage{ Message: "Failed with", Error: iodine.New(syncURLs.Error, nil), }) @@ -88,7 +88,7 @@ func runSyncCmd(ctx *cli.Context) { defer wg.Done() srcConfig, err := getHostConfig(syncURLs.SourceContent.Name) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Failed with", Error: iodine.New(err, nil), }) @@ -96,14 +96,14 @@ func runSyncCmd(ctx *cli.Context) { } tgtConfig, err := getHostConfig(syncURLs.TargetContent.Name) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Failed with", Error: iodine.New(err, nil), }) return } if err := doCopy(syncURLs.SourceContent.Name, srcConfig, syncURLs.TargetContent.Name, tgtConfig, bar); err != nil { - console.Errorln(console.ErrorMessage{ + console.Errorln(ErrorMessage{ Message: "Failed with", Error: iodine.New(err, nil), }) diff --git a/cmd-update.go b/cmd-update.go index e0ed84da..fcff4a5f 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -48,7 +48,7 @@ func doUpdateCheck(config *hostConfig) (string, error) { printUpdateNotify("new", "old") return "", nil } - return "You have latest build", nil + return "You are already running the most recent version of ‘mc’", nil } @@ -58,24 +58,24 @@ func runUpdateCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code } if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) } hostConfig, err := getHostConfig(mcUpdateURL) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read configuration for host ‘%s’", mcUpdateURL), Error: iodine.New(err, nil), }) } msg, err := doUpdateCheck(hostConfig) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: msg, Error: iodine.New(err, nil), }) } - console.Infoln(console.Message(msg)) + console.Infoln(msg) } diff --git a/diff.go b/diff.go index e7206add..eea853bb 100644 --- a/diff.go +++ b/diff.go @@ -119,13 +119,13 @@ func dodiffdirs(firstClnt client.Client, firstURL, secondURL string, recursive b switch { case errFirst != nil && errSecond == nil: ch <- diff{ - message: "Only in ‘" + secondURL + "’", + message: "‘" + newSecondURL + "’ Only in ‘" + secondURL + "’", err: nil, } continue case errFirst == nil && errSecond != nil: ch <- diff{ - message: "Only in ‘" + firstURL + "’", + message: "‘" + newFirstURL + "’ Only in ‘" + firstURL + "’", err: nil, } continue diff --git a/globals.go b/globals.go index b72f205c..08e7867d 100644 --- a/globals.go +++ b/globals.go @@ -45,3 +45,17 @@ 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 5be353d0..7f1c4be0 100644 --- a/ls.go +++ b/ls.go @@ -17,6 +17,7 @@ package main import ( + "fmt" "runtime" "strings" @@ -33,28 +34,47 @@ const ( printDate = "2006-01-02 15:04:05 MST" ) -// printContent prints content meta-data -func printContent(c *client.Content) { - content := console.Content{} +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 +} + +func parseContent(c *client.Content) Content { + content := Content{} + content.Time = c.Time.Local().Format(printDate) content.Filetype = func() string { if c.Type.IsDir() { return "inode/directory" } - if c.Type.IsRegular() { - return "application/octet-stream" - } return "application/octet-stream" }() content.Size = humanize.IBytes(uint64(c.Size)) - switch { - case runtime.GOOS == "windows": - content.Name = strings.Replace(c.Name, "/", "\\", -1) - content.Name = strings.TrimSuffix(content.Name, "\\") - default: - content.Name = strings.TrimSuffix(c.Name, "/") - } - content.Time = c.Time.Local().Format(printDate) - console.ContentInfo(content) + content.Name = func() string { + switch { + case runtime.GOOS == "windows": + c.Name = strings.Replace(c.Name, "/", "\\", -1) + c.Name = strings.TrimSuffix(c.Name, "\\") + default: + c.Name = strings.TrimSuffix(c.Name, "/") + } + if c.Type.IsDir() { + switch { + case runtime.GOOS == "windows": + return fmt.Sprintf("%s\\", c.Name) + default: + return fmt.Sprintf("%s/", c.Name) + } + } + return c.Name + }() + return content } // doList - list all entities inside a folder @@ -74,7 +94,8 @@ func doList(clnt client.Client, targetURL string, recursive bool) error { contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/") } contentCh.Content.Name = contentName - printContent(contentCh.Content) + content := parseContent(contentCh.Content) + console.Println(content) } if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) diff --git a/main.go b/main.go index 17556a0d..cad933bc 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ import ( func checkConfig() { _, err := user.Current() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Unable to determine current user", Error: err, }) @@ -49,7 +49,7 @@ func checkConfig() { // Ensures config file is sane _, err = getMcConfig() if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Unable to read config file: %s", mustGetMcConfigPath()), Error: err, }) @@ -120,14 +120,14 @@ func main() { themeName := ctx.GlobalString("theme") switch { case console.IsValidTheme(themeName) != true: - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Please choose from this list: %s.", console.GetThemeNames()), Error: fmt.Errorf("Theme ‘%s’ is not supported.", themeName), }) default: err := console.SetTheme(themeName) if err != nil { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: fmt.Sprintf("Failed to set theme ‘%s’.", themeName), Error: err, }) @@ -138,7 +138,7 @@ func main() { } app.After = func(ctx *cli.Context) error { if !isMcConfigExist() { - console.Fatalln(console.ErrorMessage{ + console.Fatalln(ErrorMessage{ Message: "Please run \"mc config generate\"", Error: iodine.New(errors.New("\"mc\" is not configured"), nil), }) diff --git a/pkg/console/console.go b/pkg/console/console.go index 621de220..900b6034 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -20,12 +20,12 @@ import ( "encoding/json" "fmt" "os" - "runtime" "sync" "path/filepath" "github.com/fatih/color" + "github.com/fatih/structs" "github.com/minio/minio/pkg/iodine" "github.com/shiena/ansicolor" ) @@ -36,23 +36,6 @@ var NoDebugPrint = true // NoJsonPrint defines if the input should be printed in json formatted or not. By default it's set to true. var NoJSONPrint = true -// Message info string -type Message string - -// ErrorMessage error message structure -type ErrorMessage struct { - Message string `json:"Message"` - Error error `json:"Reason"` -} - -// Content content message structure -type Content struct { - Filetype string `json:"ContentType"` - Time string `json:"LastModified"` - Size string `json:"Size"` - Name string `json:"Name"` -} - // Theme holds console color scheme type Theme struct { Fatal *color.Color @@ -65,6 +48,17 @@ type Theme struct { Dir *color.Color Retry *color.Color JSON *color.Color + Print *color.Color +} + +func readErrordata(data interface{}) (string, error) { + st := structs.New(data) + if st.IsZero() { + return "", nil + } + msg := st.Field("Message") + msgErr := st.Field("Error") + return msg.Value().(string), msgErr.Value().(error) } var ( @@ -91,93 +85,114 @@ var ( }() // Print prints a message - Print = themesDB[currThemeName].Info.Print + Print = func(data interface{}) { + if NoJSONPrint { + print(themesDB[currThemeName].Print, data) + return + } + printBytes, _ := json.Marshal(&data) + print(themesDB[currThemeName].JSON, string(printBytes)) + } + + // Println prints a message with a newline + Println = func(data interface{}) { + if NoJSONPrint { + println(themesDB[currThemeName].Print, data) + return + } + printBytes, _ := json.Marshal(&data) + println(themesDB[currThemeName].JSON, string(printBytes)) + } // Fatal prints a error message and exits - Fatal = func(msg ErrorMessage) { + Fatal = func(data interface{}) { defer os.Exit(1) - if msg.Error != nil { + msg, err := readErrordata(data) + if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(msg.Error).Error() - message := msg.Message + ", " + reason + reason := "Reason: " + iodine.ToError(err).Error() + message := msg + ", " + reason print(themesDB[currThemeName].Error, message) if !NoDebugPrint { - print(themesDB[currThemeName].Error, msg.Error) + print(themesDB[currThemeName].Error, err) } return } - errorMessageBytes, _ := json.Marshal(&msg) + errorMessageBytes, _ := json.Marshal(&data) print(themesDB[currThemeName].JSON, string(errorMessageBytes)) } } // Fatalln prints a error message with a new line and exits - Fatalln = func(msg ErrorMessage) { + Fatalln = func(data interface{}) { defer os.Exit(1) - if msg.Error != nil { + msg, err := readErrordata(data) + if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(msg.Error).Error() - message := msg.Message + ", " + reason + reason := "Reason: " + iodine.ToError(err).Error() + message := msg + ", " + reason println(themesDB[currThemeName].Error, message) if !NoDebugPrint { - println(themesDB[currThemeName].Error, msg.Error) + println(themesDB[currThemeName].Error, err) } return } - errorMessageBytes, _ := json.Marshal(&msg) + errorMessageBytes, _ := json.Marshal(&data) println(themesDB[currThemeName].JSON, string(errorMessageBytes)) } } // Error prints a error message - Error = func(msg ErrorMessage) { - if msg.Error != nil { + Error = func(data interface{}) { + msg, err := readErrordata(data) + if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(msg.Error).Error() - message := msg.Message + ", " + reason + reason := "Reason: " + iodine.ToError(err).Error() + message := msg + ", " + reason print(themesDB[currThemeName].Error, message) if !NoDebugPrint { - print(themesDB[currThemeName].Error, msg.Error) + print(themesDB[currThemeName].Error, err) } return } - errorMessageBytes, _ := json.Marshal(&msg) + errorMessageBytes, _ := json.Marshal(&data) print(themesDB[currThemeName].JSON, string(errorMessageBytes)) } } // Errorln prints a error message with a new line - Errorln = func(msg ErrorMessage) { - if msg.Error != nil { + Errorln = func(data interface{}) { + msg, err := readErrordata(data) + if err != nil { if NoJSONPrint { - reason := "Reason: " + iodine.ToError(msg.Error).Error() - message := msg.Message + ", " + reason + reason := "Reason: " + iodine.ToError(err).Error() + message := msg + ", " + reason println(themesDB[currThemeName].Error, message) if !NoDebugPrint { - println(themesDB[currThemeName].Error, msg.Error) + println(themesDB[currThemeName].Error, err) } return } - errorMessageBytes, _ := json.Marshal(&msg) + errorMessageBytes, _ := json.Marshal(&data) println(themesDB[currThemeName].JSON, string(errorMessageBytes)) } } // Info prints a informational message - Info = func(msg Message) { + Info = func(data interface{}) { if NoJSONPrint { - print(themesDB[currThemeName].Info, msg) + print(themesDB[currThemeName].Info, data) return } - infoBytes, _ := json.Marshal(&msg) + infoBytes, _ := json.Marshal(&data) print(themesDB[currThemeName].JSON, string(infoBytes)) } // Infoln prints a informational message with a new line - Infoln = func(msg Message) { + Infoln = func(data interface{}) { if NoJSONPrint { - println(themesDB[currThemeName].Info, msg) + println(themesDB[currThemeName].Info, data) return } - infoBytes, _ := json.Marshal(&msg) + infoBytes, _ := json.Marshal(&data) println(themesDB[currThemeName].JSON, string(infoBytes)) } @@ -195,22 +210,15 @@ var ( println(themesDB[currThemeName].Debug, a...) } } - // ContentInfo prints a structure Content - ContentInfo = func(c Content) { - if NoJSONPrint { - print(themesDB[currThemeName].Time, c.Time) - print(themesDB[currThemeName].Size, c.Size) - switch c.Filetype { - case "inode/directory": - println(themesDB[currThemeName].Dir, c.Name) - case "application/octet-stream": - println(themesDB[currThemeName].File, c.Name) - } - return - } - contentBytes, _ := json.Marshal(&c) - println(themesDB[currThemeName].JSON, string(contentBytes)) - } + + // Time helper to print Time theme + Time = themesDB[currThemeName].Time.SprintfFunc() + // Size helper to print Size theme + Size = themesDB[currThemeName].Size.SprintfFunc() + // File helper to print File theme + File = themesDB[currThemeName].File.SprintfFunc() + // Dir helper to print Dir theme + Dir = themesDB[currThemeName].Dir.SprintfFunc() // Retry prints a retry message Retry = func(a ...interface{}) { @@ -252,15 +260,6 @@ var ( c.Print(ProgramName() + ": ") c.Print(a...) mutex.Unlock() - // special cases only for Content where it requires custom formatting - case themesDB[currThemeName].Time: - mutex.Lock() - c.Print(fmt.Sprintf("[%s] ", a...)) - mutex.Unlock() - case themesDB[currThemeName].Size: - mutex.Lock() - c.Printf(fmt.Sprintf("%6s ", a...)) - mutex.Unlock() default: mutex.Lock() c.Print(a...) @@ -300,19 +299,6 @@ var ( c.Print(ProgramName() + ": ") c.Println(a...) mutex.Unlock() - case themesDB[currThemeName].Dir: - mutex.Lock() - switch { - case runtime.GOOS == "windows": - c.Printf("%s\\\n", a...) - default: - c.Printf("%s/\n", a...) - } - mutex.Unlock() - case themesDB[currThemeName].File: - mutex.Lock() - c.Println(a...) - mutex.Unlock() default: mutex.Lock() c.Println(a...) diff --git a/pkg/console/themes.go b/pkg/console/themes.go index d218f3e5..9fbb9762 100644 --- a/pkg/console/themes.go +++ b/pkg/console/themes.go @@ -30,6 +30,7 @@ var MiniTheme = Theme{ Time: (color.New(color.FgGreen)), Retry: (color.New(color.FgMagenta, color.Bold)), JSON: (color.New(color.FgWhite, color.Italic)), + Print: (color.New()), } // WhiteTheme - All white color theme @@ -44,6 +45,7 @@ var WhiteTheme = Theme{ Time: (color.New(color.FgWhite, color.Bold)), Retry: (color.New(color.FgWhite, color.Bold)), JSON: (color.New(color.FgWhite, color.Bold, color.Italic)), + Print: (color.New()), } // NoColorTheme - Disables color theme @@ -58,4 +60,5 @@ var NoColorTheme = Theme{ Time: (color.New()), Retry: (color.New()), JSON: (color.New()), + Print: (color.New()), }