From 4425378b37d171d589b7fc687f8b3e0ee50efe7f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 8 Mar 2017 01:48:45 -0800 Subject: [PATCH] mc: Code cleanup after running gosimple tool (#2065) --- cmd/accounting-reader.go | 17 +++++++++- cmd/client-fs.go | 68 ++++++++++++++++++---------------------- cmd/common-methods.go | 38 +++++++++++----------- cmd/cp-main.go | 8 +---- cmd/mirror-main.go | 21 ------------- cmd/rm-main.go | 2 +- cmd/status.go | 9 +----- 7 files changed, 69 insertions(+), 94 deletions(-) diff --git a/cmd/accounting-reader.go b/cmd/accounting-reader.go index ad3ce6e4..fd433bdd 100644 --- a/cmd/accounting-reader.go +++ b/cmd/accounting-reader.go @@ -17,9 +17,12 @@ package cmd import ( + "fmt" "sync" "sync/atomic" "time" + + "github.com/cheggaaa/pb" ) // accounter keeps tabs of ongoing data transfer information. @@ -51,7 +54,7 @@ func newAccounter(total int64) *accounter { // write calculate the final speed. func (a *accounter) write(current int64) float64 { - fromStart := time.Now().Sub(a.startTime) + fromStart := time.Since(a.startTime) currentFromStart := current - a.startValue if currentFromStart > 0 { speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second)) @@ -80,6 +83,18 @@ type accountStat struct { Speed float64 } +func (c accountStat) String() string { + speedBox := pb.Format(int64(c.Speed)).To(pb.U_BYTES).String() + if speedBox == "" { + speedBox = "0 MB" + } else { + speedBox = speedBox + "/s" + } + message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.Format(c.Total).To(pb.U_BYTES), + pb.Format(c.Transferred).To(pb.U_BYTES), speedBox) + return message +} + // Stat provides current stats captured. func (a *accounter) Stat() accountStat { var acntStat accountStat diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 8d4c8e7c..44fbfcdf 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -139,45 +139,39 @@ func (f *fsClient) Watch(params watchParams) (*watchObject, *probe.Error) { // Get fsnotify notifications for events and errors, and sent them // using eventChan and errorChan go func() { - for { - select { - case event, ok := <-neventChan: - if !ok { - return - } - if isIgnoredFile(event.Path()) { + for event := range neventChan { + if isIgnoredFile(event.Path()) { + continue + } + var i os.FileInfo + if IsPutEvent(event.Event()) { + // Look for any writes, send a response to indicate a full copy. + var e error + i, e = os.Stat(event.Path()) + if e != nil { + if os.IsNotExist(e) { + continue + } + errorChan <- probe.NewError(e) continue } - var i os.FileInfo - if IsPutEvent(event.Event()) { - // Look for any writes, send a response to indicate a full copy. - var e error - i, e = os.Stat(event.Path()) - if e != nil { - if os.IsNotExist(e) { - continue - } - errorChan <- probe.NewError(e) - continue - } - if i.IsDir() { - // we want files - continue - } - eventChan <- Event{ - Time: time.Now().Format(timeFormatFS), - Size: i.Size(), - Path: event.Path(), - Client: f, - Type: EventCreate, - } - } else if IsDeleteEvent(event.Event()) { - eventChan <- Event{ - Time: time.Now().Format(timeFormatFS), - Path: event.Path(), - Client: f, - Type: EventRemove, - } + if i.IsDir() { + // we want files + continue + } + eventChan <- Event{ + Time: time.Now().Format(timeFormatFS), + Size: i.Size(), + Path: event.Path(), + Client: f, + Type: EventCreate, + } + } else if IsDeleteEvent(event.Event()) { + eventChan <- Event{ + Time: time.Now().Format(timeFormatFS), + Path: event.Path(), + Client: f, + Type: EventRemove, } } } diff --git a/cmd/common-methods.go b/cmd/common-methods.go index 8823214b..1af4d6a1 100644 --- a/cmd/common-methods.go +++ b/cmd/common-methods.go @@ -42,26 +42,26 @@ func isAliasURLDir(aliasURL string) bool { if expandedURL == aliasURL { // This is an FS url, check if the url has a separator at the end return strings.HasSuffix(aliasURL, string(filepath.Separator)) - } else { - // This is an S3 url, then: - // *) If alias format is specified, return false - // *) If alias/bucket is specified, return true - // *) If alias/bucket/prefix, check if prefix has - // has a trailing slash. - pathURL := filepath.ToSlash(aliasURL) - fields := strings.Split(pathURL, "/") - switch len(fields) { - // Nothing or alias format - case 0, 1: - return false - // alias/bucket format - case 2: - return true - // alias/bucket/prefix format - default: - return strings.HasSuffix(pathURL, "/") - } } + + // This is an S3 url, then: + // *) If alias format is specified, return false + // *) If alias/bucket is specified, return true + // *) If alias/bucket/prefix, check if prefix has + // has a trailing slash. + pathURL := filepath.ToSlash(aliasURL) + fields := strings.Split(pathURL, "/") + switch len(fields) { + // Nothing or alias format + case 0, 1: + return false + // alias/bucket format + case 2: + return true + } // default case.. + + // alias/bucket/prefix format + return strings.HasSuffix(pathURL, "/") } // getSource gets a reader from URL. diff --git a/cmd/cp-main.go b/cmd/cp-main.go index b3a0ef22..d4d07fac 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -342,13 +342,7 @@ func doCopySession(session *sessionV8) { } } else { if !globalJSON && globalQuiet { - accntStat := accntReader.Stat() - cpStatMessage := copyStatMessage{ - Total: accntStat.Total, - Transferred: accntStat.Transferred, - Speed: accntStat.Speed, - } - console.Println(console.Colorize("Copy", cpStatMessage.String())) + console.Println(console.Colorize("Copy", accntReader.Stat().String())) } } } diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 88d31448..8f38d8f1 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -26,7 +26,6 @@ import ( "syscall" "time" - "github.com/cheggaaa/pb" "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/console" @@ -171,26 +170,6 @@ func (m mirrorMessage) JSON() string { return string(mirrorMessageBytes) } -// mirrorStatMessage container for mirror accounting message -type mirrorStatMessage struct { - Total int64 - Transferred int64 - Speed float64 -} - -// mirrorStatMessage mirror accounting message -func (c mirrorStatMessage) String() string { - speedBox := pb.Format(int64(c.Speed)).To(pb.U_BYTES).String() - if speedBox == "" { - speedBox = "0 MB" - } else { - speedBox = speedBox + "/s" - } - message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.Format(c.Total).To(pb.U_BYTES), - pb.Format(c.Transferred).To(pb.U_BYTES), speedBox) - return message -} - // doRemove - removes files on target. func (mj *mirrorJob) doRemove(sURLs URLs) URLs { if mj.isFake { diff --git a/cmd/rm-main.go b/cmd/rm-main.go index b2694c8f..e91e85dd 100644 --- a/cmd/rm-main.go +++ b/cmd/rm-main.go @@ -227,7 +227,7 @@ func removeRecursive(url string, isIncomplete bool, isFake bool, older int) erro if !isFake { sent := false - for sent == false { + for !sent { select { case contentCh <- content: sent = true diff --git a/cmd/status.go b/cmd/status.go index b4a9704d..fef5836b 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -158,14 +158,7 @@ func (qs *QuietStatus) Start() { // Finish displays the accounting summary func (qs *QuietStatus) Finish() { - accntStat := qs.accounter.Stat() - cpStatMessage := mirrorStatMessage{ - Total: accntStat.Total, - Transferred: accntStat.Transferred, - Speed: accntStat.Speed, - } - - console.Println(console.Colorize("Mirror", cpStatMessage.String())) + console.Println(console.Colorize("Mirror", qs.accounter.Stat().String())) } // Update is ignored for quietstatus