github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/cmd/camput/attr.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 23 "camlistore.org/pkg/blob" 24 "camlistore.org/pkg/cmdmain" 25 "camlistore.org/pkg/schema" 26 ) 27 28 type attrCmd struct { 29 add bool 30 del bool 31 up *Uploader 32 } 33 34 func init() { 35 cmdmain.RegisterCommand("attr", func(flags *flag.FlagSet) cmdmain.CommandRunner { 36 cmd := new(attrCmd) 37 flags.BoolVar(&cmd.add, "add", false, `Adds attribute (e.g. "tag")`) 38 flags.BoolVar(&cmd.del, "del", false, "Deletes named attribute [value]") 39 return cmd 40 }) 41 } 42 43 func (c *attrCmd) Describe() string { 44 return "Add, set, or delete a permanode's attribute." 45 } 46 47 func (c *attrCmd) Usage() { 48 cmdmain.Errorf("Usage: camput [globalopts] attr [attroption] <permanode> <name> <value>") 49 } 50 51 func (c *attrCmd) Examples() []string { 52 return []string{ 53 "<permanode> <name> <value> Set attribute", 54 "--add <permanode> <name> <value> Adds attribute (e.g. \"tag\")", 55 "--del <permanode> <name> [<value>] Deletes named attribute", 56 } 57 } 58 59 func (c *attrCmd) RunCommand(args []string) error { 60 if err := c.checkArgs(args); err != nil { 61 return err 62 } 63 permanode, attr := args[0], args[1] 64 value := "" 65 if len(args) > 2 { 66 value = args[2] 67 } 68 69 pn, ok := blob.Parse(permanode) 70 if !ok { 71 return fmt.Errorf("Error parsing blobref %q", permanode) 72 } 73 claimFunc := func() func(blob.Ref, string, string) *schema.Builder { 74 switch { 75 case c.add: 76 return schema.NewAddAttributeClaim 77 case c.del: 78 return schema.NewDelAttributeClaim 79 default: 80 return schema.NewSetAttributeClaim 81 } 82 }() 83 bb := claimFunc(pn, attr, value) 84 put, err := getUploader().UploadAndSignBlob(bb) 85 handleResult(bb.Type(), put, err) 86 return nil 87 } 88 89 func (c *attrCmd) checkArgs(args []string) error { 90 if c.del { 91 if c.add { 92 return cmdmain.UsageError("Add and del options are exclusive") 93 } 94 if len(args) < 2 { 95 return cmdmain.UsageError("Attr -del takes at least 2 args: <permanode> <attr> [<value>]") 96 } 97 return nil 98 } 99 if len(args) != 3 { 100 return cmdmain.UsageError("Attr takes 3 args: <permanode> <attr> <value>") 101 } 102 return nil 103 }