mc: Code cleanup after running gosimple tool (#2065)
This commit is contained in:
committed by
Anis Elleuch
parent
7fda78af40
commit
4425378b37
@@ -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
|
||||
|
||||
+31
-37
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-19
@@ -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.
|
||||
|
||||
+1
-7
@@ -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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-8
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user