Rename cast to mirror
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
mb Make a bucket or folder
|
||||
cat Display contents of a file
|
||||
cp Copy files and folders from many sources to a single destination
|
||||
cast Copy files and folders from a single source to many destinations
|
||||
mirror Copy files and folders from a single source to many destinations
|
||||
diff Compute differences between two files or folders
|
||||
access Set access permissions
|
||||
config Add new alias to default configuration file [~/.mc/config.json]
|
||||
|
||||
@@ -56,30 +56,30 @@ func (s *CmdTestSuite) TestCopyURLType(c *C) {
|
||||
func (s *CmdTestSuite) TestCastURLType(c *C) {
|
||||
sourceURL := server.URL + "/bucket"
|
||||
targetURLs := []string{}
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, castURLsTypeInvalid)
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
|
||||
|
||||
sourceURL = server.URL + "/bucket"
|
||||
targetURLs = nil
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, castURLsTypeInvalid)
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
|
||||
|
||||
sourceURL = ""
|
||||
targetURLs = []string{server.URL + "/bucket/object_new"}
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, castURLsTypeInvalid)
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
|
||||
|
||||
sourceURL = server.URL + "/bucket..."
|
||||
targetURLs = []string{server.URL + "/bucket"}
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, castURLsTypeC)
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeC)
|
||||
|
||||
sourceURL = server.URL + "/bucket/object1"
|
||||
targetURLs = []string{server.URL + "/bucket"}
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, castURLsTypeB)
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeB)
|
||||
|
||||
sourceURL = server.URL + "/bucket/object1"
|
||||
targetURLs = []string{server.URL + "/bucket/object_new"}
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, castURLsTypeA)
|
||||
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeA)
|
||||
}
|
||||
|
||||
// TODO fix both copy and cast
|
||||
// TODO fix both copy and mirror
|
||||
func (s *CmdTestSuite) TestCopyContext(c *C) {
|
||||
err := app.Run([]string{os.Args[0], "cp", server.URL + "/bucket...", server.URL + "/bucket"})
|
||||
c.Assert(err, IsNil)
|
||||
@@ -93,11 +93,11 @@ func (s *CmdTestSuite) TestCopyContext(c *C) {
|
||||
}
|
||||
|
||||
func (s *CmdTestSuite) TestCastContext(c *C) {
|
||||
err := app.Run([]string{os.Args[0], "cast", server.URL + "/bucket...", server.URL + "/bucket"})
|
||||
err := app.Run([]string{os.Args[0], "mirror", server.URL + "/bucket...", server.URL + "/bucket"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, true)
|
||||
|
||||
err = app.Run([]string{os.Args[0], "cast", server.URL + "/invalid...", server.URL + "/bucket"})
|
||||
err = app.Run([]string{os.Args[0], "mirror", server.URL + "/invalid...", server.URL + "/bucket"})
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(console.IsExited, Equals, true)
|
||||
// reset back
|
||||
@@ -1,26 +0,0 @@
|
||||
#### cast
|
||||
|
||||
```go
|
||||
NAME:
|
||||
mc cast - Copy files and folders from a single source to many destinations
|
||||
|
||||
USAGE:
|
||||
mc cast SOURCE TARGET [TARGET...]
|
||||
|
||||
EXAMPLES:
|
||||
1. Cast an object from local filesystem to Amazon S3 cloud storage.
|
||||
$ mc cast star-trek-episode-10-season4.ogg https://s3.amazonaws.com/trekarchive
|
||||
|
||||
2. Cast a bucket recursively from Minio cloud storage to multiple buckets on Amazon S3 cloud storage.
|
||||
$ mc cast https://play.minio.io:9000/photos/2014... https://s3.amazonaws.com/backup-photos https://s3.amazonaws.com/my-photos
|
||||
|
||||
3. Cast a local folder recursively to Minio cloud storage and Amazon S3 cloud storage.
|
||||
$ mc cast backup/... https://play.minio.io:9000/archive https://s3.amazonaws.com/archive
|
||||
|
||||
4. Cast a bucket from aliased Amazon S3 cloud storage to multiple folders on Windows.
|
||||
$ mc cast s3:documents/2014/... C:\backup\2014 C:\shared\volume\backup\2014
|
||||
|
||||
5. Cast a local file of non english character to Amazon s3 cloud storage.
|
||||
$ mc cast 本語/... s3:mylocaldocuments C:\backup\2014 play:backup
|
||||
|
||||
```
|
||||
@@ -0,0 +1,26 @@
|
||||
#### mirror
|
||||
|
||||
```go
|
||||
NAME:
|
||||
mc mirror - Copy files and folders from a single source to many destinations
|
||||
|
||||
USAGE:
|
||||
mc mirror SOURCE TARGET [TARGET...]
|
||||
|
||||
EXAMPLES:
|
||||
1. Cast an object from local filesystem to Amazon S3 cloud storage.
|
||||
$ mc mirror star-trek-episode-10-season4.ogg https://s3.amazonaws.com/trekarchive
|
||||
|
||||
2. Cast a bucket recursively from Minio cloud storage to multiple buckets on Amazon S3 cloud storage.
|
||||
$ mc mirror https://play.minio.io:9000/photos/2014... https://s3.amazonaws.com/backup-photos https://s3.amazonaws.com/my-photos
|
||||
|
||||
3. Cast a local folder recursively to Minio cloud storage and Amazon S3 cloud storage.
|
||||
$ mc mirror backup/... https://play.minio.io:9000/archive https://s3.amazonaws.com/archive
|
||||
|
||||
4. Cast a bucket from aliased Amazon S3 cloud storage to multiple folders on Windows.
|
||||
$ mc mirror s3:documents/2014/... C:\backup\2014 C:\shared\volume\backup\2014
|
||||
|
||||
5. Cast a local file of non english character to Amazon s3 cloud storage.
|
||||
$ mc mirror 本語/... s3:mylocaldocuments C:\backup\2014 play:backup
|
||||
|
||||
```
|
||||
@@ -110,8 +110,8 @@ func registerApp() *cli.App {
|
||||
registerCmd(mbCmd) // make a bucket
|
||||
registerCmd(catCmd) // concantenate an object to standard output
|
||||
registerCmd(cpCmd) // copy objects and files from multiple sources to single destination
|
||||
registerCmd(castCmd) // cast objects and files from single source to multiple destinations
|
||||
registerCmd(sessionCmd) // session handling for resuming copy and cast operations
|
||||
registerCmd(mirrorCmd) // mirror objects and files from single source to multiple destinations
|
||||
registerCmd(sessionCmd) // session handling for resuming copy and mirror operations
|
||||
registerCmd(diffCmd) // compare two objects
|
||||
registerCmd(accessCmd) // set permissions [public, private, readonly, authenticated] for buckets and folders.
|
||||
registerCmd(configCmd) // generate configuration "/home/harsha/.mc/config.json" file.
|
||||
|
||||
@@ -32,8 +32,8 @@ import (
|
||||
)
|
||||
|
||||
// Help message.
|
||||
var castCmd = cli.Command{
|
||||
Name: "cast",
|
||||
var mirrorCmd = cli.Command{
|
||||
Name: "mirror",
|
||||
Usage: "Copy files and folders from a single source to many destinations",
|
||||
Action: runCastCmd,
|
||||
CustomHelpTemplate: `NAME:
|
||||
@@ -50,29 +50,26 @@ FLAGS:
|
||||
{{end}}{{ end }}
|
||||
|
||||
EXAMPLES:
|
||||
1. Cast an object from local filesystem to Amazon S3 cloud storage.
|
||||
$ mc {{.Name}} star-trek-episode-10-season4.ogg https://s3.amazonaws.com/trekarchive
|
||||
|
||||
2. Cast a bucket recursively from Minio cloud storage to multiple buckets on Amazon S3 cloud storage.
|
||||
1. Cast a bucket recursively from Minio cloud storage to multiple buckets on Amazon S3 cloud storage.
|
||||
$ mc {{.Name}} https://play.minio.io:9000/photos/2014... https://s3.amazonaws.com/backup-photos https://s3-west-1.amazonaws.com/local-photos
|
||||
|
||||
3. Cast a local folder recursively to Minio cloud storage and Amazon S3 cloud storage.
|
||||
2. Cast a local folder recursively to Minio cloud storage and Amazon S3 cloud storage.
|
||||
$ mc {{.Name}} backup/... https://play.minio.io:9000/archive https://s3.amazonaws.com/archive
|
||||
|
||||
4. Cast a bucket from aliased Amazon S3 cloud storage to multiple folders on Windows.
|
||||
3. Cast a bucket from aliased Amazon S3 cloud storage to multiple folders on Windows.
|
||||
$ mc {{.Name}} s3:documents/2014/... C:\backup\2014 C:\shared\volume\backup\2014
|
||||
|
||||
5. Cast a local folder of non english character recursively to Amazon s3 cloud storage and Minio cloud storage.
|
||||
4. Cast a local folder of non english character recursively to Amazon s3 cloud storage and Minio cloud storage.
|
||||
$ mc {{.Name}} 本語/... s3:mylocaldocuments play:backup
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// doCast - Cast an object to multiple destination. castURLs status contains a copy of sURLs and error if any.
|
||||
func doCast(sURLs castURLs, bar *barSend, castQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- castURLs) {
|
||||
// doCast - Cast an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
|
||||
func doCast(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
|
||||
defer wg.Done() // Notify that this copy routine is done.
|
||||
defer func() {
|
||||
<-castQueueCh
|
||||
<-mirrorQueueCh
|
||||
}()
|
||||
|
||||
if sURLs.Error != nil { // Errorneous sURLs passed.
|
||||
@@ -127,14 +124,14 @@ func doCast(sURLs castURLs, bar *barSend, castQueueCh <-chan bool, wg *sync.Wait
|
||||
statusCh <- sURLs
|
||||
}
|
||||
|
||||
// doCastFake - Perform a fake cast to update the progress bar appropriately.
|
||||
func doCastFake(sURLs castURLs, bar *barSend) {
|
||||
// doCastFake - Perform a fake mirror to update the progress bar appropriately.
|
||||
func doCastFake(sURLs mirrorURLs, bar *barSend) {
|
||||
if !globalDebugFlag || !globalJSONFlag {
|
||||
bar.Progress(sURLs.SourceContent.Size)
|
||||
}
|
||||
}
|
||||
|
||||
// doPrepareCastURLs scans the source URL and prepares a list of objects for casting.
|
||||
// doPrepareCastURLs scans the source URL and prepares a list of objects for mirroring.
|
||||
func doPrepareCastURLs(session *sessionV2, trapCh <-chan bool) {
|
||||
sourceURL := session.Header.CommandArgs[0] // first one is source.
|
||||
targetURLs := session.Header.CommandArgs[1:]
|
||||
@@ -200,11 +197,11 @@ func doCastCmdSession(session *sessionV2) {
|
||||
isCopied := isCopiedFactory(session.Header.LastCopied)
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
// Limit numner of cast routines based on available CPU resources.
|
||||
castQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1)))
|
||||
defer close(castQueue)
|
||||
// Status channel for receiveing cast return status.
|
||||
statusCh := make(chan castURLs)
|
||||
// Limit numner of mirror routines based on available CPU resources.
|
||||
mirrorQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1)))
|
||||
defer close(mirrorQueue)
|
||||
// Status channel for receiveing mirror return status.
|
||||
statusCh := make(chan mirrorURLs)
|
||||
|
||||
// Go routine to monitor doCast status and signal traps.
|
||||
wg.Add(1)
|
||||
@@ -220,7 +217,7 @@ func doCastCmdSession(session *sessionV2) {
|
||||
if sURLs.Error == nil {
|
||||
session.Header.LastCopied = sURLs.SourceContent.Name
|
||||
} else {
|
||||
console.Errorf("Failed to cast ‘%s’, %s\n", sURLs.SourceContent.Name, NewIodine(sURLs.Error))
|
||||
console.Errorf("Failed to mirror ‘%s’, %s\n", sURLs.SourceContent.Name, NewIodine(sURLs.Error))
|
||||
}
|
||||
case <-trapCh: // Receive interrupt notification.
|
||||
session.Close()
|
||||
@@ -230,30 +227,30 @@ func doCastCmdSession(session *sessionV2) {
|
||||
}
|
||||
}()
|
||||
|
||||
// Go routine to perform concurrently casting.
|
||||
// Go routine to perform concurrently mirroring.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
castWg := new(sync.WaitGroup)
|
||||
mirrorWg := new(sync.WaitGroup)
|
||||
defer close(statusCh)
|
||||
|
||||
for scanner.Scan() {
|
||||
var sURLs castURLs
|
||||
var sURLs mirrorURLs
|
||||
json.Unmarshal([]byte(scanner.Text()), &sURLs)
|
||||
if isCopied(sURLs.SourceContent.Name) {
|
||||
doCastFake(sURLs, &bar)
|
||||
} else {
|
||||
// Wait for other cast routines to
|
||||
// Wait for other mirror routines to
|
||||
// complete. We only have limited CPU
|
||||
// and network resources.
|
||||
castQueue <- true
|
||||
// Account for each cast routines we start.
|
||||
castWg.Add(1)
|
||||
// Do casting in background concurrently.
|
||||
go doCast(sURLs, &bar, castQueue, castWg, statusCh)
|
||||
mirrorQueue <- true
|
||||
// Account for each mirror routines we start.
|
||||
mirrorWg.Add(1)
|
||||
// Do mirroring in background concurrently.
|
||||
go doCast(sURLs, &bar, mirrorQueue, mirrorWg, statusCh)
|
||||
}
|
||||
}
|
||||
castWg.Wait()
|
||||
mirrorWg.Wait()
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
@@ -265,7 +262,7 @@ func runCastCmd(ctx *cli.Context) {
|
||||
session := newSessionV2()
|
||||
|
||||
var err error
|
||||
session.Header.CommandType = "cast"
|
||||
session.Header.CommandType = "mirror"
|
||||
session.Header.RootPath, err = os.Getwd()
|
||||
if err != nil {
|
||||
session.Close()
|
||||
+70
-70
@@ -26,33 +26,33 @@ import (
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
)
|
||||
|
||||
type castURLs struct {
|
||||
type mirrorURLs struct {
|
||||
SourceContent *client.Content
|
||||
TargetContents []*client.Content
|
||||
Error error `json:"-"`
|
||||
}
|
||||
|
||||
type castURLsType uint8
|
||||
type mirrorURLsType uint8
|
||||
|
||||
const (
|
||||
castURLsTypeInvalid castURLsType = iota
|
||||
castURLsTypeA
|
||||
castURLsTypeB
|
||||
castURLsTypeC
|
||||
mirrorURLsTypeInvalid mirrorURLsType = iota
|
||||
mirrorURLsTypeA
|
||||
mirrorURLsTypeB
|
||||
mirrorURLsTypeC
|
||||
)
|
||||
|
||||
// NOTE: All the parse rules should reduced to A: Cast(Source, []Target).
|
||||
//
|
||||
// * CAST ARGS - VALID CASES
|
||||
// =========================
|
||||
// A: cast(f, []f) -> cast(f, []f)
|
||||
// B: cast(f, [](d | f)) -> cast(f, [](d/f | f)) -> A:
|
||||
// C: cast(d1..., [](d2)) -> []cast(d1/f, [](d2/d1/f)) -> []A:
|
||||
// A: mirror(f, []f) -> mirror(f, []f)
|
||||
// B: mirror(f, [](d | f)) -> mirror(f, [](d/f | f)) -> A:
|
||||
// C: mirror(d1..., [](d2)) -> []mirror(d1/f, [](d2/d1/f)) -> []A:
|
||||
|
||||
// checkCastSyntax(URLs []string)
|
||||
func checkCastSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) < 2 || ctx.Args().First() == "help" {
|
||||
cli.ShowCommandHelpAndExit(ctx, "cast", 1) // last argument is exit code.
|
||||
cli.ShowCommandHelpAndExit(ctx, "mirror", 1) // last argument is exit code.
|
||||
}
|
||||
// extract URLs.
|
||||
URLs, err := args2URLs(ctx.Args())
|
||||
@@ -98,20 +98,20 @@ func checkCastSyntax(ctx *cli.Context) {
|
||||
}
|
||||
|
||||
switch guessCastURLType(srcURL, tgtURLs) {
|
||||
case castURLsTypeA: // File -> File.
|
||||
case mirrorURLsTypeA: // File -> File.
|
||||
checkCastSyntaxTypeA(srcURL, tgtURLs)
|
||||
case castURLsTypeB: // File -> Folder.
|
||||
case mirrorURLsTypeB: // File -> Folder.
|
||||
checkCastSyntaxTypeB(srcURL, tgtURLs)
|
||||
case castURLsTypeC: // Folder -> Folder.
|
||||
case mirrorURLsTypeC: // Folder -> Folder.
|
||||
checkCastSyntaxTypeC(srcURL, tgtURLs)
|
||||
default:
|
||||
console.Fatalln("Invalid arguments. Unable to determine how to cast. Please report this issue at https://github.com/minio/mc/issues")
|
||||
console.Fatalln("Invalid arguments. Unable to determine how to mirror. Please report this issue at https://github.com/minio/mc/issues")
|
||||
}
|
||||
}
|
||||
|
||||
func checkCastSyntaxTypeA(srcURL string, tgtURLs []string) {
|
||||
if len(tgtURLs) == 0 && tgtURLs == nil {
|
||||
console.Fatalf("Invalid number of target arguments to cast command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
}
|
||||
_, srcContent, err := url2Stat(srcURL)
|
||||
// Source exist?.
|
||||
@@ -119,7 +119,7 @@ func checkCastSyntaxTypeA(srcURL string, tgtURLs []string) {
|
||||
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, NewIodine(iodine.New(err, nil)))
|
||||
}
|
||||
if srcContent.Type.IsDir() {
|
||||
console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to cast this folder and its contents recursively. %s\n", srcURL, srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to mirror this folder and its contents recursively. %s\n", srcURL, srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
}
|
||||
if !srcContent.Type.IsRegular() {
|
||||
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
@@ -137,7 +137,7 @@ func checkCastSyntaxTypeA(srcURL string, tgtURLs []string) {
|
||||
|
||||
func checkCastSyntaxTypeB(srcURL string, tgtURLs []string) {
|
||||
if len(tgtURLs) == 0 && tgtURLs == nil {
|
||||
console.Fatalf("Invalid number of target arguments to cast command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
}
|
||||
_, srcContent, err := url2Stat(srcURL)
|
||||
// Source exist?.
|
||||
@@ -145,7 +145,7 @@ func checkCastSyntaxTypeB(srcURL string, tgtURLs []string) {
|
||||
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, NewIodine(iodine.New(err, nil)))
|
||||
}
|
||||
if srcContent.Type.IsDir() {
|
||||
console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to cast this folder and its contents recursively. %s\n", srcURL, srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to mirror this folder and its contents recursively. %s\n", srcURL, srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
}
|
||||
if !srcContent.Type.IsRegular() {
|
||||
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
@@ -155,7 +155,7 @@ func checkCastSyntaxTypeB(srcURL string, tgtURLs []string) {
|
||||
|
||||
func checkCastSyntaxTypeC(srcURL string, tgtURLs []string) {
|
||||
if len(tgtURLs) == 0 && tgtURLs == nil {
|
||||
console.Fatalf("Invalid number of target arguments to cast command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
|
||||
}
|
||||
srcURL = stripRecursiveURL(srcURL)
|
||||
_, srcContent, err := url2Stat(srcURL)
|
||||
@@ -179,52 +179,52 @@ func checkCastSyntaxTypeC(srcURL string, tgtURLs []string) {
|
||||
|
||||
// guessCastURLType guesses the type of URL. This approach all allows prepareURL
|
||||
// functions to accurately report failure causes.
|
||||
func guessCastURLType(sourceURL string, targetURLs []string) castURLsType {
|
||||
func guessCastURLType(sourceURL string, targetURLs []string) mirrorURLsType {
|
||||
if targetURLs == nil || len(targetURLs) == 0 { // Target is empty
|
||||
return castURLsTypeInvalid
|
||||
return mirrorURLsTypeInvalid
|
||||
}
|
||||
if sourceURL == "" { // Source is empty
|
||||
return castURLsTypeInvalid
|
||||
return mirrorURLsTypeInvalid
|
||||
}
|
||||
for _, targetURL := range targetURLs {
|
||||
if targetURL == "" { // One of the target is empty
|
||||
return castURLsTypeInvalid
|
||||
return mirrorURLsTypeInvalid
|
||||
}
|
||||
}
|
||||
|
||||
if isURLRecursive(sourceURL) { // Type C
|
||||
return castURLsTypeC
|
||||
return mirrorURLsTypeC
|
||||
} // else Type A or Type B
|
||||
for _, targetURL := range targetURLs {
|
||||
if isTargetURLDir(targetURL) { // Type B
|
||||
return castURLsTypeB
|
||||
return mirrorURLsTypeB
|
||||
}
|
||||
} // else Type A
|
||||
return castURLsTypeA
|
||||
return mirrorURLsTypeA
|
||||
}
|
||||
|
||||
// prepareSingleCastURLTypeA - prepares a single source and single target argument for casting.
|
||||
func prepareSingleCastURLsTypeA(sourceURL string, targetURL string) castURLs {
|
||||
// prepareSingleCastURLTypeA - prepares a single source and single target argument for mirroring.
|
||||
func prepareSingleCastURLsTypeA(sourceURL string, targetURL string) mirrorURLs {
|
||||
_, sourceContent, err := url2Stat(sourceURL)
|
||||
if err != nil { // Source does not exist or insufficient privileges.
|
||||
return castURLs{Error: NewIodine(iodine.New(err, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(err, nil))}
|
||||
}
|
||||
if !sourceContent.Type.IsRegular() { // Source is not a regular file
|
||||
return castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
}
|
||||
|
||||
// All OK.. We can proceed. Type A
|
||||
sourceContent.Name = sourceURL
|
||||
return castURLs{SourceContent: sourceContent, TargetContents: []*client.Content{{Name: targetURL}}}
|
||||
return mirrorURLs{SourceContent: sourceContent, TargetContents: []*client.Content{{Name: targetURL}}}
|
||||
}
|
||||
|
||||
// prepareCastURLsTypeA - A: cast(f, f) -> cast(f, f)
|
||||
func prepareCastURLsTypeA(sourceURL string, targetURLs []string) castURLs {
|
||||
var sURLs castURLs
|
||||
// prepareCastURLsTypeA - A: mirror(f, f) -> mirror(f, f)
|
||||
func prepareCastURLsTypeA(sourceURL string, targetURLs []string) mirrorURLs {
|
||||
var sURLs mirrorURLs
|
||||
for _, targetURL := range targetURLs { // Prepare each target separately
|
||||
URLs := prepareSingleCastURLsTypeA(sourceURL, targetURL)
|
||||
if URLs.Error != nil {
|
||||
return castURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
|
||||
}
|
||||
sURLs.SourceContent = URLs.SourceContent
|
||||
sURLs.TargetContents = append(sURLs.TargetContents, URLs.TargetContents...)
|
||||
@@ -232,17 +232,17 @@ func prepareCastURLsTypeA(sourceURL string, targetURLs []string) castURLs {
|
||||
return sURLs
|
||||
}
|
||||
|
||||
// prepareSingleCastURLsTypeB - prepares a single target and single source URLs for casting.
|
||||
func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) castURLs {
|
||||
// prepareSingleCastURLsTypeB - prepares a single target and single source URLs for mirroring.
|
||||
func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) mirrorURLs {
|
||||
_, sourceContent, err := url2Stat(sourceURL)
|
||||
if err != nil {
|
||||
// Source does not exist or insufficient privileges.
|
||||
return castURLs{Error: NewIodine(iodine.New(err, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(err, nil))}
|
||||
}
|
||||
|
||||
if !sourceContent.Type.IsRegular() {
|
||||
// Source is not a regular file.
|
||||
return castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
}
|
||||
|
||||
_, targetContent, err := url2Stat(targetURL)
|
||||
@@ -259,25 +259,25 @@ func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) castURLs {
|
||||
// Source is a file, target is a folder and exists.
|
||||
sourceURLParse, err := client.Parse(sourceURL)
|
||||
if err != nil {
|
||||
return castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
}
|
||||
|
||||
targetURLParse, err := client.Parse(targetURL)
|
||||
if err != nil {
|
||||
return castURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
|
||||
}
|
||||
// Reduce Type B to Type A.
|
||||
targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path))
|
||||
return prepareSingleCastURLsTypeA(sourceURL, targetURLParse.String())
|
||||
}
|
||||
|
||||
// prepareCastURLsTypeB - B: cast(f, d) -> cast(f, d/f) -> A
|
||||
func prepareCastURLsTypeB(sourceURL string, targetURLs []string) castURLs {
|
||||
var sURLs castURLs
|
||||
// prepareCastURLsTypeB - B: mirror(f, d) -> mirror(f, d/f) -> A
|
||||
func prepareCastURLsTypeB(sourceURL string, targetURLs []string) mirrorURLs {
|
||||
var sURLs mirrorURLs
|
||||
for _, targetURL := range targetURLs {
|
||||
URLs := prepareSingleCastURLsTypeB(sourceURL, targetURL)
|
||||
if URLs.Error != nil {
|
||||
return castURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
|
||||
return mirrorURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
|
||||
}
|
||||
sURLs.SourceContent = URLs.SourceContent
|
||||
sURLs.TargetContents = append(sURLs.TargetContents, URLs.TargetContents[0])
|
||||
@@ -286,13 +286,13 @@ func prepareCastURLsTypeB(sourceURL string, targetURLs []string) castURLs {
|
||||
}
|
||||
|
||||
// prepareCastURLsTypeC - C:
|
||||
func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs {
|
||||
castURLsCh := make(chan castURLs)
|
||||
func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan mirrorURLs {
|
||||
mirrorURLsCh := make(chan mirrorURLs)
|
||||
go func() {
|
||||
defer close(castURLsCh)
|
||||
defer close(mirrorURLsCh)
|
||||
if !isURLRecursive(sourceURL) {
|
||||
// Source is not of recursive type.
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errSourceNotRecursive{URL: sourceURL}, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(errSourceNotRecursive{URL: sourceURL}, nil))}
|
||||
return
|
||||
}
|
||||
// add `/` after trimming off `...` to emulate folders
|
||||
@@ -301,30 +301,30 @@ func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs
|
||||
// Source exist?
|
||||
if err != nil {
|
||||
// Source does not exist or insufficient privileges.
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(err, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(err, nil))}
|
||||
return
|
||||
}
|
||||
|
||||
if !sourceContent.Type.IsDir() {
|
||||
// Source is not a dir.
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errSourceIsNotDir{URL: sourceURL}, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(errSourceIsNotDir{URL: sourceURL}, nil))}
|
||||
return
|
||||
}
|
||||
|
||||
for sourceContent := range sourceClient.List(true) {
|
||||
if sourceContent.Err != nil {
|
||||
// Listing failed.
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(sourceContent.Err, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(sourceContent.Err, nil))}
|
||||
continue
|
||||
}
|
||||
if !sourceContent.Content.Type.IsRegular() {
|
||||
// Source is not a regular file. Skip it for cast.
|
||||
// Source is not a regular file. Skip it for mirror.
|
||||
continue
|
||||
}
|
||||
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
|
||||
sourceURLParse, err := client.Parse(sourceURL)
|
||||
if err != nil {
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
|
||||
continue
|
||||
}
|
||||
var newTargetURLs []string
|
||||
@@ -332,7 +332,7 @@ func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs
|
||||
for _, targetURL := range targetURLs {
|
||||
targetURLParse, err := client.Parse(targetURL)
|
||||
if err != nil {
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
|
||||
continue
|
||||
}
|
||||
sourceURLDelimited := sourceURLParse.String()[:strings.LastIndex(sourceURLParse.String(),
|
||||
@@ -341,7 +341,7 @@ func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs
|
||||
sourceContentURL := sourceURLDelimited + sourceContentName
|
||||
sourceContentParse, err = client.Parse(sourceContentURL)
|
||||
if err != nil {
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceContentName}, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceContentName}, nil))}
|
||||
continue
|
||||
}
|
||||
// Construct target path from recursive path of source without its prefix dir.
|
||||
@@ -349,29 +349,29 @@ func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs
|
||||
newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName)
|
||||
newTargetURLs = append(newTargetURLs, newTargetURLParse.String())
|
||||
}
|
||||
castURLsCh <- prepareCastURLsTypeA(sourceContentParse.String(), newTargetURLs)
|
||||
mirrorURLsCh <- prepareCastURLsTypeA(sourceContentParse.String(), newTargetURLs)
|
||||
}
|
||||
}()
|
||||
return castURLsCh
|
||||
return mirrorURLsCh
|
||||
}
|
||||
|
||||
// prepareCastURLs - prepares target and source URLs for casting.
|
||||
func prepareCastURLs(sourceURL string, targetURLs []string) <-chan castURLs {
|
||||
castURLsCh := make(chan castURLs)
|
||||
// prepareCastURLs - prepares target and source URLs for mirroring.
|
||||
func prepareCastURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs {
|
||||
mirrorURLsCh := make(chan mirrorURLs)
|
||||
go func() {
|
||||
defer close(castURLsCh)
|
||||
defer close(mirrorURLsCh)
|
||||
switch guessCastURLType(sourceURL, targetURLs) {
|
||||
case castURLsTypeA:
|
||||
castURLsCh <- prepareCastURLsTypeA(sourceURL, targetURLs)
|
||||
case castURLsTypeB:
|
||||
castURLsCh <- prepareCastURLsTypeB(sourceURL, targetURLs)
|
||||
case castURLsTypeC:
|
||||
case mirrorURLsTypeA:
|
||||
mirrorURLsCh <- prepareCastURLsTypeA(sourceURL, targetURLs)
|
||||
case mirrorURLsTypeB:
|
||||
mirrorURLsCh <- prepareCastURLsTypeB(sourceURL, targetURLs)
|
||||
case mirrorURLsTypeC:
|
||||
for sURLs := range prepareCastURLsTypeC(sourceURL, targetURLs) {
|
||||
castURLsCh <- sURLs
|
||||
mirrorURLsCh <- sURLs
|
||||
}
|
||||
default:
|
||||
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidArgument{}, nil))}
|
||||
mirrorURLsCh <- mirrorURLs{Error: NewIodine(iodine.New(errInvalidArgument{}, nil))}
|
||||
}
|
||||
}()
|
||||
return castURLsCh
|
||||
return mirrorURLsCh
|
||||
}
|
||||
+4
-4
@@ -106,7 +106,7 @@ func (c CopyMessage) String() string {
|
||||
return console.JSON(string(copyMessageBytes) + "\n")
|
||||
}
|
||||
|
||||
// CastMessage container for file cast messages
|
||||
// CastMessage container for file mirror messages
|
||||
type CastMessage struct {
|
||||
Version string `json:"version"`
|
||||
Source string `json:"source"`
|
||||
@@ -114,15 +114,15 @@ type CastMessage struct {
|
||||
Length int64 `json:"length"`
|
||||
}
|
||||
|
||||
// String string printer for cast message
|
||||
// String string printer for mirror message
|
||||
func (s CastMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets)
|
||||
}
|
||||
s.Version = "1.0.0"
|
||||
castMessageBytes, err := json.MarshalIndent(s, "", "\t")
|
||||
mirrorMessageBytes, err := json.MarshalIndent(s, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return console.JSON(string(castMessageBytes) + "\n")
|
||||
return console.JSON(string(mirrorMessageBytes) + "\n")
|
||||
}
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@ import (
|
||||
// Help message.
|
||||
var sessionCmd = cli.Command{
|
||||
Name: "session",
|
||||
Usage: "Manage sessions for cp and cast",
|
||||
Usage: "Manage sessions for cp and mirror",
|
||||
Action: runSessionCmd,
|
||||
CustomHelpTemplate: `NAME:
|
||||
mc {{.Name}} - {{.Usage}}
|
||||
@@ -110,7 +110,7 @@ func sessionExecute(s *sessionV2) {
|
||||
switch s.Header.CommandType {
|
||||
case "cp":
|
||||
doCopyCmdSession(s)
|
||||
case "cast":
|
||||
case "mirror":
|
||||
doCastCmdSession(s)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user