1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00

quiet mode support for copy and sync

This commit is contained in:
Anand Babu (AB) Periasamy
2015-05-16 17:07:58 -07:00
committed by Harshavardhana
parent a2b8f053b0
commit b35ab2f873
4 changed files with 52 additions and 61 deletions

View File

@@ -45,11 +45,15 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target
var writers []io.Writer var writers []io.Writer
writers = append(writers, writeCloser) writers = append(writers, writeCloser)
// set up progress bar if !globalQuietFlag {
writers = append(writers, bar) // set up progress bar
writers = append(writers, bar)
} else {
console.Infof("%s -> %s\n", sourceURL, targetURL)
}
// write progress bar
multiWriter := io.MultiWriter(writers...) multiWriter := io.MultiWriter(writers...)
// copy data to writers // copy data to writers
_, copyErr := io.CopyN(multiWriter, readCloser, int64(length)) _, copyErr := io.CopyN(multiWriter, readCloser, int64(length))
// close to see the error, verify it later // close to see the error, verify it later
@@ -64,6 +68,20 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target
return nil return nil
} }
// args2URLs extracts source and target URLs from command-line args.
func args2URLs(args cli.Args) ([]string, error) {
config, err := getMcConfig()
if err != nil {
return nil, iodine.New(err, nil)
}
// Convert arguments to URLs: expand alias, fix format...
URLs, err := getExpandedURLs(args, config.Aliases)
if err != nil {
return nil, iodine.New(err, nil)
}
return URLs, nil
}
// runCopyCmd is bound to sub-command // runCopyCmd is bound to sub-command
func runCopyCmd(ctx *cli.Context) { func runCopyCmd(ctx *cli.Context) {
if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { if len(ctx.Args()) < 2 || ctx.Args().First() == "help" {
@@ -72,32 +90,22 @@ func runCopyCmd(ctx *cli.Context) {
if !isMcConfigExist() { if !isMcConfigExist() {
console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".") console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".")
} }
config, err := getMcConfig() // extract URLs.
URLs, err := args2URLs(ctx.Args())
if err != nil { if err != nil {
console.Debugln(iodine.New(err, nil)) console.Fatalln(iodine.ToError(err))
console.Fatalf("Unable to read config file [%s]. Reason: [%s].\n", mustGetMcConfigPath(), iodine.ToError(err))
}
// Convert arguments to URLs: expand alias, fix format...
URLs, err := getExpandedURLs(ctx.Args(), config.Aliases)
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unknown type of URL(s).\n")
default:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to parse arguments. Reason: [%s].\n", e)
}
} }
// Separate source and target. 'cp' can take only one target, // Separate source and target. 'cp' can take only one target,
// but any number of sources, even the recursive URLs mixed in-between. // but any number of sources, even the recursive URLs mixed in-between.
targetURL := URLs[len(URLs)-1] // Last one is target
sourceURLs := URLs[:len(URLs)-1] sourceURLs := URLs[:len(URLs)-1]
targetURL := URLs[len(URLs)-1] // Last one is target
var bar barSend
// set up progress bar // set up progress bar
bar := newCopyBar(globalQuietFlag) if !globalQuietFlag {
bar = newCopyBar()
}
go func(sourceURLs []string, targetURL string) { go func(sourceURLs []string, targetURL string) {
for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) {
@@ -106,7 +114,9 @@ func runCopyCmd(ctx *cli.Context) {
// will be printed later during Copy() // will be printed later during Copy()
continue continue
} }
bar.Extend(cpURLs.SourceContent.Size) if !globalQuietFlag {
bar.Extend(cpURLs.SourceContent.Size)
}
} }
}(sourceURLs, targetURL) }(sourceURLs, targetURL)
@@ -120,7 +130,7 @@ func runCopyCmd(ctx *cli.Context) {
} }
cpQueue <- true cpQueue <- true
wg.Add(1) wg.Add(1)
go func(cpURLs copyURLs) { go func(cpURLs *copyURLs, bar *barSend) {
defer wg.Done() defer wg.Done()
srcConfig, err := getHostConfig(cpURLs.SourceContent.Name) srcConfig, err := getHostConfig(cpURLs.SourceContent.Name)
if err != nil { if err != nil {
@@ -132,12 +142,14 @@ func runCopyCmd(ctx *cli.Context) {
console.Errorln(iodine.ToError(err)) console.Errorln(iodine.ToError(err))
return return
} }
if err := doCopy(cpURLs.SourceContent.Name, srcConfig, cpURLs.TargetContent.Name, tgtConfig, &bar); err != nil { if err := doCopy(cpURLs.SourceContent.Name, srcConfig, cpURLs.TargetContent.Name, tgtConfig, bar); err != nil {
console.Errorln(iodine.ToError(err)) console.Errorln(iodine.ToError(err))
} }
<-cpQueue <-cpQueue
}(*cpURLs) }(cpURLs, &bar)
} }
wg.Wait() wg.Wait()
bar.Finish() if !globalQuietFlag {
bar.Finish()
}
} }

