new pig command

This commit is contained in:
Anand Babu (AB) Periasamy
2015-10-11 01:45:32 -07:00
parent f49cac08b9
commit 21a90da0bf
3 changed files with 115 additions and 13 deletions
+28 -13
View File
@@ -49,7 +49,7 @@ func isTargetURLDir(targetURL string) bool {
return true
}
// getSource gets a reader from URL<
// getSource gets a reader from URL
func getSource(sourceURL string) (reader io.ReadCloser, length int64, err *probe.Error) {
sourceClnt, err := source2Client(sourceURL)
if err != nil {
@@ -58,7 +58,7 @@ func getSource(sourceURL string) (reader io.ReadCloser, length int64, err *probe
return sourceClnt.GetObject(0, 0)
}
// putTarget writes to URL from reader.
// putTarget writes to URL from reader. If length=0, read until EOF.
func putTarget(targetURL string, length int64, reader io.Reader) *probe.Error {
targetClnt, err := target2Client(targetURL)
if err != nil {
@@ -71,11 +71,13 @@ func putTarget(targetURL string, length int64, reader io.Reader) *probe.Error {
return nil
}
// putTargets writes to URL from reader.
// putTargets writes to URL from reader. If length=0, read until EOF.
func putTargets(targetURLs []string, length int64, reader io.Reader) *probe.Error {
var tgtReaders []io.ReadCloser
var tgtWriters []io.WriteCloser
var tgtReaders []*io.PipeReader
var tgtWriters []*io.PipeWriter
var tgtClients []client.Client
errCh := make(chan *probe.Error)
defer close(errCh)
for _, targetURL := range targetURLs {
tgtClient, err := target2Client(targetURL)
@@ -91,11 +93,23 @@ func putTargets(targetURLs []string, length int64, reader io.Reader) *probe.Erro
go func() {
var writers []io.Writer
for _, tgtWriter := range tgtWriters {
defer tgtWriter.Close()
writers = append(writers, io.Writer(tgtWriter))
}
multiTgtWriter := io.MultiWriter(writers...)
io.CopyN(multiTgtWriter, reader, length)
var e error
switch length {
case 0:
_, e = io.Copy(multiTgtWriter, reader)
default:
_, e = io.CopyN(multiTgtWriter, reader, length)
}
for _, tgtWriter := range tgtWriters {
if e != nil {
tgtWriter.CloseWithError(e)
}
tgtWriter.Close()
}
}()
var wg sync.WaitGroup
@@ -111,22 +125,23 @@ func putTargets(targetURLs []string, length int64, reader io.Reader) *probe.Erro
go func(targetClient client.Client, reader io.ReadCloser, errorCh chan<- *probe.Error) {
defer wg.Done()
defer reader.Close()
err := targetClient.PutObject(length, reader)
if err != nil {
defer reader.Close()
errorCh <- err.Trace()
return
}
errorCh <- nil
}(tgtClient, tgtReader, errorCh)
}
wg.Wait()
}()
for err := range errorCh {
if err != nil { // Return on first error encounter.
return err.Trace()
}
// Return on first error encounter.
err := <-errorCh
if err != nil {
return err.Trace()
}
return nil // success.
}
+1
View File
@@ -119,6 +119,7 @@ func registerApp() *cli.App {
registerCmd(lsCmd) // List contents of a bucket.
registerCmd(mbCmd) // Make a bucket.
registerCmd(catCmd) // Display contents of a file.
registerCmd(pigCmd) // Write contents of stdin to a file.
registerCmd(cpCmd) // Copy objects and files from multiple sources to single destination.
registerCmd(mirrorCmd) // Mirror objects and files from single source to multiple destinations.
registerCmd(sessionCmd) // Manage sessions for copy and mirror.
+86
View File
@@ -0,0 +1,86 @@
/*
* 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 (
"os"
"syscall"
"github.com/minio/cli"
"github.com/minio/minio/pkg/probe"
)
// Display contents of a file.
var pigCmd = cli.Command{
Name: "pig",
Usage: "Write contents of stdin to files. Pig is the opposite of cat command.",
Action: mainPig,
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} TARGET [TARGET...]
EXAMPLES:
1. Write contents of stdin to an object on Amazon S3 cloud storage.
$ mc {{.Name}} https://s3.amazonaws.com/personalbuck/meeting-notes.txt
2. Concatinate part files to an object on Amazon S3 cloud storage.
$ cat part1.img part2.img | mc {{.Name}} https://s3.amazonaws.com/ferenginar/gnuos.iso
3. Stream MySQL database dump to Amazon S3 directly.
$ mysqldump -u root -p ******* accountsdb | mc {{.Name}} https://s3.amazonaws.com/ferenginar/backups/accountsdb-oct-9-2015.sql
4. Contatinate a zip file to two object storage servers simultaneously.
$ cat ~/myphotos.zip | mc {{.Name}} https://s3.amazonaws.com/mybucket/photos.zip https://minio.mystartup.io:9000/backup/photos.zip
`,
}
// checkPigSyntax performs command-line input validation for pig command.
func checkPigSyntax(ctx *cli.Context) {
if !ctx.Args().Present() || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "pig", 1) // last argument is exit code
}
}
// pig writes contents of stdin a collection of URLs.
func pig(targetURLs []string) *probe.Error {
URLs := []string{}
config := mustGetMcConfig()
for _, URL := range targetURLs {
URLs = append(URLs, getAliasURL(URL, config.Aliases))
}
//Stream from stdin to multiple objects until EOF.
// Ignore size, since os.Stat() would not return proper size all the time for local filesystem for example /proc files.
err := putTargets(URLs, 0, os.Stdin)
// TODO: See if this check is necessary.
switch e := err.ToGoError().(type) {
case *os.PathError:
if e.Err == syscall.EPIPE {
// stdin closed by the user. Gracefully exit.
return nil
}
}
return err.Trace()
}
// mainPig is the main entry point for pig command.
func mainPig(ctx *cli.Context) {
checkPigSyntax(ctx)
fatalIf(pig(ctx.Args()).Trace(ctx.Args()...), "Unable to write to one or more targets.")
}