diff --git a/clients/go/camput/blobs.go b/clients/go/camput/blobs.go new file mode 100644 index 000000000..4f711d10f --- /dev/null +++ b/clients/go/camput/blobs.go @@ -0,0 +1,63 @@ +/* +Copyright 2011 Google 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 ( + "flag" + "fmt" + "os" + + "camli/client" +) + +type blobCmd struct { + flags *flag.FlagSet +} + +func init() { + flags := flag.NewFlagSet("blob options", flag.ContinueOnError) + flags.Usage = func() {} + cmd := &blobCmd{flags: flags} + + RegisterCommand("blob", cmd) +} + +func (c *blobCmd) Usage() { + fmt.Fprintf(os.Stderr, "Usage: camput [globalopts] blob \n camput [globalopts] blob -\n") + c.flags.PrintDefaults() +} + +func (c *blobCmd) RunCommand(up *Uploader, args []string) os.Error { + if err := c.flags.Parse(args); err != nil { + return ErrUsage + } + args = c.flags.Args() + if len(args) == 0 { + return os.NewError("No files given.") + } + + var ( + err os.Error + put *client.PutResult + ) + + for _, arg := range args { + put, err = up.UploadFileBlob(arg) + handleResult("blob", put, err) + } + return nil +} diff --git a/clients/go/camput/camput.go b/clients/go/camput/camput.go index 2d9952c0e..d2bd25f60 100644 --- a/clients/go/camput/camput.go +++ b/clients/go/camput/camput.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "bytes" "crypto/sha1" "flag" "fmt" @@ -108,24 +109,47 @@ func blobDetails(contents io.ReadSeeker) (bref *blobref.BlobRef, size int64, err } func (up *Uploader) UploadFileBlob(filename string) (*client.PutResult, os.Error) { - fi, err := os.Stat(filename) - if err != nil { - return nil, err - } - if !fi.IsRegular() { - return nil, fmt.Errorf("%q is not a regular file", filename) - } - file, err := os.Open(filename) - if err != nil { - return nil, err + var ( + err os.Error + size int64 + ref *blobref.BlobRef + body io.Reader + ) + if filename == "-" { + buf := bytes.NewBuffer(make([]byte, 0)) + size, err = io.Copy(buf, os.Stdin) + if err != nil { + return nil, err + } + // assuming what I did here is not too lame, maybe I should set a limit on how much we accept from the stdin? + file := buf.Bytes() + s1 := sha1.New() + size, err = io.Copy(s1, buf) + if err != nil { + return nil, err + } + ref = blobref.FromHash("sha1", s1) + body = io.LimitReader(bytes.NewBuffer(file), size) + } else { + fi, err := os.Stat(filename) + if err != nil { + return nil, err + } + if !fi.IsRegular() { + return nil, fmt.Errorf("%q is not a regular file", filename) + } + file, err := os.Open(filename) + if err != nil { + return nil, err + } + ref, size, err = blobDetails(file) + if err != nil { + return nil, err + } + file.Seek(0, 0) + body = io.LimitReader(file, size) } - ref, size, err := blobDetails(file) - if err != nil { - return nil, err - } - file.Seek(0, 0) - body := io.LimitReader(file, size) handle := &client.UploadHandle{ref, size, body} return up.Upload(handle) }