github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/dev/devcam/camget.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 "get" subcommand to devcam, to run camget against the dev server.
    18  
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	"path/filepath"
    25  	"regexp"
    26  	"strconv"
    27  	"strings"
    28  
    29  	"camlistore.org/pkg/cmdmain"
    30  )
    31  
    32  type getCmd struct {
    33  	// start of flag vars
    34  	altkey bool
    35  	path   string
    36  	port   string
    37  	tls    bool
    38  	// end of flag vars
    39  
    40  	env *Env
    41  }
    42  
    43  func init() {
    44  	cmdmain.RegisterCommand("get", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    45  		cmd := &getCmd{
    46  			env: NewCopyEnv(),
    47  		}
    48  		flags.BoolVar(&cmd.altkey, "altkey", false, "Use different gpg key and password from the server's.")
    49  		flags.StringVar(&cmd.path, "path", "/bs", "Optional URL prefix path.")
    50  		flags.StringVar(&cmd.port, "port", "3179", "Port camlistore is listening on.")
    51  		flags.BoolVar(&cmd.tls, "tls", false, "Use TLS.")
    52  		return cmd
    53  	})
    54  }
    55  
    56  func (c *getCmd) Usage() {
    57  	fmt.Fprintf(cmdmain.Stderr, "Usage: devcam get [get_opts] -- camget_args\n")
    58  }
    59  
    60  func (c *getCmd) Examples() []string {
    61  	return []string{
    62  		"<blobref>",
    63  		"-- --shared http://localhost:3169/share/<blobref>",
    64  	}
    65  }
    66  
    67  func (c *getCmd) Describe() string {
    68  	return "run camget in dev mode."
    69  }
    70  
    71  func (c *getCmd) RunCommand(args []string) error {
    72  	err := c.checkFlags(args)
    73  	if err != nil {
    74  		return cmdmain.UsageError(fmt.Sprint(err))
    75  	}
    76  	if !*noBuild {
    77  		if err := build(filepath.Join("cmd", "camget")); err != nil {
    78  			return fmt.Errorf("Could not build camget: %v", err)
    79  		}
    80  	}
    81  	c.env.SetCamdevVars(c.altkey)
    82  
    83  	cmdBin := filepath.Join("bin", "camget")
    84  	cmdArgs := []string{
    85  		"-verbose=" + strconv.FormatBool(*cmdmain.FlagVerbose || !quiet),
    86  	}
    87  	if !isSharedMode(args) {
    88  		blobserver := "http://localhost:" + c.port + c.path
    89  		if c.tls {
    90  			blobserver = strings.Replace(blobserver, "http://", "https://", 1)
    91  		}
    92  		cmdArgs = append(cmdArgs, "-server="+blobserver)
    93  	}
    94  	cmdArgs = append(cmdArgs, args...)
    95  	return runExec(cmdBin, cmdArgs, c.env)
    96  }
    97  
    98  func (c *getCmd) checkFlags(args []string) error {
    99  	if _, err := strconv.ParseInt(c.port, 0, 0); err != nil {
   100  		return fmt.Errorf("Invalid -port value: %q", c.port)
   101  	}
   102  	return nil
   103  }
   104  
   105  func isSharedMode(args []string) bool {
   106  	sharedRgx := regexp.MustCompile("--?shared")
   107  	for _, v := range args {
   108  		if sharedRgx.MatchString(v) {
   109  			return true
   110  		}
   111  	}
   112  	return false
   113  }