2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// Writes to stdout by default:
|
|
|
|
// camget BLOBREF
|
|
|
|
//
|
|
|
|
// Like curl, lets you set output file/directory with -o:
|
|
|
|
// camget -o dir BLOBREF (if dir exists and is directory, BLOBREF must be a directory, and -f to overwrite any files)
|
|
|
|
// camget -o file BLOBREF
|
|
|
|
//
|
|
|
|
// Should be possible to get a directory JSON blob without recursively
|
|
|
|
// fetching an entire directory. Likewise with files. But default
|
|
|
|
// should be sensitive on the type of the listed blob. Maybe --blob
|
|
|
|
// just to get the blob? Seems consistent.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"camli/blobref"
|
|
|
|
"camli/client"
|
|
|
|
"flag"
|
2011-01-15 01:25:37 +00:00
|
|
|
"io"
|
2011-01-02 22:36:03 +00:00
|
|
|
"log"
|
2011-01-15 01:25:37 +00:00
|
|
|
"os"
|
2011-01-26 05:33:19 +00:00
|
|
|
"strings"
|
2011-01-02 22:36:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var flagVerbose *bool = flag.Bool("verbose", false, "be verbose")
|
|
|
|
|
|
|
|
var flagCheck *bool = flag.Bool("check", false, "just check for the existence of listed blobs; returning 0 if all our present")
|
|
|
|
var flagOutput *string = flag.String("o", "-", "Output file/directory to create. Use -f to overwrite.")
|
2011-01-26 05:33:19 +00:00
|
|
|
var flagVia *string = flag.String("via", "", "Fetch the blob via the given comma-separated sharerefs (dev only).")
|
2011-01-02 22:36:03 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
client := client.NewOrFail()
|
|
|
|
if *flagCheck {
|
|
|
|
// Simply do HEAD requests checking if the blobs exists.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-01-15 01:25:37 +00:00
|
|
|
var w io.Writer = os.Stdout
|
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
for n := 0; n < flag.NArg(); n++ {
|
|
|
|
arg := flag.Arg(n)
|
2011-01-26 05:33:19 +00:00
|
|
|
br := blobref.Parse(arg)
|
|
|
|
if br == nil {
|
2011-02-02 20:27:30 +00:00
|
|
|
log.Fatalf("Failed to parse argument \"%s\" as a blobref.", arg)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
if *flagVerbose {
|
2011-01-26 05:33:19 +00:00
|
|
|
log.Printf("Need to fetch %s", br.String())
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
r blobref.ReadSeekCloser
|
|
|
|
err os.Error
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(*flagVia) > 0 {
|
|
|
|
vs := strings.Split(*flagVia, ",", -1)
|
|
|
|
abr := make([]*blobref.BlobRef, len(vs))
|
|
|
|
for i, sbr := range vs {
|
|
|
|
abr[i] = blobref.Parse(sbr)
|
|
|
|
if abr[i] == nil {
|
2011-02-02 20:27:30 +00:00
|
|
|
log.Fatalf("Invalid -via blobref: %q", sbr)
|
2011-01-26 05:33:19 +00:00
|
|
|
}
|
|
|
|
if *flagVerbose {
|
|
|
|
log.Printf("via: %s", sbr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r, _, err = client.FetchVia(br, abr)
|
|
|
|
} else {
|
|
|
|
r, _, err = client.Fetch(br)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2011-01-15 01:25:37 +00:00
|
|
|
if err != nil {
|
2011-02-02 20:27:30 +00:00
|
|
|
log.Fatalf("Failed to fetch %q: %s", br, err)
|
2011-01-15 01:25:37 +00:00
|
|
|
}
|
|
|
|
_, err = io.Copy(w, r)
|
|
|
|
if err != nil {
|
2011-02-02 20:27:30 +00:00
|
|
|
log.Fatalf("Failed transferring %q: %s", br, err)
|
2011-01-15 01:25:37 +00:00
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|