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

     1  /*
     2  Copyright 2011 Google Inc.
     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  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"time"
    23  
    24  	"camlistore.org/pkg/blob"
    25  	"camlistore.org/pkg/cmdmain"
    26  	"camlistore.org/pkg/schema"
    27  )
    28  
    29  type shareCmd struct {
    30  	transitive bool
    31  	duration   time.Duration // zero means forever
    32  }
    33  
    34  func init() {
    35  	cmdmain.RegisterCommand("share", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    36  		cmd := new(shareCmd)
    37  		flags.BoolVar(&cmd.transitive, "transitive", false, "share everything reachable from the given blobref")
    38  		flags.DurationVar(&cmd.duration, "duration", 0, "how long the share claim is valid for. The default of 0 means forever. For valid formats, see http://golang.org/pkg/time/#ParseDuration")
    39  		return cmd
    40  	})
    41  }
    42  
    43  func (c *shareCmd) Describe() string {
    44  	return `Grant access to a resource by making a "share" blob.`
    45  }
    46  
    47  func (c *shareCmd) Usage() {
    48  	fmt.Fprintf(cmdmain.Stderr, `Usage: camput share [opts] <blobref>
    49  `)
    50  }
    51  
    52  func (c *shareCmd) Examples() []string {
    53  	return []string{
    54  		"[opts] <blobref to share via haveref>",
    55  	}
    56  }
    57  
    58  func (c *shareCmd) RunCommand(args []string) error {
    59  	if len(args) != 1 {
    60  		return cmdmain.UsageError("share takes exactly one argument, a blobref")
    61  	}
    62  	target, ok := blob.Parse(args[0])
    63  	if !ok {
    64  		return cmdmain.UsageError("invalid blobref")
    65  	}
    66  	unsigned := schema.NewShareRef(schema.ShareHaveRef, target, c.transitive)
    67  	if c.duration != 0 {
    68  		unsigned.SetShareExpiration(time.Now().Add(c.duration))
    69  	}
    70  
    71  	pr, err := getUploader().UploadAndSignBlob(unsigned)
    72  	handleResult("share", pr, err)
    73  	return nil
    74  }