From b35ab2f873279fc4c9d8b3b7c33722a0492cd03c Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sat, 16 May 2015 17:07:58 -0700 Subject: [PATCH] quiet mode support for copy and sync --- cmd-copy.go | 64 +++++++++++++++++++++++++++++++---------------------- cmd-sync.go | 33 ++++++++++----------------- cmd_test.go | 12 ---------- copy-bar.go | 4 ++-- 4 files changed, 52 insertions(+), 61 deletions(-) diff --git a/cmd-copy.go b/cmd-copy.go index 78b63755..009764e1 100644 --- a/cmd-copy.go +++ b/cmd-copy.go @@ -45,11 +45,15 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target var writers []io.Writer writers = append(writers, writeCloser) - // set up progress bar - writers = append(writers, bar) + if !globalQuietFlag { + // set up progress bar + writers = append(writers, bar) + } else { + console.Infof("ā€˜%s’ -> ā€˜%s’\n", sourceURL, targetURL) + } - // write progress bar multiWriter := io.MultiWriter(writers...) + // copy data to writers _, copyErr := io.CopyN(multiWriter, readCloser, int64(length)) // close to see the error, verify it later @@ -64,6 +68,20 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target 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 func runCopyCmd(ctx *cli.Context) { if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { @@ -72,32 +90,22 @@ func runCopyCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".") } - config, err := getMcConfig() + // extract URLs. + URLs, err := args2URLs(ctx.Args()) if err != nil { - console.Debugln(iodine.New(err, nil)) - 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) - } + console.Fatalln(iodine.ToError(err)) } // Separate source and target. 'cp' can take only one target, // 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] + targetURL := URLs[len(URLs)-1] // Last one is target + var bar barSend // set up progress bar - bar := newCopyBar(globalQuietFlag) + if !globalQuietFlag { + bar = newCopyBar() + } go func(sourceURLs []string, targetURL string) { for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { @@ -106,7 +114,9 @@ func runCopyCmd(ctx *cli.Context) { // will be printed later during Copy() continue } - bar.Extend(cpURLs.SourceContent.Size) + if !globalQuietFlag { + bar.Extend(cpURLs.SourceContent.Size) + } } }(sourceURLs, targetURL) @@ -120,7 +130,7 @@ func runCopyCmd(ctx *cli.Context) { } cpQueue <- true wg.Add(1) - go func(cpURLs copyURLs) { + go func(cpURLs *copyURLs, bar *barSend) { defer wg.Done() srcConfig, err := getHostConfig(cpURLs.SourceContent.Name) if err != nil { @@ -132,12 +142,14 @@ func runCopyCmd(ctx *cli.Context) { console.Errorln(iodine.ToError(err)) 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)) } <-cpQueue - }(*cpURLs) + }(cpURLs, &bar) } wg.Wait() - bar.Finish() + if !globalQuietFlag { + bar.Finish() + } } diff --git a/cmd-sync.go b/cmd-sync.go index 42d4f52a..e2b6654c 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -32,31 +32,18 @@ func runSyncCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatalln("\"mc\" is not configured. Please run \"mc config generate\".") } - config, err := getMcConfig() + URLs, err := args2URLs(ctx.Args()) if err != nil { - console.Debugln(iodine.New(err, nil)) - console.Fatalf("Unable to read config file [%s]. Reason: [%s].\n", mustGetMcConfigPath(), iodine.ToError(err)) + console.Fatalln(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. // but any number of targets, even the recursive URLs mixed in-between. sourceURL := URLs[0] // first one is source targetURLs := URLs[1:] // set up progress bar - bar := newCopyBar(globalQuietFlag) + bar := newCopyBar() go func(sourceURL string, targetURLs []string) { for syncURLs := range prepareSyncURLs(sourceURL, targetURLs) { @@ -65,7 +52,9 @@ func runSyncCmd(ctx *cli.Context) { // will be printed later during Sync() continue } - bar.Extend(syncURLs.SourceContent.Size) + if !globalQuietFlag { + bar.Extend(syncURLs.SourceContent.Size) + } } }(sourceURL, targetURLs) @@ -79,7 +68,7 @@ func runSyncCmd(ctx *cli.Context) { } syncQueue <- true wg.Add(1) - go func(syncURLs copyURLs) { + go func(syncURLs *copyURLs, bar *barSend) { defer wg.Done() srcConfig, err := getHostConfig(syncURLs.SourceContent.Name) if err != nil { @@ -91,12 +80,14 @@ func runSyncCmd(ctx *cli.Context) { console.Errorln(iodine.ToError(err)) 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)) } <-syncQueue - }(*syncURLs) + }(syncURLs, &bar) } wg.Wait() - bar.Finish() + if !globalQuietFlag { + bar.Finish() + } } diff --git a/cmd_test.go b/cmd_test.go index 376b50c6..6ce17604 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -196,18 +196,6 @@ func (ta *testAddr) String() string { 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) { opError := &net.OpError{ Op: "read", diff --git a/copy-bar.go b/copy-bar.go index 4e7245c4..2e222cfb 100644 --- a/copy-bar.go +++ b/copy-bar.go @@ -63,8 +63,8 @@ func (b barSend) Finish() { <-b.finishCh } -// newCopyBar - instantiate a copyBar. When 'Quiet' is set to true, it CopyBar only prints text message for each item. -func newCopyBar(quiet bool) barSend { +// newCopyBar - instantiate a copyBar. +func newCopyBar() barSend { cmdCh := make(chan barMsg) finishCh := make(chan bool)