diff --git a/access-main.go b/access-main.go index 39f54d53..3adf5ca4 100644 --- a/access-main.go +++ b/access-main.go @@ -33,7 +33,7 @@ var accessCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} PERMISSION TARGET [TARGET...] {{if .Description}} + mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} PERMISSION TARGET [TARGET ...] {{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} @@ -71,8 +71,8 @@ func mainAccess(ctx *cli.Context) { } for _, arg := range ctx.Args().Tail() { targetURL, err := getCanonicalizedURL(arg, config.Aliases) - fatalIf(err.Trace(arg), "Unable to parse URL argument ‘"+arg+"’.") - fatalIf(doUpdateAccessCmd(targetURL, acl).Trace(targetURL), "Unable to set access permission ‘"+acl.String()+"’ for URL ‘"+targetURL+"’.") + fatalIf(err.Trace(arg), "Unable to parse argument ‘"+arg+"’.") + fatalIf(doUpdateAccessCmd(targetURL, acl).Trace(targetURL), "Unable to set access permission ‘"+acl.String()+"’ for ‘"+targetURL+"’.") } } diff --git a/alias-json.go b/alias-json.go deleted file mode 100644 index ddffe06e..00000000 --- a/alias-json.go +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "encoding/json" - "fmt" - - "github.com/minio/mc/pkg/console" -) - -// AliasMessage container for content message structure -type AliasMessage struct { - Alias string `json:"alias"` - URL string `json:"url"` -} - -// String string printer for Content metadata -func (a AliasMessage) String() string { - if !globalJSONFlag { - message := console.Colorize("Alias", fmt.Sprintf("[%s] <-", a.Alias)) - message += console.Colorize("URL", fmt.Sprintf("%s", a.URL)) - return message + "\n" - } - jsonMessageBytes, err := json.Marshal(a) - if err != nil { - panic(err) - } - return string(jsonMessageBytes) + "\n" -} diff --git a/config-main.go b/config-main.go index 2f019a98..f8776e5a 100644 --- a/config-main.go +++ b/config-main.go @@ -17,6 +17,7 @@ package main import ( + "encoding/json" "errors" "fmt" "strings" @@ -58,6 +59,26 @@ EXAMPLES: `, } +// AliasMessage container for content message structure +type AliasMessage struct { + Alias string `json:"alias"` + URL string `json:"url"` +} + +// String string printer for Content metadata +func (a AliasMessage) String() string { + if !globalJSONFlag { + message := console.Colorize("Alias", fmt.Sprintf("[%s] <-", a.Alias)) + message += console.Colorize("URL", fmt.Sprintf("%s", a.URL)) + return message + "\n" + } + jsonMessageBytes, err := json.Marshal(a) + if err != nil { + panic(err) + } + return string(jsonMessageBytes) + "\n" +} + // mainConfig is the handle for "mc config" sub-command. writes configuration data in json format to config file. func mainConfig(ctx *cli.Context) { // show help if nothing is set diff --git a/cp-json.go b/cp-json.go deleted file mode 100644 index 79c3d57c..00000000 --- a/cp-json.go +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "encoding/json" - "fmt" - - "github.com/minio/minio/pkg/probe" -) - -// CopyMessage container for file copy messages -type CopyMessage struct { - Source string `json:"source"` - Target string `json:"target"` - Length int64 `json:"length"` -} - -// String string printer for copy message -func (c CopyMessage) String() string { - if !globalJSONFlag { - return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target) - } - copyMessageBytes, err := json.Marshal(c) - fatalIf(probe.NewError(err), "Failed to marshal copy message.") - - return string(copyMessageBytes) + "\n" -} diff --git a/cp-main.go b/cp-main.go index 4102da54..da381209 100644 --- a/cp-main.go +++ b/cp-main.go @@ -68,6 +68,24 @@ EXAMPLES: `, } +// CopyMessage container for file copy messages +type CopyMessage struct { + Source string `json:"source"` + Target string `json:"target"` + Length int64 `json:"length"` +} + +// String string printer for copy message +func (c CopyMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target) + } + copyMessageBytes, err := json.Marshal(c) + fatalIf(probe.NewError(err), "Failed to marshal copy message.") + + return string(copyMessageBytes) + "\n" +} + // doCopy - Copy a singe file from source to destination func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGroup, statusCh chan<- copyURLs) { defer wg.Done() // Notify that this copy routine is done. diff --git a/cp-url.go b/cp-url.go index e29ff1b5..337f9633 100644 --- a/cp-url.go +++ b/cp-url.go @@ -82,7 +82,7 @@ func checkCopySyntax(ctx *cli.Context) { } if url.Host != "" { if url.Path == string(url.Separator) { - fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Bucket creation detected for %s, cloud storage URL's should use ‘mc mb’ to create buckets.", tgtURL)) + fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL)) } } } diff --git a/diff-main.go b/diff-main.go index 971b629b..f36f03fb 100644 --- a/diff-main.go +++ b/diff-main.go @@ -59,15 +59,15 @@ func mainDiff(ctx *cli.Context) { } config := mustGetMcConfig() - firstURL := ctx.Args().First() - secondURL := ctx.Args()[1] + firstArg := ctx.Args().First() + secondArg := ctx.Args()[1] var err *probe.Error - firstURL, err = getCanonicalizedURL(firstURL, config.Aliases) - fatalIf(err.Trace(firstURL), "Unable to canonicalize first URL.") + firstURL, err := getCanonicalizedURL(firstArg, config.Aliases) + fatalIf(err.Trace(firstArg), "Unable to parse first argument ‘"+firstArg+"’.") - secondURL, err = getCanonicalizedURL(secondURL, config.Aliases) - fatalIf(err.Trace(secondURL), "Unable to canonicalize second URL.") + secondURL, err := getCanonicalizedURL(secondArg, config.Aliases) + fatalIf(err.Trace(secondArg), "Unable to parse second argument ‘"+secondArg+"’.") if isURLRecursive(secondURL) { fatalIf(probe.NewError(errInvalidArgument), "Second argument ‘"+secondURL+"’ cannot be recursive.") diff --git a/doc.go b/doc.go deleted file mode 100644 index bcecedc5..00000000 --- a/doc.go +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Minio Client (C) 2014, 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main diff --git a/docs/access.md b/docs/access.md deleted file mode 100644 index b3d727d6..00000000 --- a/docs/access.md +++ /dev/null @@ -1,24 +0,0 @@ -#### access - -```go -Name: - mc access - Set access permissions - -USAGE: - mc access PERMISSION TARGET [TARGET...] - -EXAMPLES: - - 1. Set bucket to "private" on Amazon S3 cloud storage. - $ mc access private https://s3.amazonaws.com/burningman2011 - - 2. Set bucket to "public" on Amazon S3 cloud storage. - $ mc access public https://s3.amazonaws.com/shared - - 3. Set bucket to "authenticated" on Amazon S3 cloud storage to provide read access to IAM Authenticated Users group. - $ mc access authenticated https://s3.amazonaws.com/shared-authenticated - - 4. Set folder to world readwrite (chmod 777) on local filesystem. - $ mc access public /shared/Music - -``` diff --git a/docs/cat.md b/docs/cat.md deleted file mode 100644 index 4a5376cd..00000000 --- a/docs/cat.md +++ /dev/null @@ -1,23 +0,0 @@ -#### cat - -```go -NAME: - mc cat - Display contents of a file - -USAGE: - mc cat SOURCE [SOURCE...] - -EXAMPLES: - 1. Concantenate an object from Amazon S3 cloud storage to mplayer standard input. - $ mc cat https://s3.amazonaws.com/ferenginar/klingon_opera_aktuh_maylotah.ogg | mplayer - - - 2. Concantenate a file from local filesystem to standard output. - $ mc cat khitomer-accords.txt - - 3. Concantenate multiple files from local filesystem to standard output. - $ mc cat *.txt > newfile.txt - - 4. Concatenate a non english file name from Amazon S3 cloud storage. - $ mc cat s3:andoria/本語 > /tmp/本語 - -``` diff --git a/docs/config.md b/docs/config.md deleted file mode 100644 index dae6215a..00000000 --- a/docs/config.md +++ /dev/null @@ -1,13 +0,0 @@ -#### config - -```go -NAME: - mc config - Add new alias to default configuration "~/.mc/config.json" file. - -USAGE: - mc config alias NAME HOSTURL - -EXAMPLES: - 1. Add alias URLs - $ mc config alias zek https://s3.amazonaws.com/ - ``` diff --git a/docs/cp.md b/docs/cp.md deleted file mode 100644 index 9b286a04..00000000 --- a/docs/cp.md +++ /dev/null @@ -1,26 +0,0 @@ -#### cp - -```go -NAME: - mc cp - Copy files and folders from many sources to a single destination - -USAGE: - mc cp SOURCE [SOURCE...] TARGET - -EXAMPLES: - 1. Copy list of objects from local file system to Amazon S3 cloud storage. - $ mc cp Music/*.ogg https://s3.amazonaws.com/jukebox/ - - 2. Copy a bucket recursively from Minio cloud storage to Amazon S3 cloud storage. - $ mc cp https://play.minio.io:9000/photos/burningman2011... https://s3.amazonaws.com/private-photos/burningman/ - - 3. Copy multiple local folders recursively to Minio cloud storage. - $ mc cp backup/2014/... backup/2015/... https://play.minio.io:9000/archive/ - - 4. Copy a bucket recursively from aliased Amazon S3 cloud storage to local filesystem on Windows. - $ mc cp s3:documents/2014/... C:\backup\2014 - - 5. Copy an object of non english characters to Amazon S3 cloud storage. - $ mc cp 本語 s3:andoria/本語 - -``` diff --git a/docs/ls.md b/docs/ls.md deleted file mode 100644 index d77f5913..00000000 --- a/docs/ls.md +++ /dev/null @@ -1,37 +0,0 @@ -#### ls - -```go -NAME: - mc ls - List files and folders - -USAGE: - mc ls TARGET [TARGET...] - -EXAMPLES: - 1. List objects recursively on Minio cloud storage. - $ mc ls https://play.minio.io:9000/backup/... - [2015-03-28 12:47:50 PDT] 34MiB 2006-Jan-1/backup.tar.gz - [2015-03-31 14:46:33 PDT] 55MiB 2006-Mar-1/backup.tar.gz - - 2. List buckets on Amazon S3 cloud storage. - $ mc ls https://s3.amazonaws.com/ - [2015-01-20 15:42:00 PST] 0B rom/ - [2015-01-15 00:05:40 PST] 0B zek/ - - 3. List buckets from Amazon S3 cloud storage and recursively list objects from Minio cloud storage. - $ mc ls https://s3.amazonaws.com/ https://play.minio.io:9000/backup/... - 2015-01-15 00:05:40 PST 0B zek/ - 2015-03-31 14:46:33 PDT 55MiB 2006-Mar-1/backup.tar.gz - - 4. List files recursively on local filesystem on Windows. - $ mc ls C:\Users\Worf\... - [2015-03-28 12:47:50 PDT] 11.00MiB Martok\Klingon Council Ministers.pdf - [2015-03-31 14:46:33 PDT] 15.00MiB Gowron\Khitomer Conference Details.pdf - - 5. List files with non english characters on Amazon S3 cloud storage. - $ mc ls s3:andoria/本... - [2015-05-19 17:21:49 PDT] 41B 本語.pdf - [2015-05-19 17:24:19 PDT] 41B 本語.txt - [2015-05-19 17:28:22 PDT] 41B 本語.md - -``` diff --git a/docs/mb.md b/docs/mb.md deleted file mode 100644 index b11c4265..00000000 --- a/docs/mb.md +++ /dev/null @@ -1,19 +0,0 @@ -#### mb - -```go -NAME: - mc mb - Make a bucket or folder - -USAGE: - mc mb TARGET [TARGET...] - -EXAMPLES: - 1. Create a bucket on Amazon S3 cloud storage. - $ mc mb https://s3.amazonaws.com/public-document-store - - 3. Make a folder on local filesystem, including its parent folders as needed. - $ mc mb ~/ - - 3. Create a bucket on Minio cloud storage. - $ mc mb https://play.minio.io:9000/mongodb-backup -``` diff --git a/docs/mirror.md b/docs/mirror.md deleted file mode 100644 index addcb450..00000000 --- a/docs/mirror.md +++ /dev/null @@ -1,26 +0,0 @@ -#### mirror - -```go -NAME: - mc mirror - Mirror files and folders from a single source to many destinations - -USAGE: - mc mirror SOURCE TARGET [TARGET...] - -EXAMPLES: - 1. Mirror an object from local filesystem to Amazon S3 cloud storage. - $ mc mirror star-trek-episode-10-season4.ogg https://s3.amazonaws.com/trekarchive - - 2. Mirror 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. Mirror 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. Mirror 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. Mirror a local file of non english character to Amazon s3 cloud storage. - $ mc mirror 本語/... s3:mylocaldocuments C:\backup\2014 play:backup - -``` diff --git a/docs/update.md b/docs/update.md deleted file mode 100644 index f322070e..00000000 --- a/docs/update.md +++ /dev/null @@ -1,14 +0,0 @@ -#### update - -```go -Name: - mc update - Check for new software updates - -USAGE: - mc update - -EXAMPLES: - 1. Check for new updates - $ mc update - -``` diff --git a/ls-json.go b/ls-json.go deleted file mode 100644 index 5d8add10..00000000 --- a/ls-json.go +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "encoding/json" - "fmt" - - "github.com/minio/mc/pkg/console" -) - -// ContentMessage container for content message structure -type ContentMessage struct { - Filetype string `json:"type"` - Time string `json:"last-modified"` - Size string `json:"size"` - Name string `json:"name"` -} - -// String string printer for Content metadata -func (c ContentMessage) String() string { - if !globalJSONFlag { - message := console.Colorize("Time", fmt.Sprintf("[%s] ", c.Time)) - message = message + console.Colorize("Size", fmt.Sprintf("%6s ", c.Size)) - message = func() string { - if c.Filetype == "folder" { - return message + console.Colorize("Dir", fmt.Sprintf("%s", c.Name)) - } - return message + console.Colorize("File", fmt.Sprintf("%s", c.Name)) - }() - return message + "\n" - } - jsonMessageBytes, err := json.Marshal(c) - if err != nil { - panic(err) - } - return string(jsonMessageBytes) + "\n" -} diff --git a/ls-main.go b/ls-main.go index df81a3f0..0f37e25c 100644 --- a/ls-main.go +++ b/ls-main.go @@ -30,7 +30,7 @@ var lsCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}} TARGET [TARGET...] {{if .Description}} + mc {{.Name}} TARGET [TARGET ...] {{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} @@ -83,10 +83,10 @@ func mainList(ctx *cli.Context) { config := mustGetMcConfig() for _, arg := range args { targetURL, err := getCanonicalizedURL(arg, config.Aliases) - fatalIf(err.Trace(), "Unable to canonicalize URL") + fatalIf(err.Trace(arg), "Unable to parse argument ‘"+arg+"’.") // if recursive strip off the "..." - fatalIf(doListCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL)).Trace(), "Unable to list target URL") + fatalIf(doListCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL)).Trace(), "Unable to list target ‘"+targetURL+"’.") } } diff --git a/ls.go b/ls.go index 201a284b..526ef05b 100644 --- a/ls.go +++ b/ls.go @@ -17,6 +17,7 @@ package main import ( + "encoding/json" "fmt" "os" "runtime" @@ -34,7 +35,35 @@ const ( printDate = "2006-01-02 15:04:05 MST" ) -// parseContent parse client Content container into printer struct +// ContentMessage container for content message structure. +type ContentMessage struct { + Filetype string `json:"type"` + Time string `json:"last-modified"` + Size string `json:"size"` + Name string `json:"name"` +} + +// String string printer for Content metadata. +func (c ContentMessage) String() string { + if !globalJSONFlag { + message := console.Colorize("Time", fmt.Sprintf("[%s] ", c.Time)) + message = message + console.Colorize("Size", fmt.Sprintf("%6s ", c.Size)) + message = func() string { + if c.Filetype == "folder" { + return message + console.Colorize("Dir", fmt.Sprintf("%s", c.Name)) + } + return message + console.Colorize("File", fmt.Sprintf("%s", c.Name)) + }() + return message + } + jsonMessageBytes, err := json.Marshal(c) + if err != nil { + panic(err) + } + return string(jsonMessageBytes) +} + +// parseContent parse client Content container into printer struct. func parseContent(c *client.Content) ContentMessage { content := ContentMessage{} content.Time = c.Time.Local().Format(printDate) @@ -49,7 +78,7 @@ func parseContent(c *client.Content) ContentMessage { content.Size = humanize.IBytes(uint64(c.Size)) - // Convert OS Type to match console file printing style + // Convert OS Type to match console file printing style. content.Name = func() string { switch { case runtime.GOOS == "windows": @@ -71,7 +100,7 @@ func parseContent(c *client.Content) ContentMessage { return content } -// doList - list all entities inside a folder +// doList - list all entities inside a folder. func doList(clnt client.Client, recursive bool) *probe.Error { var err *probe.Error for contentCh := range clnt.List(recursive) { @@ -89,7 +118,7 @@ func doList(clnt client.Client, recursive bool) *probe.Error { err = contentCh.Err.Trace() break } - console.Print(parseContent(contentCh.Content)) + console.Println(parseContent(contentCh.Content)) } if err != nil { return err.Trace() diff --git a/main.go b/main.go index 5555628c..96dd684d 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ package main import ( + "errors" "fmt" "net/http" "os" @@ -27,19 +28,18 @@ import ( "github.com/minio/cli" "github.com/minio/mc/pkg/console" + "github.com/minio/minio/pkg/probe" "github.com/minio/pb" ) // Check for the environment early on and gracefully report. func checkConfig() { - _, err := user.Current() - if err != nil { - console.Fatalf("Unable to determine current user. %s\n", err) - } + _, e := user.Current() + fatalIf(probe.NewError(e), "Unable to determine current user.") // Ensures config file is sane - _, perr := getMcConfig() - fatalIf(perr.Trace(), "Unable to read config") + _, err := getMcConfig() + fatalIf(err.Trace(), "Unable to access configuration file.") } func migrate() { @@ -52,10 +52,9 @@ func migrate() { // Get os/arch/platform specific information. // Returns a map of current os/arch/platform/memstats func getSystemData() map[string]string { - host, err := os.Hostname() - if err != nil { - console.Fatalf("Unable to determine hostname. %s\n", err) - } + host, e := os.Hostname() + fatalIf(probe.NewError(e), "Unable to determine the hostname.") + memstats := &runtime.MemStats{} runtime.ReadMemStats(memstats) mem := fmt.Sprintf("Used: %s | Allocated: %s | UsedHeap: %s | AllocatedHeap: %s", @@ -151,7 +150,7 @@ VERSION: {{end}} ` app.CommandNotFound = func(ctx *cli.Context, command string) { - console.Fatalf("Command not found: ‘%s’\n", command) + fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Command not found: ‘%s’", command)) } return app } diff --git a/mb-main.go b/mb-main.go index a3d3f560..d2a35900 100644 --- a/mb-main.go +++ b/mb-main.go @@ -31,7 +31,7 @@ var mbCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}} TARGET [TARGET...] {{if .Description}} + mc {{.Name}} TARGET [TARGET ...] {{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} @@ -60,11 +60,9 @@ func mainMakeBucket(ctx *cli.Context) { config := mustGetMcConfig() for _, arg := range ctx.Args() { targetURL, err := getCanonicalizedURL(arg, config.Aliases) - fatalIf(err.Trace(), "Unable to canonicalize URL") - - fatalIf(doMakeBucketCmd(targetURL).Trace(), "Unable to make bucket") - - console.Infoln("Bucket created successfully : " + targetURL) + fatalIf(err.Trace(arg), "Unable to parse argument ‘"+arg+"’.") + fatalIf(doMakeBucketCmd(targetURL).Trace(targetURL), "Unable to make bucket ‘"+targetURL+"’.") + console.Infoln("Bucket created successfully: " + targetURL) } } diff --git a/mirror-json.go b/mirror-json.go deleted file mode 100644 index 8aaf645c..00000000 --- a/mirror-json.go +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "encoding/json" - "fmt" -) - -// MirrorMessage container for file mirror messages -type MirrorMessage struct { - Source string `json:"source"` - Targets []string `json:"targets"` - Length int64 `json:"length"` -} - -// String string printer for mirror message -func (s MirrorMessage) String() string { - if !globalJSONFlag { - return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets) - } - mirrorMessageBytes, err := json.Marshal(s) - if err != nil { - panic(err) - } - return string(mirrorMessageBytes) + "\n" -} diff --git a/mirror-main.go b/mirror-main.go index 6d04cddf..5031a6d1 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -40,7 +40,7 @@ var mirrorCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE TARGET [TARGET...] {{if .Description}} + mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE TARGET [TARGET ...] {{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} diff --git a/mirror-url.go b/mirror-url.go index a5a7d39f..2b662550 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -17,13 +17,15 @@ package main import ( + "encoding/json" + "errors" + "fmt" "path/filepath" "strings" "sync" "github.com/minio/cli" "github.com/minio/mc/pkg/client" - "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/probe" "github.com/tchap/go-patricia/patricia" ) @@ -44,6 +46,25 @@ func (m mirrorURLs) isEmpty() bool { return false } +// MirrorMessage container for file mirror messages +type MirrorMessage struct { + Source string `json:"source"` + Targets []string `json:"targets"` + Length int64 `json:"length"` +} + +// String string printer for mirror message +func (s MirrorMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("‘%s’ -> ‘%s’", s.Source, s.Targets) + } + mirrorMessageBytes, err := json.Marshal(s) + if err != nil { + panic(err) + } + return string(mirrorMessageBytes) +} + // // * MIRROR ARGS - VALID CASES // ========================= @@ -57,7 +78,7 @@ func checkMirrorSyntax(ctx *cli.Context) { // extract URLs. URLs, err := args2URLs(ctx.Args()) - fatalIf(err.Trace(), "Unable to convert args to URLs") + fatalIf(err.Trace(ctx.Args()...), "Unable to parse arguments.") srcURL := URLs[0] tgtURLs := URLs[1:] @@ -66,45 +87,47 @@ func checkMirrorSyntax(ctx *cli.Context) { // Source cannot be a folder (except when recursive) if !isURLRecursive(srcURL) { _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(), "Unable to stat URL") + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if !srcContent.Type.IsRegular() { if srcContent.Type.IsDir() { - console.Fatalf("Source ‘%s’ is a folder. Please use ‘%s...’ to recursively copy this folder and its contents.\n", srcURL, srcURL) + fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively.", srcURL, srcURL)) } - console.Fatalf("Source ‘%s’ is not a regular file.\n", srcURL) + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a file.") } - } + } else { // Recursive source URL. + newSrcURL := stripRecursiveURL(srcURL) + _, srcContent, err := url2Stat(newSrcURL) + fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+newSrcURL+"’.") - for _, tgtURL := range tgtURLs { - url, err := client.Parse(tgtURL) - if err != nil { - console.Fatalf("Unable to parse target ‘%s’ argument. %s\n", tgtURL, err) - } - if url.Host != "" { - if url.Path == string(url.Separator) { - console.Fatalf("Bucket creation detected for %s, cloud storage URL's should use ‘mc mb’ to create buckets\n", tgtURL) - } + if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. + fatalIf(probe.NewError(errInvalidArgument), "Source ‘"+srcURL+"’ is not a folder.") } } if len(tgtURLs) == 0 && tgtURLs == nil { - console.Fatalf("Invalid number of target arguments to mirror command. %s\n", errInvalidArgument) + fatalIf(probe.NewError(errInvalidArgument), "Invalid number of target arguments to mirror command.") } - _, srcContent, err := url2Stat(stripRecursiveURL(srcURL)) - fatalIf(err.Trace(), "Unable to stat source URL.") - - if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. - fatalIf(probe.NewError(errSourceIsNotDir{URL: srcURL}), "Invalid source type.") - } for _, tgtURL := range tgtURLs { - _, content, err := url2Stat(tgtURL) - if err == nil { - if !content.Type.IsDir() { - fatalIf(probe.NewError(errTargetIsNotDir{URL: tgtURL}), "Invalid target type.") + // Recursive URLs are not allowed in target. + if isURLRecursive(tgtURL) { + fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL)) + } + + url, e := client.Parse(tgtURL) + fatalIf(probe.NewError(e), "Unable to parse target ‘"+tgtURL+"’ argument.") + + if url.Host != "" { + if url.Path == string(url.Separator) { + fatalIf(probe.NewError(errInvalidArgument), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL)) } } + _, content, err := url2Stat(tgtURL) + fatalIf(err.Trace(tgtURL), "Unable to stat target ‘"+tgtURL+"’.") + if !content.Type.IsDir() { + fatalIf(probe.NewError(errInvalidArgument), "Target ‘"+tgtURL+"’ is not a folder.") + } } } @@ -189,7 +212,7 @@ func prepareMirrorURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs defer close(mirrorURLsCh) sourceClnt, err := url2Client(stripRecursiveURL(sourceURL)) if err != nil { - mirrorURLsCh <- mirrorURLs{Error: err.Trace()} + mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceURL)} return } targetClnts := make([]client.Client, len(targetURLs)) @@ -197,7 +220,7 @@ func prepareMirrorURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs targetURL = stripRecursiveURL(targetURL) targetClnt, targetContent, err := url2Stat(targetURL) if err != nil { - mirrorURLsCh <- mirrorURLs{Error: err.Trace()} + mirrorURLsCh <- mirrorURLs{Error: err.Trace(targetURL)} return } // if one of the targets is not dir exit @@ -209,7 +232,7 @@ func prepareMirrorURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs newTargetURL := strings.TrimSuffix(targetURL, string(targetClnt.URL().Separator)) + string(targetClnt.URL().Separator) targetClnt, err = url2Client(newTargetURL) if err != nil { - mirrorURLsCh <- mirrorURLs{Error: err.Trace()} + mirrorURLsCh <- mirrorURLs{Error: err.Trace(newTargetURL)} return } targetClnts[i] = targetClnt diff --git a/session-json.go b/session-json.go deleted file mode 100644 index 9480ed08..00000000 --- a/session-json.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "encoding/json" - "fmt" - "strings" - - "github.com/minio/mc/pkg/console" -) - -// SessionJSONMessage json container for session messages -type SessionJSONMessage struct { - SessionID string `json:"sessionid"` - Time string `json:"time"` - CommandType string `json:"command-type"` - CommandArgs []string `json:"command-args"` -} - -func (s sessionV2) String() string { - if !globalJSONFlag { - message := console.Colorize("SessionID", fmt.Sprintf("%s -> ", s.SessionID)) - message = message + console.Colorize("Time", fmt.Sprintf("[%s]", s.Header.When.Local().Format(printDate))) - message = message + console.Colorize("Command", fmt.Sprintf(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " "))) - return message + "\n" - } - sessionMesage := SessionJSONMessage{ - SessionID: s.SessionID, - Time: s.Header.When.Local().Format(printDate), - CommandType: s.Header.CommandType, - CommandArgs: s.Header.CommandArgs, - } - sessionJSONBytes, err := json.Marshal(sessionMesage) - if err != nil { - panic(err) - } - return string(sessionJSONBytes) + "\n" -} diff --git a/session-v2.go b/session-v2.go index 43f54249..23c7ba60 100644 --- a/session-v2.go +++ b/session-v2.go @@ -19,9 +19,12 @@ package main import ( + "encoding/json" "errors" + "fmt" "io" "os" + "strings" "sync" "time" @@ -54,6 +57,14 @@ type sessionV2Header struct { TotalObjects int `json:"total-objects"` } +// SessionJSONMessage json container for session messages +type SessionJSONMessage struct { + SessionID string `json:"sessionid"` + Time string `json:"time"` + CommandType string `json:"command-type"` + CommandArgs []string `json:"command-args"` +} + // sessionV2 type sessionV2 struct { Header *sessionV2Header @@ -63,6 +74,26 @@ type sessionV2 struct { sigCh bool } +func (s sessionV2) String() string { + if !globalJSONFlag { + message := console.Colorize("SessionID", fmt.Sprintf("%s -> ", s.SessionID)) + message = message + console.Colorize("Time", fmt.Sprintf("[%s]", s.Header.When.Local().Format(printDate))) + message = message + console.Colorize("Command", fmt.Sprintf(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " "))) + return message + "\n" + } + sessionMesage := SessionJSONMessage{ + SessionID: s.SessionID, + Time: s.Header.When.Local().Format(printDate), + CommandType: s.Header.CommandType, + CommandArgs: s.Header.CommandArgs, + } + sessionJSONBytes, err := json.Marshal(sessionMesage) + if err != nil { + panic(err) + } + return string(sessionJSONBytes) + "\n" +} + // newSessionV2 provides a new session func newSessionV2() *sessionV2 { s := &sessionV2{} diff --git a/share-json.go b/share-json.go deleted file mode 100644 index 5278440b..00000000 --- a/share-json.go +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "time" -) - -// ShareMessage container for share messages -type ShareMessage struct { - Expires time.Duration `json:"expire-seconds"` - PresignedURL string `json:"presigned-url"` -} - -// String string printer for share message -func (s ShareMessage) String() string { - if !globalJSONFlag { - return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s\n", s.Expires, s.PresignedURL) - } - shareMessageBytes, err := json.Marshal(s) - if err != nil { - panic(err) - } - // json encoding escapes ampersand into its unicode character which is not usable directly for share - // and fails with cloud storage. convert them back so that they are usable - shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) - return fmt.Sprintf("%s\n", string(shareMessageBytes)) -} diff --git a/share-main.go b/share-main.go index e310f298..c6777c51 100644 --- a/share-main.go +++ b/share-main.go @@ -17,7 +17,10 @@ package main import ( + "bytes" + "encoding/json" "errors" + "fmt" "path/filepath" "strings" "time" @@ -76,12 +79,33 @@ func mainShare(ctx *cli.Context) { fatalIf(probe.NewError(err), "Unable to parse time argument") } targetURL, err := getCanonicalizedURL(url, config.Aliases) - fatalIf(err.Trace(), "Unable to canonicalize URL") + fatalIf(err.Trace(url), "Unable to parse argument ‘"+url+"’.") // if recursive strip off the "..." fatalIf(doShareCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires).Trace(), "Unable generate URL for sharing") } +// ShareMessage container for share messages +type ShareMessage struct { + Expires time.Duration `json:"expire-seconds"` + PresignedURL string `json:"presigned-url"` +} + +// String string printer for share message +func (s ShareMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s\n", s.Expires, s.PresignedURL) + } + shareMessageBytes, err := json.Marshal(s) + if err != nil { + panic(err) + } + // json encoding escapes ampersand into its unicode character which is not usable directly for share + // and fails with cloud storage. convert them back so that they are usable + shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) + return fmt.Sprintf("%s\n", string(shareMessageBytes)) +} + func getNewTargetURL(targetParser *client.URL, name string) string { match, _ := filepath.Match("*.s3*.amazonaws.com", targetParser.Host) if match { diff --git a/typed-errors.go b/typed-errors.go index 2b7abeb5..784c63fb 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -73,12 +73,6 @@ func (e errInvalidTarget) Error() string { return "Invalid target " + e.URL } -type errTargetIsNotDir errInvalidURL - -func (e errTargetIsNotDir) Error() string { - return "Target ‘" + e.URL + "’ is not a folder." -} - type errSourceNotRecursive errInvalidURL func (e errSourceNotRecursive) Error() string {