github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/dev/devcam/camput.go (about)

     1  /*
     2  Copyright 2013 The Camlistore Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This file adds the "put" subcommand to devcam, to run camput against the dev server.
    18  
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	"path/filepath"
    25  	"strconv"
    26  	"strings"
    27  
    28  	"camlistore.org/pkg/cmdmain"
    29  )
    30  
    31  type putCmd struct {
    32  	// start of flag vars
    33  	altkey bool
    34  	path   string
    35  	port   string
    36  	tls    bool
    37  	// end of flag vars
    38  
    39  	env *Env
    40  }
    41  
    42  func init() {
    43  	cmdmain.RegisterCommand("put", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    44  		cmd := &putCmd{
    45  			env: NewCopyEnv(),
    46  		}
    47  		flags.BoolVar(&cmd.altkey, "altkey", false, "Use different gpg key and password from the server's.")
    48  		flags.BoolVar(&cmd.tls, "tls", false, "Use TLS.")
    49  		flags.StringVar(&cmd.path, "path", "/", "Optional URL prefix path.")
    50  		flags.StringVar(&cmd.port, "port", "3179", "Port camlistore is listening on.")
    51  		return cmd
    52  	})
    53  }
    54  
    55  func (c *putCmd) Usage() {
    56  	fmt.Fprintf(cmdmain.Stderr, "Usage: devcam put [put_opts] camput_args\n")
    57  }
    58  
    59  func (c *putCmd) Examples() []string {
    60  	return []string{
    61  		"file --filenodes /mnt/camera/DCIM",
    62  	}
    63  }
    64  
    65  func (c *putCmd) Describe() string {
    66  	return "run camput in dev mode."
    67  }
    68  
    69  func (c *putCmd) RunCommand(args []string) error {
    70  	err := c.checkFlags(args)
    71  	if err != nil {
    72  		return cmdmain.UsageError(fmt.Sprint(err))
    73  	}
    74  	if !*noBuild {
    75  		if err := build(filepath.Join("cmd", "camput")); err != nil {
    76  			return fmt.Errorf("Could not build camput: %v", err)
    77  		}
    78  	}
    79  	c.env.SetCamdevVars(c.altkey)
    80  
    81  	blobserver := "http://localhost:" + c.port + c.path
    82  	if c.tls {
    83  		blobserver = strings.Replace(blobserver, "http://", "https://", 1)
    84  	}
    85  
    86  	cmdBin := filepath.Join("bin", "camput")
    87  	cmdArgs := []string{
    88  		"-verbose=" + strconv.FormatBool(*cmdmain.FlagVerbose || !quiet),
    89  		"-server=" + blobserver,
    90  	}
    91  	cmdArgs = append(cmdArgs, args...)
    92  	return runExec(cmdBin, cmdArgs, c.env)
    93  }
    94  
    95  func (c *putCmd) checkFlags(args []string) error {
    96  	if _, err := strconv.ParseInt(c.port, 0, 0); err != nil {
    97  		return fmt.Errorf("Invalid -port value: %q", c.port)
    98  	}
    99  	return nil
   100  }