View File

@@ -32,31 +32,18 @@ func runSyncCmd(ctx *cli.Context) {
if !isMcConfigExist() { if !isMcConfigExist() {
console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".") console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".")
} }
config, err := getMcConfig() URLs, err := args2URLs(ctx.Args())
if err != nil { if err != nil {
console.Debugln(iodine.New(err, nil)) console.Fatalln(iodine.ToError(err))
console.Fatalf("Unable to read config file [%s]. Reason: [%s].\n", mustGetMcConfigPath(), iodine.ToError(err))
} }
// Convert arguments to URLs: expand alias, fix format...
URLs, err := getExpandedURLs(ctx.Args(), config.Aliases)
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unknown type of URL(s).\n")
default:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to parse arguments. Reason: [%s].\n", e)
}
}
// Separate source and target. 'sync' can take only one source. // Separate source and target. 'sync' can take only one source.
// but any number of targets, even the recursive URLs mixed in-between. // but any number of targets, even the recursive URLs mixed in-between.
sourceURL := URLs[0] // first one is source sourceURL := URLs[0] // first one is source
targetURLs := URLs[1:] targetURLs := URLs[1:]
// set up progress bar // set up progress bar
bar := newCopyBar(globalQuietFlag) bar := newCopyBar()
go func(sourceURL string, targetURLs []string) { go func(sourceURL string, targetURLs []string) {
for syncURLs := range prepareSyncURLs(sourceURL, targetURLs) { for syncURLs := range prepareSyncURLs(sourceURL, targetURLs) {
@@ -65,7 +52,9 @@ func runSyncCmd(ctx *cli.Context) {
// will be printed later during Sync() // will be printed later during Sync()
continue continue
} }
bar.Extend(syncURLs.SourceContent.Size) if !globalQuietFlag {
bar.Extend(syncURLs.SourceContent.Size)
}
} }
}(sourceURL, targetURLs) }(sourceURL, targetURLs)
@@ -79,7 +68,7 @@ func runSyncCmd(ctx *cli.Context) {
} }
syncQueue <- true syncQueue <- true
wg.Add(1) wg.Add(1)
go func(syncURLs copyURLs) { go func(syncURLs *copyURLs, bar *barSend) {
defer wg.Done() defer wg.Done()
srcConfig, err := getHostConfig(syncURLs.SourceContent.Name) srcConfig, err := getHostConfig(syncURLs.SourceContent.Name)
if err != nil { if err != nil {
@@ -91,12 +80,14 @@ func runSyncCmd(ctx *cli.Context) {
console.Errorln(iodine.ToError(err)) console.Errorln(iodine.ToError(err))
return return
} }
if err := doCopy(syncURLs.SourceContent.Name, srcConfig, syncURLs.TargetContent.Name, tgtConfig, &bar); err != nil { if err := doCopy(syncURLs.SourceContent.Name, srcConfig, syncURLs.TargetContent.Name, tgtConfig, bar); err != nil {
console.Errorln(iodine.ToError(err)) console.Errorln(iodine.ToError(err))
} }
<-syncQueue <-syncQueue
}(*syncURLs) }(syncURLs, &bar)
} }
wg.Wait() wg.Wait()
bar.Finish() if !globalQuietFlag {
bar.Finish()
}
} }

View File

@@ -196,18 +196,6 @@ func (ta *testAddr) String() string {
return "testAddr" return "testAddr"
} }
func (s *CmdTestSuite) TestStatusBar(c *C) {
/* TODO: Fix after finishing copyBar wrapper around progress bar.
bar := newCopyBar(globalQuietFlag)
c.Assert(bar, Not(IsNil))
c.Assert(bar.Units, Equals, pb.U_BYTES)
c.Assert(bar.RefreshRate, Equals, time.Millisecond*10)
c.Assert(bar.NotPrint, Equals, true)
c.Assert(bar.ShowSpeed, Equals, true)
*/
}
func (s *CmdTestSuite) TestIsValidRetry(c *C) { func (s *CmdTestSuite) TestIsValidRetry(c *C) {
opError := &net.OpError{ opError := &net.OpError{
Op: "read", Op: "read",

View File

@@ -63,8 +63,8 @@ func (b barSend) Finish() {
<-b.finishCh <-b.finishCh
} }
// newCopyBar - instantiate a copyBar. When 'Quiet' is set to true, it CopyBar only prints text message for each item. // newCopyBar - instantiate a copyBar.
func newCopyBar(quiet bool) barSend { func newCopyBar() barSend {
cmdCh := make(chan barMsg) cmdCh := make(chan barMsg)
finishCh := make(chan bool) finishCh := make(chan bool)