MCPcopy
hub / github.com/peak/s5cmd / planRun

Method planRun

command/sync.go:439–531  ·  view source on GitHub ↗

planRun prepares the commands and writes them to writer 'w'.

(
	c *cli.Context,
	onlySource, onlyDest chan *url.URL,
	common chan *ObjectPair,
	dsturl *url.URL,
	strategy SyncStrategy,
	w io.WriteCloser,
	isBatch bool,
)

Source from the content-addressed store, hash-verified

437
438// planRun prepares the commands and writes them to writer 'w'.
439func (s Sync) planRun(
440 c *cli.Context,
441 onlySource, onlyDest chan *url.URL,
442 common chan *ObjectPair,
443 dsturl *url.URL,
444 strategy SyncStrategy,
445 w io.WriteCloser,
446 isBatch bool,
447) {
448 defer w.Close()
449
450 // Always use raw mode since sync command generates commands
451 // from raw S3 objects. Otherwise, generated copy command will
452 // try to expand given source.
453 defaultFlags := map[string]interface{}{
454 "raw": true,
455 }
456
457 // it should wait until both of the child goroutines for onlySource and common channels
458 // are completed before closing the WriteCloser w to ensure that all URLs are processed.
459 var wg sync.WaitGroup
460
461 // only in source
462 wg.Add(1)
463 go func() {
464 defer wg.Done()
465 for srcurl := range onlySource {
466 curDestURL := generateDestinationURL(srcurl, dsturl, isBatch)
467 command, err := generateCommand(c, "cp", defaultFlags, srcurl, curDestURL)
468 if err != nil {
469 printDebug(s.op, err, srcurl, curDestURL)
470 continue
471 }
472 fmt.Fprintln(w, command)
473 }
474 }()
475
476 // both in source and destination
477 wg.Add(1)
478 go func() {
479 defer wg.Done()
480 for commonObject := range common {
481 sourceObject, destObject := commonObject.src, commonObject.dst
482 curSourceURL, curDestURL := sourceObject.URL, destObject.URL
483 err := strategy.ShouldSync(sourceObject, destObject) // check if object should be copied.
484 if err != nil {
485 printDebug(s.op, err, curSourceURL, curDestURL)
486 continue
487 }
488
489 command, err := generateCommand(c, "cp", defaultFlags, curSourceURL, curDestURL)
490 if err != nil {
491 printDebug(s.op, err, curSourceURL, curDestURL)
492 continue
493 }
494 fmt.Fprintln(w, command)
495 }
496 }()

Callers 1

RunMethod · 0.95

Calls 6

generateDestinationURLFunction · 0.85
generateCommandFunction · 0.85
printDebugFunction · 0.85
CloseMethod · 0.80
WaitMethod · 0.80
ShouldSyncMethod · 0.65

Tested by

no test coverage detected