1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00

Make some doc changes to cat cmd

This commit is contained in:
Harshavardhana
2015-04-23 20:49:21 -07:00
parent 86c5986df3
commit bc68f1d9f4
3 changed files with 61 additions and 21 deletions

View File

@@ -25,6 +25,7 @@ import (
"sync" "sync"
"github.com/minio-io/cli" "github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/console" "github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/crypto/md5" "github.com/minio-io/minio/pkg/utils/crypto/md5"
@@ -92,26 +93,36 @@ func doCatCmd(manager clientManager, writer io.Writer, sourceURLConfigMap map[st
return "Unable to retrieve file: " + url, iodine.New(err, nil) return "Unable to retrieve file: " + url, iodine.New(err, nil)
} }
defer reader.Close() defer reader.Close()
wg := &sync.WaitGroup{} var teeReader io.Reader
md5Reader, md5Writer := io.Pipe()
var actualMd5 []byte var actualMd5 []byte
wg.Add(1) if client.GetType(url) != client.Filesystem {
go func() { wg := &sync.WaitGroup{}
actualMd5, _ = md5.Sum(md5Reader) md5Reader, md5Writer := io.Pipe()
// drop error, we'll catch later on if it fails wg.Add(1)
wg.Done() go func() {
}() actualMd5, _ = md5.Sum(md5Reader)
teeReader := io.TeeReader(reader, md5Writer) // drop error, we'll catch later on if it fails
wg.Done()
}()
teeReader = io.TeeReader(reader, md5Writer)
_, err = io.CopyN(writer, teeReader, size)
md5Writer.Close()
wg.Wait()
if err != nil {
return "Copying data from source failed: " + url, iodine.New(errors.New("Copy data from source failed"), nil)
}
actualMd5String := hex.EncodeToString(actualMd5)
if expectedMd5 != actualMd5String {
return "Copying data from source was corrupted in transit: " + url,
iodine.New(errors.New("Data copied from source was corrupted in transit"), nil)
}
return "", nil
}
teeReader = reader
_, err = io.CopyN(writer, teeReader, size) _, err = io.CopyN(writer, teeReader, size)
md5Writer.Close()
wg.Wait()
if err != nil { if err != nil {
return "Copying data from source failed: " + url, iodine.New(errors.New("Copy data from source failed"), nil) return "Copying data from source failed: " + url, iodine.New(errors.New("Copy data from source failed"), nil)
} }
actualMd5String := hex.EncodeToString(actualMd5)
if expectedMd5 != actualMd5String {
return "Copying data from source was corrupted in transit: " + url, iodine.New(errors.New("Data copied from source was corrupted in transit"), nil)
}
} }
return "", nil return "", nil
} }

View File

@@ -28,7 +28,7 @@ import (
var ( var (
catCmd = cli.Command{ catCmd = cli.Command{
Name: "cat", Name: "cat",
Usage: "Copy an object to standard out", Usage: "Concantenate an object to standard output",
Action: runCatCmd, Action: runCatCmd,
CustomHelpTemplate: `NAME: CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}} mc {{.Name}} - {{.Usage}}
@@ -44,11 +44,11 @@ OPTIONS:
{{end}}{{ end }} {{end}}{{ end }}
EXAMPLES: EXAMPLES:
1. Copy an object from Amazon S3 object storage to standard out. 1. Concantenate an object from Amazon S3 object storage to mplayer standard input
$ mc {{.Name}} https://s3.amazonaws.com/jukebox/klingon_opera_aktuh_maylotah.ogg $ mc {{.Name}} https://s3.amazonaws.com/jukebox/klingon_opera_aktuh_maylotah.ogg | mplayer -
2. Copy an object from the file system to standard out. 2. Concantenate a file from local filesystem to standard output.
$ mc {{.Name}} klingon_opera_aktuh_maylotah.ogg $ mc {{.Name}} khitomer-accords.txt
`, `,
} }

View File

@@ -484,7 +484,7 @@ func (s *CmdTestSuite) TestMbCmdOnFile(c *C) {
cl1.AssertExpectations(c) cl1.AssertExpectations(c)
} }
func (s *CmdTestSuite) TestCatCmd(c *C) { func (s *CmdTestSuite) TestCatCmdObject(c *C) {
sourceURL, err := getURL("http://example.com/bucket1/object1", nil) sourceURL, err := getURL("http://example.com/bucket1/object1", nil)
c.Assert(err, IsNil) c.Assert(err, IsNil)
@@ -514,3 +514,32 @@ func (s *CmdTestSuite) TestCatCmd(c *C) {
manager.AssertExpectations(c) manager.AssertExpectations(c)
cl1.AssertExpectations(c) cl1.AssertExpectations(c)
} }
func (s *CmdTestSuite) TestCatCmdFile(c *C) {
sourceURL, err := getURL("object1", nil)
c.Assert(err, IsNil)
manager := &MockclientManager{}
cl1 := &clientMocks.Client{}
data1 := "hello1"
dataLen1 := int64(len(data1))
sourceURLConfigMap := make(map[string]*hostConfig)
sourceConfig := new(hostConfig)
sourceConfig.AccessKeyID = ""
sourceConfig.SecretAccessKey = ""
sourceURLConfigMap[sourceURL] = sourceConfig
var results bytes.Buffer
manager.On("getNewClient", sourceURL, sourceConfig, false).Return(cl1, nil).Once()
cl1.On("Get").Return(ioutil.NopCloser(bytes.NewBufferString(data1)), dataLen1, "", nil)
msg, err := doCatCmd(manager, &results, sourceURLConfigMap, false)
c.Assert(msg, Equals, "")
c.Assert(err, IsNil)
c.Assert(data1, Equals, results.String())
manager.AssertExpectations(c)
cl1.AssertExpectations(c)
}