From 4b88ad7786eda3ea80f7089d67a66a265e309ab4 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 22 Jun 2015 15:21:36 -0700 Subject: [PATCH 1/2] Move to structure printing everywhere, fix incorrect iodine usage at few places --- cmd-config.go | 4 +--- cmd-cp.go | 17 +++++++++++++---- cmd-diff.go | 14 +++++++------- cmd-ls.go | 4 ++-- cmd-session.go | 6 +++--- cmd-sync.go | 12 +++++++++--- cmd-update.go | 5 ++--- ls.go | 14 ++++++++++---- main.go | 8 ++++---- pkg/console/console.go | 2 +- print-structs.go | 12 ++++++++++++ 11 files changed, 64 insertions(+), 34 deletions(-) diff --git a/cmd-config.go b/cmd-config.go index 768abf71..e03953ac 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -74,7 +74,7 @@ func runConfigCmd(ctx *cli.Context) { if err != nil { console.Fatals(ErrorMessage{ Message: msg, - Error: err, + Error: iodine.New(err, nil), }) } console.Infos(InfoMessage{ @@ -89,12 +89,10 @@ func saveConfig(arg string, aliases []string) error { if isMcConfigExist() { return iodine.New(errConfigExists{}, nil) } - config, err := newConfig() if err != nil { return iodine.New(err, nil) } - err = writeConfig(config) if err != nil { return iodine.New(err, nil) diff --git a/cmd-cp.go b/cmd-cp.go index a4684596..15d871ab 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -95,7 +95,10 @@ func doCopySession(cURLs cpURLs, bar *barSend, s *sessionV1) error { var newReader io.Reader switch globalQuietFlag { case true: - console.Infoln(fmt.Sprintf("‘%s’ -> ‘%s’", cURLs.SourceContent.Name, cURLs.TargetContent.Name)) + console.Infos(CopyMessage{ + Source: cURLs.SourceContent.Name, + Target: cURLs.TargetContent.Name, + }) newReader = yielder.NewReader(reader) default: // set up progress @@ -226,13 +229,19 @@ func runCopyCmd(ctx *cli.Context) { } if !isSessionDirExists() { if err := createSessionDir(); err != nil { - console.Fatalln(iodine.ToError(err)) + console.Fatals(ErrorMessage{ + Message: "Failed with", + Error: iodine.New(err, nil), + }) } } s, err := newSession() if err != nil { - console.Fatalln(iodine.ToError(err)) + console.Fatals(ErrorMessage{ + Message: "Failed with", + Error: iodine.New(err, nil), + }) } s.CommandType = "cp" @@ -262,7 +271,7 @@ func runCopyCmd(ctx *cli.Context) { if err := saveSession(s); err != nil { console.Fatals(ErrorMessage{ Message: "Failed with", - Error: iodine.ToError(err), + Error: iodine.New(err, nil), }) } console.Infos(InfoMessage{ diff --git a/cmd-diff.go b/cmd-diff.go index f6f05d49..0a69bb84 100644 --- a/cmd-diff.go +++ b/cmd-diff.go @@ -68,7 +68,7 @@ func runDiffCmd(ctx *cli.Context) { if err != nil { console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unable to read config file ‘%s’", mustGetMcConfigPath()), - Error: err, + Error: iodine.New(err, nil), }) } @@ -81,12 +81,12 @@ func runDiffCmd(ctx *cli.Context) { case errUnsupportedScheme: console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", firstURL), - Error: err, + Error: iodine.New(err, nil), }) default: console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", firstURL), - Error: err, + Error: iodine.New(err, nil), }) } } @@ -96,19 +96,19 @@ func runDiffCmd(ctx *cli.Context) { case errUnsupportedScheme: console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", secondURL), - Error: err, + Error: iodine.New(err, nil), }) default: console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", secondURL), - Error: err, + Error: iodine.New(err, nil), }) } } if isURLRecursive(secondURL) { console.Fatals(ErrorMessage{ Message: "Second URL cannot be recursive, diff command is unidirectional", - Error: errInvalidArgument{}, + Error: iodine.New(errInvalidArgument{}, nil), }) } newFirstURL := stripRecursiveURL(firstURL) @@ -116,7 +116,7 @@ func runDiffCmd(ctx *cli.Context) { if diff.err != nil { console.Fatals(ErrorMessage{ Message: diff.message, - Error: diff.err, + Error: iodine.New(diff.err, nil), }) } console.Infos(InfoMessage{ diff --git a/cmd-ls.go b/cmd-ls.go index c667409b..1897df07 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -119,7 +119,7 @@ func runListCmd(ctx *cli.Context) { if err != nil { console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Failed to list ‘%s’", targetURL), - Error: iodine.New(err, nil), + Error: iodine.New(err, map[string]string{"Target": targetURL}), }) } } @@ -133,7 +133,7 @@ func doListCmd(targetURL string, recursive bool) error { } err = doList(clnt, recursive) if err != nil { - return iodine.New(err, nil) + return iodine.New(err, map[string]string{"Target": targetURL}) } return nil } diff --git a/cmd-session.go b/cmd-session.go index 820b4255..feafc6e7 100644 --- a/cmd-session.go +++ b/cmd-session.go @@ -73,7 +73,7 @@ func listSessions(sdir string) error { if err != nil { return iodine.New(err, nil) } - console.Println(s) + console.Prints(s) } } return nil @@ -140,7 +140,7 @@ func sessionExecute(bar barSend, s *sessionV1) { if err := saveSession(s); err != nil { console.Fatals(ErrorMessage{ Message: "Failed with", - Error: iodine.ToError(err), + Error: iodine.New(err, nil), }) } os.Exit(0) @@ -158,7 +158,7 @@ func sessionExecute(bar barSend, s *sessionV1) { if err := saveSession(s); err != nil { console.Fatals(ErrorMessage{ Message: "Failed with", - Error: iodine.ToError(err), + Error: iodine.New(err, nil), }) } // this os.Exit is needed really to exit in-case of "os.Interrupt" diff --git a/cmd-sync.go b/cmd-sync.go index 1627eff7..da4915b0 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -222,13 +222,19 @@ func runSyncCmd(ctx *cli.Context) { if !isSessionDirExists() { if err := createSessionDir(); err != nil { - console.Fatalln(iodine.ToError(err)) + console.Fatals(ErrorMessage{ + Message: "Failed with", + Error: iodine.New(err, nil), + }) } } s, err := newSession() if err != nil { - console.Fatalln(iodine.ToError(err)) + console.Fatals(ErrorMessage{ + Message: "Failed with", + Error: iodine.New(err, nil), + }) } s.CommandType = "sync" @@ -258,7 +264,7 @@ func runSyncCmd(ctx *cli.Context) { if err := saveSession(s); err != nil { console.Fatals(ErrorMessage{ Message: "Failed wtih", - Error: iodine.ToError(err), + Error: iodine.New(err, nil), }) } console.Infos(InfoMessage{ diff --git a/cmd-update.go b/cmd-update.go index 915d1166..57cb282f 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -18,7 +18,6 @@ package main import ( "encoding/json" - "fmt" "net/http" "runtime" "time" @@ -93,7 +92,7 @@ https://dl.minio.io:9000 for continuous updates` if err != nil { return "", err } - fmt.Println(msg) + console.Prints(InfoMessage{Message: msg}) return "", nil } return "You are already running the most recent version of ‘mc’", nil @@ -120,6 +119,6 @@ func runUpdateCmd(ctx *cli.Context) { } // no msg do not print one if msg != "" { - console.Infoln(msg) + console.Infos(InfoMessage{Message: msg}) } } diff --git a/ls.go b/ls.go index df96a3f0..47f77d5b 100644 --- a/ls.go +++ b/ls.go @@ -77,20 +77,26 @@ func doList(clnt client.Client, recursive bool) error { var err error for contentCh := range clnt.List(recursive) { if contentCh.Err != nil { - switch e := iodine.ToError(contentCh.Err).(type) { + switch err := iodine.ToError(contentCh.Err).(type) { // handle this specifically for filesystem case client.ISBrokenSymlink: - console.Errorln(e) + console.Errors(ErrorMessage{ + Message: "Failed with", + Error: iodine.New(err, nil), + }) continue } if os.IsNotExist(iodine.ToError(contentCh.Err)) || os.IsPermission(iodine.ToError(contentCh.Err)) { - console.Errorln(iodine.ToError(contentCh.Err)) + console.Errors(ErrorMessage{ + Message: "Failed with", + Error: iodine.New(contentCh.Err, nil), + }) continue } err = contentCh.Err break } - console.Println(parseContent(contentCh.Content)) + console.Prints(parseContent(contentCh.Content)) } if err != nil { return iodine.New(err, map[string]string{"Target": clnt.URL().String()}) diff --git a/main.go b/main.go index 4a907465..c34bbdc1 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func checkConfig() { if err != nil { console.Fatals(ErrorMessage{ Message: "Unable to determine current user", - Error: err, + Error: iodine.New(err, nil), }) } @@ -49,7 +49,7 @@ func checkConfig() { if err != nil { console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unable to read config file: %s", mustGetMcConfigPath()), - Error: err, + Error: iodine.New(err, nil), }) } } @@ -126,7 +126,7 @@ func main() { err := iodine.New(errInvalidTheme{Theme: themeName}, nil) console.Errors(ErrorMessage{ Message: fmt.Sprintf("Please choose from this list: %s.", console.GetThemeNames()), - Error: err, + Error: iodine.New(err, nil), }) return err default: @@ -134,7 +134,7 @@ func main() { if err != nil { console.Errors(ErrorMessage{ Message: fmt.Sprintf("Failed to set theme ‘%s’.", themeName), - Error: err, + Error: iodine.New(err, nil), }) return err } diff --git a/pkg/console/console.go b/pkg/console/console.go index ae2b7880..85952749 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -128,7 +128,7 @@ var ( Prints = func(data ...interface{}) { for i := 0; i < len(data); i++ { if NoJSONPrint { - println(themesDB[currThemeName].Info, data[i]) + println(themesDB[currThemeName].Print, data[i]) return } infoBytes, _ := json.Marshal(&data[i]) diff --git a/print-structs.go b/print-structs.go index cb88e1ed..065eb44d 100644 --- a/print-structs.go +++ b/print-structs.go @@ -18,6 +18,8 @@ package main import ( + "fmt" + "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/iodine" ) @@ -71,3 +73,13 @@ type InfoMessage struct { func (i InfoMessage) String() string { return i.Message } + +type CopyMessage struct { + Message string `json:"message"` + Source string `json:"source"` + Target string `json:"target"` +} + +func (c CopyMessage) String() string { + return fmt.Sprintf("‘%s’ -> ‘%s’", c.Source, c.Target) +} From 9b901bac21c5111e222873a53badb72b18ca54a5 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 22 Jun 2015 15:56:58 -0700 Subject: [PATCH 2/2] Fix sync bug when in TypeA and TypeB sync style source doesn't exist --- cmd-sync.go | 9 +++++++++ cp-url.go | 7 +++++++ print-structs.go | 8 ++++++++ sync-url.go | 24 ++++++++++++++++++++++-- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/cmd-sync.go b/cmd-sync.go index da4915b0..71552c18 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -23,6 +23,7 @@ import ( "os" "os/signal" "runtime" + "strings" "sync" "github.com/minio/cli" @@ -71,9 +72,13 @@ EXAMPLES: // doSyncSession - Sync an object to multiple destination func doSyncSession(sURLs syncURLs, bar *barSend, syncQueue chan bool, ssCh chan syncSession, wg *sync.WaitGroup, s *sessionV1) { + // waitgroup reply deferred until this function returns defer wg.Done() + + // hold lock for map updates inside session s.Lock.Lock() defer s.Lock.Unlock() + if !globalQuietFlag { bar.SetCaption(sURLs.SourceContent.Name + ": ") } @@ -105,6 +110,10 @@ func doSyncSession(sURLs syncURLs, bar *barSend, syncQueue chan bool, ssCh chan var newReader io.Reader switch globalQuietFlag { case true: + console.Infos(SyncMessage{ + Source: sURLs.SourceContent.Name, + Target: strings.Join(targetURLs, " "), + }) newReader = yielder.NewReader(reader) default: // set up progress diff --git a/cp-url.go b/cp-url.go index d3c71788..3cc69963 100644 --- a/cp-url.go +++ b/cp-url.go @@ -53,6 +53,13 @@ type cpURLs struct { Error error } +func (c cpURLs) IsEmpty() bool { + if c.SourceContent == nil && c.TargetContent == nil { + return true + } + return false +} + type cpURLsType uint8 const ( diff --git a/print-structs.go b/print-structs.go index 065eb44d..b81c2119 100644 --- a/print-structs.go +++ b/print-structs.go @@ -74,6 +74,7 @@ func (i InfoMessage) String() string { return i.Message } +// CopyMessage container for file copy messages type CopyMessage struct { Message string `json:"message"` Source string `json:"source"` @@ -83,3 +84,10 @@ type CopyMessage struct { func (c CopyMessage) String() string { return fmt.Sprintf("‘%s’ -> ‘%s’", c.Source, c.Target) } + +// SyncMessage container for file sync messages, inherits CopyMessage +type SyncMessage CopyMessage + +func (s SyncMessage) String() string { + return fmt.Sprintf("‘%s’ -> ‘%s’", s.Source, s.Target) +} diff --git a/sync-url.go b/sync-url.go index 7fd82351..efd10c16 100644 --- a/sync-url.go +++ b/sync-url.go @@ -52,6 +52,22 @@ type syncURLs struct { Error error } +func (s syncURLs) IsEmpty() bool { + empty := false + if s.SourceContent == nil { + empty = true + if s.TargetContents == nil { + empty = true + return empty + } + if len(s.TargetContents) > 0 && s.TargetContents[0] == nil { + empty = true + return empty + } + } + return empty +} + type syncURLsType cpURLsType // guessSyncURLType guesses the type of URL. This approach all allows prepareURL @@ -91,7 +107,9 @@ func prepareSyncURLsTypeA(sourceURL string, targetURLs []string) <-chan syncURLs sURLs.SourceContent = cURLs.SourceContent sURLs.TargetContents = append(sURLs.TargetContents, cURLs.TargetContent) } - syncURLsCh <- sURLs + if !sURLs.IsEmpty() { + syncURLsCh <- sURLs + } }() return syncURLsCh } @@ -113,7 +131,9 @@ func prepareSyncURLsTypeB(sourceURL string, targetURLs []string) <-chan syncURLs sURLs.SourceContent = cURLs.SourceContent sURLs.TargetContents = append(sURLs.TargetContents, cURLs.TargetContent) } - syncURLsCh <- sURLs + if !sURLs.IsEmpty() { + syncURLsCh <- sURLs + } }() return syncURLsCh }