2011-03-01 16:29:44 +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.
|
|
|
|
*/
|
2010-07-11 05:57:53 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2012-10-21 20:58:06 +00:00
|
|
|
"io"
|
2010-12-20 22:57:41 +00:00
|
|
|
"log"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
2010-07-11 05:57:53 +00:00
|
|
|
"os"
|
2012-10-21 20:58:06 +00:00
|
|
|
"sort"
|
2011-05-16 04:54:31 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/client"
|
|
|
|
"camlistore.org/pkg/jsonsign"
|
2010-07-11 05:57:53 +00:00
|
|
|
)
|
|
|
|
|
2011-09-25 00:20:47 +00:00
|
|
|
const buffered = 16 // arbitrary
|
|
|
|
|
|
|
|
var (
|
2012-10-19 10:50:43 +00:00
|
|
|
flagHelp = flag.Bool("help", false, "print usage")
|
2011-09-27 03:02:35 +00:00
|
|
|
flagVerbose = flag.Bool("verbose", false, "extra debug logging")
|
2011-09-25 00:20:47 +00:00
|
|
|
)
|
2011-09-17 23:59:04 +00:00
|
|
|
|
2011-09-29 17:37:24 +00:00
|
|
|
var ErrUsage = UsageError("invalid command usage")
|
2010-12-20 22:13:57 +00:00
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
type UsageError string
|
2011-03-14 05:21:58 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (ue UsageError) Error() string {
|
2011-09-27 03:02:35 +00:00
|
|
|
return "Usage error: " + string(ue)
|
|
|
|
}
|
2011-09-26 00:40:01 +00:00
|
|
|
|
|
|
|
type CommandRunner interface {
|
|
|
|
Usage()
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
RunCommand(up *Uploader, args []string) error
|
2011-09-26 00:40:01 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
type Exampler interface {
|
|
|
|
Examples() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var modeCommand = make(map[string]CommandRunner)
|
|
|
|
var modeFlags = make(map[string]*flag.FlagSet)
|
2011-09-26 00:40:01 +00:00
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
func RegisterCommand(mode string, makeCmd func(Flags *flag.FlagSet) CommandRunner) {
|
|
|
|
if _, dup := modeCommand[mode]; dup {
|
2011-09-26 00:40:01 +00:00
|
|
|
log.Fatalf("duplicate command %q registered", mode)
|
|
|
|
}
|
2011-09-27 03:02:35 +00:00
|
|
|
flags := flag.NewFlagSet(mode+" options", flag.ContinueOnError)
|
|
|
|
flags.Usage = func() {}
|
|
|
|
modeFlags[mode] = flags
|
|
|
|
modeCommand[mode] = makeCmd(flags)
|
2011-09-26 00:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wereErrors gets set to true if any error was encountered, which
|
|
|
|
// changes the os.Exit value
|
2010-12-24 18:04:08 +00:00
|
|
|
var wereErrors = false
|
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
type namedMode struct {
|
|
|
|
Name string
|
|
|
|
Command CommandRunner
|
|
|
|
}
|
|
|
|
|
|
|
|
func allModes(startModes []string) <-chan namedMode {
|
|
|
|
ch := make(chan namedMode)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
done := map[string]bool{}
|
|
|
|
for _, name := range startModes {
|
|
|
|
done[name] = true
|
|
|
|
cmd := modeCommand[name]
|
|
|
|
if cmd == nil {
|
|
|
|
panic("bogus mode: " + name)
|
|
|
|
}
|
|
|
|
ch <- namedMode{name, cmd}
|
|
|
|
}
|
2012-10-21 20:58:06 +00:00
|
|
|
var rest []string
|
|
|
|
for name := range modeCommand {
|
2011-09-27 03:02:35 +00:00
|
|
|
if !done[name] {
|
2012-10-21 20:58:06 +00:00
|
|
|
rest = append(rest, name)
|
2011-09-27 03:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-21 20:58:06 +00:00
|
|
|
sort.Strings(rest)
|
|
|
|
for _, name := range rest {
|
|
|
|
ch <- namedMode{name, modeCommand[name]}
|
|
|
|
}
|
2011-09-27 03:02:35 +00:00
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func errf(format string, args ...interface{}) {
|
2012-10-21 20:58:06 +00:00
|
|
|
fmt.Fprintf(stderr, format, args...)
|
2011-09-27 03:02:35 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 22:57:41 +00:00
|
|
|
func usage(msg string) {
|
|
|
|
if msg != "" {
|
2011-09-27 03:02:35 +00:00
|
|
|
errf("Error: %v\n", msg)
|
2010-12-20 22:57:41 +00:00
|
|
|
}
|
2011-09-27 03:02:35 +00:00
|
|
|
errf(`
|
2011-09-26 00:40:01 +00:00
|
|
|
Usage: camput [globalopts] <mode> [commandopts] [commandargs]
|
|
|
|
|
|
|
|
Examples:
|
2011-09-27 03:02:35 +00:00
|
|
|
`)
|
|
|
|
order := []string{"init", "file", "permanode", "blob", "attr"}
|
|
|
|
for mode := range allModes(order) {
|
|
|
|
errf("\n")
|
|
|
|
if ex, ok := mode.Command.(Exampler); ok {
|
|
|
|
for _, example := range ex.Examples() {
|
|
|
|
errf(" camput %s %s\n", mode.Name, example)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errf(" camput %s ...\n", mode.Name)
|
|
|
|
}
|
|
|
|
}
|
2011-09-26 00:40:01 +00:00
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
errf(`
|
2011-09-26 00:40:01 +00:00
|
|
|
For mode-specific help:
|
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
camput <mode> -help
|
2011-09-26 00:40:01 +00:00
|
|
|
|
|
|
|
Global options:
|
2010-12-20 22:57:41 +00:00
|
|
|
`)
|
|
|
|
flag.PrintDefaults()
|
2012-10-21 21:14:51 +00:00
|
|
|
exit(1)
|
2010-12-20 22:57:41 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 02:36:00 +00:00
|
|
|
func handleResult(what string, pr *client.PutResult, err error) error {
|
2010-12-24 15:46:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error putting %s: %s", what, err)
|
|
|
|
wereErrors = true
|
2012-04-13 02:36:00 +00:00
|
|
|
return err
|
2010-12-24 15:46:12 +00:00
|
|
|
}
|
2011-09-25 00:20:47 +00:00
|
|
|
fmt.Println(pr.BlobRef.String())
|
2012-04-13 02:36:00 +00:00
|
|
|
return nil
|
2010-12-24 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
2012-04-21 14:22:32 +00:00
|
|
|
func newUploader() *Uploader {
|
2011-03-12 21:10:56 +00:00
|
|
|
cc := client.NewOrFail()
|
2011-01-18 18:29:38 +00:00
|
|
|
if !*flagVerbose {
|
2011-03-12 21:10:56 +00:00
|
|
|
cc.SetLogger(nil)
|
2011-01-18 18:29:38 +00:00
|
|
|
}
|
2011-09-17 01:03:13 +00:00
|
|
|
|
2012-11-08 03:00:22 +00:00
|
|
|
httpStats := &statsTransport{
|
2012-11-08 04:21:52 +00:00
|
|
|
transport: &http.Transport{DisableKeepAlives: false},
|
2012-11-08 03:00:22 +00:00
|
|
|
}
|
2012-11-08 03:03:46 +00:00
|
|
|
cc.SetHTTPClient(&http.Client{Transport: httpStats})
|
2011-09-17 22:14:37 +00:00
|
|
|
|
2011-09-17 01:03:13 +00:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("os.Getwd: %v", err)
|
|
|
|
}
|
|
|
|
|
2012-04-21 14:22:32 +00:00
|
|
|
return &Uploader{
|
2011-09-26 00:40:01 +00:00
|
|
|
Client: cc,
|
2012-11-08 03:00:22 +00:00
|
|
|
transport: httpStats,
|
2011-09-26 00:40:01 +00:00
|
|
|
pwd: pwd,
|
2011-05-16 21:35:38 +00:00
|
|
|
entityFetcher: &jsonsign.CachingEntityFetcher{
|
2011-05-30 19:38:26 +00:00
|
|
|
Fetcher: &jsonsign.FileEntityFetcher{File: cc.SecretRingFile()},
|
2011-05-16 21:35:38 +00:00
|
|
|
},
|
|
|
|
}
|
2011-09-27 03:02:35 +00:00
|
|
|
}
|
2011-09-17 22:14:37 +00:00
|
|
|
|
2011-09-27 03:02:35 +00:00
|
|
|
func hasFlags(flags *flag.FlagSet) bool {
|
|
|
|
any := false
|
|
|
|
flags.VisitAll(func(*flag.Flag) {
|
|
|
|
any = true
|
|
|
|
})
|
|
|
|
return any
|
|
|
|
}
|
|
|
|
|
2011-09-26 00:40:01 +00:00
|
|
|
func main() {
|
|
|
|
jsonsign.AddFlags()
|
2011-10-11 00:38:18 +00:00
|
|
|
client.AddFlags()
|
2011-09-26 00:40:01 +00:00
|
|
|
flag.Parse()
|
2012-10-21 20:58:06 +00:00
|
|
|
camputMain(flag.Args()...)
|
|
|
|
}
|
|
|
|
|
2012-10-21 21:14:51 +00:00
|
|
|
func realExit(code int) {
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2012-10-21 20:58:06 +00:00
|
|
|
// Indirections for replacement by tests:
|
|
|
|
var (
|
|
|
|
stderr io.Writer = os.Stderr
|
|
|
|
stdout io.Writer = os.Stdout
|
|
|
|
stdin io.Reader = os.Stdin
|
2012-10-21 21:14:51 +00:00
|
|
|
|
|
|
|
exit = realExit
|
|
|
|
|
|
|
|
// TODO: abstract out vfs operation. should never call os.Stat, os.Open, os.Create, etc.
|
|
|
|
// Only use fs.Stat, fs.Open, where vs is an interface type.
|
|
|
|
|
|
|
|
// TODO: switch from using the global flag FlagSet and use our own. right now
|
|
|
|
// running "go test -v" dumps the flag usage data to the global stderr.
|
2012-10-21 20:58:06 +00:00
|
|
|
)
|
2011-09-26 00:40:01 +00:00
|
|
|
|
2012-10-21 20:58:06 +00:00
|
|
|
// camputMain is separated from main for testing from camput
|
|
|
|
func camputMain(args ...string) {
|
2012-10-19 10:50:43 +00:00
|
|
|
if *flagHelp {
|
|
|
|
usage("")
|
|
|
|
}
|
2012-10-21 20:58:06 +00:00
|
|
|
if len(args) == 0 {
|
2011-09-26 00:40:01 +00:00
|
|
|
usage("No mode given.")
|
|
|
|
}
|
|
|
|
|
2012-10-21 20:58:06 +00:00
|
|
|
mode := args[0]
|
2011-09-27 03:02:35 +00:00
|
|
|
cmd, ok := modeCommand[mode]
|
2011-09-26 00:40:01 +00:00
|
|
|
if !ok {
|
|
|
|
usage(fmt.Sprintf("Unknown mode %q", mode))
|
|
|
|
}
|
|
|
|
|
2012-04-13 02:36:00 +00:00
|
|
|
var up *Uploader
|
|
|
|
if mode != "init" {
|
2012-04-21 14:22:32 +00:00
|
|
|
up = newUploader()
|
2012-04-13 02:36:00 +00:00
|
|
|
}
|
2011-09-27 03:02:35 +00:00
|
|
|
|
|
|
|
cmdFlags := modeFlags[mode]
|
2012-10-21 20:58:06 +00:00
|
|
|
err := cmdFlags.Parse(args[1:])
|
2011-09-27 03:02:35 +00:00
|
|
|
if err != nil {
|
|
|
|
err = ErrUsage
|
|
|
|
} else {
|
|
|
|
err = cmd.RunCommand(up, cmdFlags.Args())
|
|
|
|
}
|
2011-09-29 17:37:24 +00:00
|
|
|
if ue, isUsage := err.(UsageError); isUsage {
|
2011-09-27 03:02:35 +00:00
|
|
|
if isUsage {
|
|
|
|
errf("%s\n", ue)
|
|
|
|
}
|
2011-09-26 00:40:01 +00:00
|
|
|
cmd.Usage()
|
2011-09-27 03:02:35 +00:00
|
|
|
errf("\nGlobal options:\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
|
|
|
|
if hasFlags(cmdFlags) {
|
|
|
|
errf("\nMode-specific options for mode %q:\n", mode)
|
|
|
|
cmdFlags.PrintDefaults()
|
|
|
|
}
|
2012-10-21 21:14:51 +00:00
|
|
|
exit(1)
|
2011-09-26 00:40:01 +00:00
|
|
|
}
|
|
|
|
if *flagVerbose {
|
|
|
|
stats := up.Stats()
|
|
|
|
log.Printf("Client stats: %s", stats.String())
|
|
|
|
log.Printf(" #HTTP reqs: %d", up.transport.reqs)
|
|
|
|
}
|
2012-10-23 16:28:42 +00:00
|
|
|
previousErrors := wereErrors
|
|
|
|
if err != nil {
|
|
|
|
wereErrors = true
|
|
|
|
if !previousErrors {
|
|
|
|
log.Printf("Error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if wereErrors {
|
2012-10-21 21:14:51 +00:00
|
|
|
exit(2)
|
2011-09-26 00:40:01 +00:00
|
|
|
}
|
|
|
|
}
|