mirror of https://github.com/perkeep/perkeep.git
blob command for new camput cli
Change-Id: I7e20119761c0670aa2d01b5f7f38439eddcf4304
This commit is contained in:
parent
12f236da37
commit
93da4a48d3
|
@ -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 <files>\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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue