From d4047ca8994cfac8df7460d7ea51156ffb128017 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 14 Jul 2015 11:01:05 -0700 Subject: [PATCH] Cleanup minio, remote struct printing for Info, Error and Fatal - simplifying overall error handling --- cmd-access.go | 33 +++++----------------------- cmd-cat.go | 29 +++++------------------- cmd-config.go | 10 ++------- cmd-cp.go | 25 +++++++++------------ cmd-diff.go | 50 ++++++++++-------------------------------- cmd-ls.go | 31 +++++--------------------- cmd-mb.go | 28 +++++------------------ cmd-session.go | 30 +++++-------------------- cmd-sync.go | 32 ++++++++++----------------- cmd-update.go | 10 ++------- cmd_test.go | 3 ++- config.go | 24 ++++++++++++++++++-- hostconfig.go | 10 +++++++++ ls.go | 10 ++------- main.go | 31 ++++++-------------------- pkg/console/console.go | 18 --------------- print-structs.go | 30 ------------------------- session-v2.go | 23 +++++-------------- 18 files changed, 112 insertions(+), 315 deletions(-) diff --git a/cmd-access.go b/cmd-access.go index 5b2507db..cb5430e6 100644 --- a/cmd-access.go +++ b/cmd-access.go @@ -65,47 +65,26 @@ func runAccessCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code } if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) - } - config, err := getMcConfig() - if err != nil { - console.Fatals(ErrorMessage{ - Message: "loading config file failed", - Error: iodine.New(err, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } + config := mustGetMcConfig() acl := bucketACL(ctx.Args().First()) if !acl.isValidBucketACL() { - console.Fatals(ErrorMessage{ - Message: "Valid types are [private, public, readonly].", - Error: iodine.New(errInvalidACL{acl: acl.String()}, nil), - }) + console.Fatalf("Valid types are [private, public, readonly]. %s\n", errInvalidACL{acl: acl.String()}) } for _, arg := range ctx.Args().Tail() { targetURL, err := getExpandedURL(arg, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), - Error: iodine.New(e, nil), - }) + console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to parse argument %s. %s\n", arg, err) } } msg, err := doUpdateAccessCmd(targetURL, acl) if err != nil { - console.Errors(ErrorMessage{ - Message: msg, - Error: iodine.New(err, nil), - }) + console.Fatalln(msg) } console.Infoln(msg) } diff --git a/cmd-cat.go b/cmd-cat.go index 4fba5066..5d075b24 100644 --- a/cmd-cat.go +++ b/cmd-cat.go @@ -17,7 +17,6 @@ package main import ( - "fmt" "io" "os" "syscall" @@ -66,41 +65,23 @@ func runCatCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code } if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) - } - config, err := getMcConfig() - if err != nil { - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), - Error: iodine.New(err, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } + config := mustGetMcConfig() // Convert arguments to URLs: expand alias, fix format... for _, arg := range ctx.Args() { sourceURL, err := getExpandedURL(arg, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), - Error: iodine.New(e, nil), - }) + console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to parse argument %s. %s\n", arg, err) } } errorMsg, err := doCatCmd(sourceURL) if err != nil { - console.Fatals(ErrorMessage{ - Message: errorMsg, - Error: iodine.New(err, nil), - }) + console.Fatalln(errorMsg) } } } diff --git a/cmd-config.go b/cmd-config.go index e1696bd1..21045ffe 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -65,17 +65,11 @@ func runConfigCmd(ctx *cli.Context) { arg := ctx.Args().First() tailArgs := ctx.Args().Tail() if len(tailArgs) > 2 { - console.Fatals(ErrorMessage{ - Message: "Incorrect number of arguments, please use \"mc config help\"", - Error: iodine.New(errInvalidArgument{}, nil), - }) + console.Fatalf("Incorrect number of arguments, please use \"mc config help\". %s", errInvalidArgument{}) } msg, err := doConfig(arg, tailArgs) if err != nil { - console.Fatals(ErrorMessage{ - Message: msg, - Error: iodine.New(err, nil), - }) + console.Fatalln(msg) } console.Infoln(msg) } diff --git a/cmd-cp.go b/cmd-cp.go index b3e2c4c0..2479d1ef 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -90,7 +90,7 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue chan bool, wg *sync.WaitGroup var newReader io.ReadCloser if globalQuietFlag || globalJSONFlag { - console.Infos(CopyMessage{ + console.Infoln(CopyMessage{ Source: cpURLs.SourceContent.Name, Target: cpURLs.TargetContent.Name, Length: cpURLs.SourceContent.Size, @@ -144,20 +144,14 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { break } if cpURLs.Error != nil { - console.Errors(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(cpURLs.Error, nil), - }) + console.Errorln(cpURLs.Error) break } jsonData, err := json.Marshal(cpURLs) if err != nil { session.Close() - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to marshal URLs to JSON for ‘%s’", cpURLs.SourceContent.Name), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to marshal URLs to JSON. %s\n", err) } fmt.Fprintln(dataFP, string(jsonData)) scanBar(cpURLs.SourceContent.Name) @@ -225,18 +219,19 @@ func runCopyCmd(ctx *cli.Context) { session := newSessionV2() defer session.Close() + var err error session.Header.CommandType = "cp" - session.Header.RootPath, _ = os.Getwd() + session.Header.RootPath, err = os.Getwd() + if err != nil { + session.Close() + console.Fatalf("Unable to get current working directory. %s\n", err) + } // extract URLs. - var err error session.Header.CommandArgs, err = args2URLs(ctx.Args()) if err != nil { session.Close() - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown URL types found: ‘%s’", ctx.Args()), - Error: iodine.New(err, nil), - }) + console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), err) } doCopyCmdSession(session) diff --git a/cmd-diff.go b/cmd-diff.go index 8489e772..6ff60bef 100644 --- a/cmd-diff.go +++ b/cmd-diff.go @@ -17,8 +17,6 @@ package main import ( - "fmt" - "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/iodine" @@ -59,65 +57,39 @@ func runDiffCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code } if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) - } - config, err := getMcConfig() - if err != nil { - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), - Error: iodine.New(err, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } + config := mustGetMcConfig() firstURL := ctx.Args().First() secondURL := ctx.Args()[1] + var err error firstURL, err = getExpandedURL(firstURL, config.Aliases) if err != nil { - switch iodine.ToError(err).(type) { + switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown type of URL ‘%s’", firstURL), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to parse argument ‘%s’", firstURL), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to parse argument %s. %s\n", firstURL, err) } } secondURL, err = getExpandedURL(secondURL, config.Aliases) if err != nil { - switch iodine.ToError(err).(type) { + switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown type of URL ‘%s’", secondURL), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to parse argument ‘%s’", secondURL), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to parse argument %s. %s\n", secondURL, err) } } if isURLRecursive(secondURL) { - console.Fatals(ErrorMessage{ - Message: "Second URL cannot be recursive, diff command is unidirectional", - Error: iodine.New(errInvalidArgument{}, nil), - }) + console.Fatalf("Second URL cannot be recursive. %s\n", errInvalidArgument{}) } newFirstURL := stripRecursiveURL(firstURL) for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { if diff.err != nil { - console.Fatals(ErrorMessage{ - Message: diff.message, - Error: iodine.New(diff.err, nil), - }) + console.Fatalln(diff.message) } console.Infoln(diff.message) } diff --git a/cmd-ls.go b/cmd-ls.go index 220d3e1d..da6b35ad 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -17,8 +17,6 @@ package main import ( - "fmt" - "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/iodine" @@ -84,43 +82,24 @@ func runListCmd(ctx *cli.Context) { } if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } - config, err := getMcConfig() - if err != nil { - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), - Error: iodine.New(err, nil), - }) - } - + config := mustGetMcConfig() for _, arg := range args { targetURL, err := getExpandedURL(arg, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), - Error: iodine.New(e, nil), - }) + console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to parse argument %s. %s\n", arg, err) } } // if recursive strip off the "..." newTargetURL := stripRecursiveURL(targetURL) err = doListCmd(newTargetURL, isURLRecursive(targetURL)) if err != nil { - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Failed to list ‘%s’", targetURL), - Error: iodine.New(err, map[string]string{"Target": targetURL}), - }) + console.Fatalln("Failed to list : %s. %s\n", targetURL, err) } } } diff --git a/cmd-mb.go b/cmd-mb.go index e8e437e8..673aed08 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -61,40 +61,22 @@ func runMakeBucketCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code } if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) - } - config, err := getMcConfig() - if err != nil { - console.Fatals(ErrorMessage{ - Message: "Unable to read config file ‘" + mustGetMcConfigPath() + "’", - Error: iodine.New(err, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } + config := mustGetMcConfig() for _, arg := range ctx.Args() { targetURL, err := getExpandedURL(arg, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), - Error: iodine.New(e, nil), - }) + console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to parse argument %s. %s\n", arg, err) } } msg, err := doMakeBucketCmd(targetURL) if err != nil { - console.Errors(ErrorMessage{ - Message: msg, - Error: iodine.New(err, nil), - }) + console.Fatalln(msg) } console.Infoln(msg) } diff --git a/cmd-session.go b/cmd-session.go index 22ba7279..09027435 100644 --- a/cmd-session.go +++ b/cmd-session.go @@ -108,10 +108,7 @@ func runSessionCmd(ctx *cli.Context) { } if !isSessionDirExists() { if err := createSessionDir(); err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to create session directory. %s\n", err) } } switch strings.TrimSpace(ctx.Args().First()) { @@ -119,10 +116,7 @@ func runSessionCmd(ctx *cli.Context) { case "list": err := listSessions() if err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Fatalln(err) } case "resume": if len(ctx.Args().Tail()) != 1 { @@ -136,26 +130,17 @@ func runSessionCmd(ctx *cli.Context) { _, err := os.Stat(getSessionFile(sid)) if err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(errInvalidSessionID{id: sid}, nil), - }) + console.Fatalln(errInvalidSessionID{id: sid}) } s, err := loadSessionV2(sid) if err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(errInvalidSessionID{id: sid}, nil), - }) + console.Fatalln(errInvalidSessionID{id: sid}) } savedCwd, err := os.Getwd() if err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Fatalln("Unable to verify your current working directory. %s\n", err) } if s.Header.RootPath != "" { // chdir to RootPath @@ -165,10 +150,7 @@ func runSessionCmd(ctx *cli.Context) { sessionExecute(s) err = s.Close() if err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Fatalln("Unable to close session file properly. %s\n", err) } // change dir back diff --git a/cmd-sync.go b/cmd-sync.go index f5934838..aacc5d07 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -28,7 +28,6 @@ import ( "github.com/minio/cli" "github.com/minio/mc/pkg/console" - "github.com/minio/minio/pkg/iodine" ) // Help message. @@ -84,10 +83,7 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, wg *sync.WaitGrou if !globalQuietFlag || !globalJSONFlag { bar.ErrorGet(int64(length)) } - console.Errors(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Errorf("Unable to read from source %s. %s\n", sURLs.SourceContent.Name, err) } var targetURLs []string @@ -97,7 +93,7 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, wg *sync.WaitGrou var newReader io.ReadCloser if globalQuietFlag || globalJSONFlag { - console.Infos(SyncMessage{ + console.Infoln(SyncMessage{ Source: sURLs.SourceContent.Name, Targets: targetURLs, }) @@ -113,6 +109,7 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, wg *sync.WaitGrou if !globalQuietFlag || !globalJSONFlag { bar.ErrorPut(int64(length)) } + console.Errorf("Unable to write to one or many destinations. %s\n", err) } } return nil @@ -147,20 +144,14 @@ func doPrepareSyncURLs(session *sessionV2, trapCh <-chan bool) { break } if sURLs.Error != nil { - console.Errors(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(sURLs.Error, nil), - }) + console.Errorln(sURLs.Error) break } jsonData, err := json.Marshal(sURLs) if err != nil { session.Close() - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to marshal URLs to JSON for ‘%s’", sURLs.SourceContent.Name), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to marshal URLs to JSON. %s\n", err) } fmt.Fprintln(dataFP, string(jsonData)) scanBar(sURLs.SourceContent.Name) @@ -227,18 +218,19 @@ func runSyncCmd(ctx *cli.Context) { session := newSessionV2() defer session.Close() + var err error session.Header.CommandType = "sync" - session.Header.RootPath, _ = os.Getwd() + session.Header.RootPath, err = os.Getwd() + if err != nil { + session.Close() + console.Fatalf("Unable to get current working directory. %s\n", err) + } // extract URLs. - var err error session.Header.CommandArgs, err = args2URLs(ctx.Args()) if err != nil { session.Close() - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unknown URL types found: ‘%s’", ctx.Args()), - Error: iodine.New(err, nil), - }) + console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), err) } doSyncCmdSession(session) diff --git a/cmd-update.go b/cmd-update.go index 82e7a0c9..e52a16ae 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -105,17 +105,11 @@ func runUpdateCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code } if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } msg, err := doUpdateCheck() if err != nil { - console.Fatals(ErrorMessage{ - Message: msg, - Error: iodine.New(err, nil), - }) + console.Fatalln(msg) } // no msg do not print one if msg != "" { diff --git a/cmd_test.go b/cmd_test.go index 5dc669be..bbf6f666 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -52,7 +52,6 @@ func (s *CmdTestSuite) SetUpSuite(c *C) { // For windows the path is slightly different. if runtime.GOOS == "windows" { customConfigDir = filepath.Join(tmpDir, mcConfigWindowsDir) - } else { customConfigDir = filepath.Join(tmpDir, mcConfigDir) } @@ -73,6 +72,8 @@ func (s *CmdTestSuite) TearDownSuite(c *C) { func (s *CmdTestSuite) TestGetNewClient(c *C) { _, err := getNewClient("http://example.com/bucket1", &hostConfig{}) c.Assert(err, IsNil) + _, err = getNewClient("https://example.com/bucket1", &hostConfig{}) + c.Assert(err, IsNil) _, err = getNewClient("C:\\Users\\Administrator\\MyDocuments", &hostConfig{}) c.Assert(err, IsNil) _, err = getNewClient("/usr/bin/pandoc", &hostConfig{}) diff --git a/config.go b/config.go index 24e20f87..4ec67b1c 100644 --- a/config.go +++ b/config.go @@ -50,7 +50,6 @@ func getMcConfigDir() (string, error) { if mcCustomConfigDir != "" { return mcCustomConfigDir, nil } - u, err := user.Current() if err != nil { return "", iodine.New(err, nil) @@ -97,7 +96,10 @@ func getMcConfigPath() (string, error) { // mustGetMcConfigPath - similar to getMcConfigPath, ignores errors func mustGetMcConfigPath() string { - p, _ := getMcConfigPath() + p, err := getMcConfigPath() + if err != nil { + console.Fatalf("Unable to determine default config path. %s\n", err) + } return p } @@ -132,6 +134,15 @@ func getMcConfig() (*configV1, error) { } +// mustGetMcConfig - reads configuration file and returns configs, exits on error +func mustGetMcConfig() *configV1 { + config, err := getMcConfig() + if err != nil { + console.Fatalf("Unable to retrieve mc configuration. %s\n", err) + } + return config +} + // isMcConfigExists xreturns err if config doesn't exist func isMcConfigExists() bool { configFile, err := getMcConfigPath() @@ -210,3 +221,12 @@ func newConfig() (config quick.Config, err error) { return config, nil } + +// mustNewConfig instantiates a new config handler, exists upon error +func mustNewConfig() quick.Config { + config, err := newConfig() + if err != nil { + console.Fatalf("Unable to instantiate a new config handler. %s\n", err) + } + return config +} diff --git a/hostconfig.go b/hostconfig.go index 20afdba6..32b587b2 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/minio/mc/pkg/client" + "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/iodine" ) @@ -69,3 +70,12 @@ func getHostConfig(URL string) (*hostConfig, error) { } return nil, iodine.New(errNoMatchingHost{}, nil) } + +// mustGetHostConfig retrieves host specific configuration such as access keys, exits upon error +func mustGetHostConfig(URL string) *hostConfig { + hostCfg, err := getHostConfig(URL) + if err != nil { + console.Fatalf("Unable to retrieve host configuration. %s\n", err) + } + return hostCfg +} diff --git a/ls.go b/ls.go index 49b77343..f39f3870 100644 --- a/ls.go +++ b/ls.go @@ -79,17 +79,11 @@ func doList(clnt client.Client, recursive bool) error { switch err := iodine.ToError(contentCh.Err).(type) { // handle this specifically for filesystem case client.ISBrokenSymlink: - console.Errors(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Errorf(err.Error()) continue } if os.IsNotExist(iodine.ToError(contentCh.Err)) || os.IsPermission(iodine.ToError(contentCh.Err)) { - console.Errors(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(contentCh.Err, nil), - }) + console.Errorf(contentCh.Err.Error()) continue } err = contentCh.Err diff --git a/main.go b/main.go index 466b84dc..175004be 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,6 @@ import ( "github.com/minio/cli" "github.com/minio/mc/pkg/console" - "github.com/minio/minio/pkg/iodine" "github.com/minio/pb" ) @@ -33,10 +32,7 @@ import ( func checkConfig() { _, err := user.Current() if err != nil { - console.Fatals(ErrorMessage{ - Message: "Unable to determine current user", - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to determine current user. %s\n", err) } // If config doesn't exist, do not attempt to read it @@ -47,10 +43,7 @@ func checkConfig() { // Ensures config file is sane _, err = getMcConfig() if err != nil { - console.Fatals(ErrorMessage{ - Message: fmt.Sprintf("Unable to read config file: %s", mustGetMcConfigPath()), - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to read config file. %s\n", err) } } @@ -59,7 +52,7 @@ func checkConfig() { func getSystemData() map[string]string { host, err := os.Hostname() if err != nil { - host = "" + console.Fatalf("Unable to determine hostname. %s\n", err) } memstats := &runtime.MemStats{} runtime.ReadMemStats(memstats) @@ -133,19 +126,12 @@ func main() { themeName := ctx.GlobalString("theme") switch { case console.IsValidTheme(themeName) != true: - err := iodine.New(errInvalidTheme{Theme: themeName}, nil) - console.Errors(ErrorMessage{ - Message: fmt.Sprintf("Please choose from this list: %s.", console.GetThemeNames()), - Error: iodine.New(err, nil), - }) - return err + console.Errorf("Invalid theme, please choose from the following list: %s.\n", console.GetThemeNames()) + return errInvalidTheme{Theme: themeName} default: err := console.SetTheme(themeName) if err != nil { - console.Errors(ErrorMessage{ - Message: fmt.Sprintf("Failed to set theme ‘%s’.", themeName), - Error: iodine.New(err, nil), - }) + console.Errorf("Failed to set theme ‘%s’.", themeName) return err } } @@ -154,10 +140,7 @@ func main() { } app.After = func(ctx *cli.Context) error { if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } return nil } diff --git a/pkg/console/console.go b/pkg/console/console.go index 89ab2a87..5c9c93cc 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -126,13 +126,6 @@ var ( return } - // Fatals print a error structure with a new line and exit - Fatals = func(data ...interface{}) { - defer os.Exit(1) - println(themesDB[currThemeName].Error, data...) - return - } - // Error prints a error message Error = func(data ...interface{}) { print(themesDB[currThemeName].Error, data...) @@ -151,11 +144,6 @@ var ( return } - // Errors print a error structure with a new line - Errors = func(data ...interface{}) { - println(themesDB[currThemeName].Error, data...) - } - // Info prints a informational message Info = func(data ...interface{}) { print(themesDB[currThemeName].Info, data...) @@ -174,12 +162,6 @@ var ( return } - // Infos print a informational structured message - Infos = func(data ...interface{}) { - println(themesDB[currThemeName].Info, data...) - return - } - // Debug prints a debug message without a new line // Debug prints a debug message Debug = func(data ...interface{}) { diff --git a/print-structs.go b/print-structs.go index 2352820a..5f3b9c47 100644 --- a/print-structs.go +++ b/print-structs.go @@ -22,38 +22,8 @@ import ( "fmt" "github.com/minio/mc/pkg/console" - "github.com/minio/minio/pkg/iodine" ) -// ErrorMessage container for error reason encapsulation -type ErrorMessage struct { - Message string `json:"-"` - Error error `json:"error"` -} - -// String string printer for error message -func (e ErrorMessage) String() string { - if !globalJSONFlag { - var message string - if e.Error != nil { - switch e.Error.(type) { - case iodine.Error: - reason := iodine.ToError(e.Error).Error() - message = reason - default: - reason := e.Error.Error() - message = reason - } - } - return message - } - eBytes, err := json.Marshal(iodine.ToError(e.Error)) - if err != nil { - panic(err) - } - return string(eBytes) -} - // Content container for content message structure type Content struct { Filetype string `json:"type"` diff --git a/session-v2.go b/session-v2.go index c0427a55..8ebc8141 100644 --- a/session-v2.go +++ b/session-v2.go @@ -65,18 +65,12 @@ type sessionV2 struct { func newSessionV2() *sessionV2 { // TODO: Blindly create .mc and session dirs at init and remove these checks -ab. if !isMcConfigExists() { - console.Fatals(ErrorMessage{ - Message: "Please run \"mc config generate\"", - Error: iodine.New(errNotConfigured{}, nil), - }) + console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) } if !isSessionDirExists() { if err := createSessionDir(); err != nil { - console.Fatals(ErrorMessage{ - Message: "Failed with", - Error: iodine.New(err, nil), - }) + console.Fatalf("Unable to create session directory. %s\n", err) } } @@ -91,12 +85,8 @@ func newSessionV2() *sessionV2 { var err error s.DataFP, err = os.Create(getSessionDataFile(s.SessionID)) if err != nil { - console.Fatals(ErrorMessage{ - Message: "Unable to create session data file \"" + getSessionDataFile(s.SessionID) + "\"", - Error: iodine.New(errNotConfigured{}, nil), - }) + console.Fatalf("Unable to create session data file \""+getSessionDataFile(s.SessionID)+"\". %s\n", err) } - return s } @@ -202,10 +192,7 @@ func loadSessionV2(sid string) (*sessionV2, error) { s.DataFP, err = os.Open(getSessionDataFile(s.SessionID)) if err != nil { - console.Fatals(ErrorMessage{ - Message: "Unable to open session data file \"" + getSessionDataFile(s.SessionID) + "\"", - Error: iodine.New(errNotConfigured{}, nil), - }) + console.Fatalf("Unable to open session data file \""+getSessionDataFile(s.SessionID)+"\". %s", errNotConfigured{}) } return s, nil @@ -218,7 +205,7 @@ func isCopiedFactory(lastCopied string) func(string) bool { copied := true // closure return func(sourceURL string) bool { if sourceURL == "" { - console.Fatalln("Empty source URL passed to isCopied() function. Please report this bug.") + console.Fatalf("Empty source URL passed to isCopied() function. %s\n", errUnexpected{}) } if lastCopied == "" { return false