github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/cmd/camput/permanode.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 "errors" 21 "flag" 22 "fmt" 23 "strings" 24 "time" 25 26 "camlistore.org/pkg/client" 27 "camlistore.org/pkg/cmdmain" 28 "camlistore.org/pkg/schema" 29 ) 30 31 type permanodeCmd struct { 32 title string 33 tag string 34 key string // else random 35 sigTime string 36 } 37 38 func init() { 39 cmdmain.RegisterCommand("permanode", func(flags *flag.FlagSet) cmdmain.CommandRunner { 40 cmd := new(permanodeCmd) 41 flags.StringVar(&cmd.title, "title", "", "Optional 'title' attribute to set on new permanode") 42 flags.StringVar(&cmd.tag, "tag", "", "Optional tag(s) to set on new permanode; comma separated.") 43 flags.StringVar(&cmd.key, "key", "", "Optional key to create deterministic ('planned') permanodes. Must also use --sigtime.") 44 flags.StringVar(&cmd.sigTime, "sigtime", "", "Optional time to put in the OpenPGP signature packet instead of the current time. Required when producing a deterministic permanode (with --key). In format YYYY-MM-DD HH:MM:SS") 45 return cmd 46 }) 47 } 48 49 func (c *permanodeCmd) Describe() string { 50 return "Create and upload a permanode." 51 } 52 53 func (c *permanodeCmd) Usage() { 54 cmdmain.Errorf("Usage: camput [globalopts] permanode [permanodeopts]\n") 55 } 56 57 func (c *permanodeCmd) Examples() []string { 58 return []string{ 59 " (create a new permanode)", 60 `-name="Some Name" -tag=foo,bar (with attributes added)`, 61 } 62 } 63 64 func (c *permanodeCmd) RunCommand(args []string) error { 65 if len(args) > 0 { 66 return errors.New("Permanode command doesn't take any additional arguments") 67 } 68 69 var ( 70 permaNode *client.PutResult 71 err error 72 up = getUploader() 73 ) 74 if (c.key != "") != (c.sigTime != "") { 75 return errors.New("Both --key and --sigtime must be used to produce deterministic permanodes.") 76 } 77 if c.key == "" { 78 // Normal case, with a random permanode. 79 permaNode, err = up.UploadNewPermanode() 80 } else { 81 const format = "2006-01-02 15:04:05" 82 sigTime, err := time.Parse(format, c.sigTime) 83 if err != nil { 84 return fmt.Errorf("Error parsing time %q; expecting time of form %q", c.sigTime, format) 85 } 86 permaNode, err = up.UploadPlannedPermanode(c.key, sigTime) 87 } 88 if handleResult("permanode", permaNode, err) != nil { 89 return err 90 } 91 92 if c.title != "" { 93 put, err := up.UploadAndSignBlob(schema.NewSetAttributeClaim(permaNode.BlobRef, "title", c.title)) 94 handleResult("claim-permanode-title", put, err) 95 } 96 if c.tag != "" { 97 tags := strings.Split(c.tag, ",") 98 m := schema.NewSetAttributeClaim(permaNode.BlobRef, "tag", tags[0]) 99 for _, tag := range tags { 100 m = schema.NewAddAttributeClaim(permaNode.BlobRef, "tag", tag) 101 put, err := up.UploadAndSignBlob(m) 102 handleResult("claim-permanode-tag", put, err) 103 } 104 } 105 return nil 106 }