perkeep/dev/devcam/camput.go

157 lines
4.3 KiB
Go
Raw Normal View History

/*
Copyright 2013 The Camlistore Authors.
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.
*/
// This program runs camput in "dev" mode,
// to facilitate hacking on camlistore.
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"camlistore.org/pkg/cmdmain"
"camlistore.org/pkg/osutil"
)
type putCmd struct {
// start of flag vars
altkey bool
path string
port string
tls bool
// end of flag vars
verbose string // set by CAMLI_QUIET
camliSrcRoot string // the camlistore source tree
}
func init() {
cmdmain.RegisterCommand("put", func(flags *flag.FlagSet) cmdmain.CommandRunner {
cmd := new(putCmd)
flags.BoolVar(&cmd.altkey, "altkey", false, "Use different gpg key and password from the server's.")
flags.BoolVar(&cmd.tls, "tls", false, "Use TLS.")
flags.StringVar(&cmd.path, "path", "/", "Optional URL prefix path.")
flags.StringVar(&cmd.port, "port", "3179", "Port camlistore is listening on.")
cmd.verbose = "false"
return cmd
})
}
func (c *putCmd) Usage() {
fmt.Fprintf(cmdmain.Stderr, "Usage: devcam put [put_opts] camput_args\n")
}
func (c *putCmd) Examples() []string {
return []string{
"file --filenodes /mnt/camera/DCIM",
}
}
func (c *putCmd) Describe() string {
return "run camput in dev mode."
}
func (c *putCmd) RunCommand(args []string) error {
err := c.checkFlags(args)
if err != nil {
return cmdmain.UsageError(fmt.Sprint(err))
}
c.camliSrcRoot, err = osutil.GoPackagePath("camlistore.org")
if err != nil {
return errors.New("Package camlistore.org not found in $GOPATH (or $GOPATH not defined).")
}
err = os.Chdir(c.camliSrcRoot)
if err != nil {
return fmt.Errorf("Could not chdir to %v: %v", c.camliSrcRoot, err)
}
if err := c.setEnvVars(); err != nil {
return fmt.Errorf("Could not setup the env vars: %v", err)
}
blobserver := "http://localhost:" + c.port + c.path
if c.tls {
blobserver = strings.Replace(blobserver, "http://", "https://", 1)
}
cmdBin := filepath.Join(c.camliSrcRoot, "bin", "camput")
cmdArgs := []string{
"-verbose=" + c.verbose,
"-server=" + blobserver,
}
cmdArgs = append(cmdArgs, args...)
return runExec(cmdBin, cmdArgs)
}
func (c *putCmd) checkFlags(args []string) error {
if _, err := strconv.ParseInt(c.port, 0, 0); err != nil {
return fmt.Errorf("Invalid -port value: %q", c.port)
}
return nil
}
func (c *putCmd) build(name string) error {
cmdName := "camput"
target := filepath.Join("camlistore.org", "cmd", cmdName)
binPath := filepath.Join(c.camliSrcRoot, "bin", cmdName)
var modtime int64
fi, err := os.Stat(binPath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Could not stat %v: %v", binPath, err)
}
} else {
modtime = fi.ModTime().Unix()
}
args := []string{
"run", "make.go",
"--quiet",
"--embed_static=false",
fmt.Sprintf("--if_mods_since=%d", modtime),
"--targets=" + target,
}
cmd := exec.Command("go", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("Error building %v: %v", target, err)
}
return nil
}
func (c *putCmd) setEnvVars() error {
setenv("CAMLI_CONFIG_DIR", filepath.Join(c.camliSrcRoot, "config", "dev-client-dir"))
setenv("CAMLI_SECRET_RING", filepath.Join(c.camliSrcRoot,
filepath.FromSlash("pkg/jsonsign/testdata/test-secring.gpg")))
setenv("CAMLI_KEYID", "26F5ABDA")
setenv("CAMLI_AUTH", "userpass:camlistore:pass3179")
setenv("CAMLI_DEV_KEYBLOBS", filepath.Join(c.camliSrcRoot,
filepath.FromSlash("config/dev-client-dir/keyblobs")))
if c.altkey {
setenv("CAMLI_SECRET_RING", filepath.Join(c.camliSrcRoot,
filepath.FromSlash("pkg/jsonsign/testdata/password-foo-secring.gpg")))
setenv("CAMLI_KEYID", "C7C3E176")
println("**\n** Note: password is \"foo\"\n**\n")
}
Document environment variables usage. Running 'go run dev/envvardoc/envvardoc.go' now shows: 'All environment variables are documented' I also took the liberty of cleaning-up our mishmash of logic for handling boolean environment variables, and cleaned up a couple other spots that didn't seem right. This change adds docmentation for all variables starting with (CAM|DEV|AWS). This leaves some variables still undocumented. If there are variables worth documenting in the following list, maybe we should rename them to have a CAM{LI} prefix for consistency's sake: APPDATA pkg/osutil/paths.go:86 APPDATA pkg/osutil/paths.go:102 DISPLAY pkg/misc/gpgagent/gpgagent.go:126 GOPATH pkg/fileembed/genfileembed/genfileembed.go:321 GOPATH pkg/osutil/paths.go:168 GOPATH pkg/test/world.go:54 GOPATH server/appengine/build_test.go:77 GPGKEY cmd/camput/init.go:77 GPG_AGENT_INFO cmd/camput/init.go:153 GPG_AGENT_INFO pkg/misc/gpgagent/gpgagent.go:50 HOME pkg/jsonsign/keys.go:79 HOME pkg/jsonsign/signhandler/sig.go:64 HOME pkg/osutil/paths.go:36 HOMEPATH pkg/osutil/paths.go:34 PKG_CONFIG_PATH pkg/index/sqlite/dbschema.go:59 RUN_BROKEN_TESTS pkg/fs/fs_test.go:67 SKIP_DEP_TESTS pkg/test/testdep.go:29 TERM pkg/misc/gpgagent/gpgagent.go:133 TERM pkg/misc/pinentry/pinentry.go:99 TESTING_PORT_WRITE_FD pkg/webserver/webserver.go:135 TEST_GPGAGENT_LIB pkg/misc/gpgagent/gpgagent_test.go:27 USER pkg/netutil/ident.go:135 USER pkg/osutil/paths.go:45 USERNAME pkg/jsonconfig/eval.go:228 USERNAME pkg/osutil/paths.go:43 VERBOSE_FUSE pkg/fs/fs_test.go:133 VERBOSE_FUSE_STDERR pkg/fs/fs_test.go:137 XDG_CONFIG_HOME pkg/osutil/paths.go:104 Change-Id: Ief28710d3deefd1e65247cb5d3b1d8dde73e1f2d
2013-09-06 04:07:15 +00:00
c.verbose, _ = strconv.ParseBool(os.Getenv("CAMLI_QUIET"))
return nil
}