From 839d8bb858c47f0d0c74a1009bbca48cf09e5a5f Mon Sep 17 00:00:00 2001 From: mpl Date: Mon, 26 Sep 2011 12:32:26 +0200 Subject: [PATCH] attr command for new camput cli Change-Id: I4f3b1231d8817f3874a6a3b499dc364ebd971957 --- clients/go/camput/attr.go | 80 +++++++++++++++++++++++++++++++++++++ clients/go/camput/camput.go | 15 ------- 2 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 clients/go/camput/attr.go diff --git a/clients/go/camput/attr.go b/clients/go/camput/attr.go new file mode 100644 index 000000000..158f59da0 --- /dev/null +++ b/clients/go/camput/attr.go @@ -0,0 +1,80 @@ +/* +Copyright 2011 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "flag" + "fmt" + "os" + + "camli/blobref" + "camli/schema" +) + +type attrCmd struct { + flags *flag.FlagSet + + add bool + del bool +} + +func init() { + flags := flag.NewFlagSet("attr options", flag.ContinueOnError) + flags.Usage = func() {} + cmd := &attrCmd{flags: flags} + + flags.BoolVar(&cmd.add, "add", false, `Adds attribute (e.g. "tag")`) + flags.BoolVar(&cmd.del, "del", false, "Deletes named attribute [value]") + + RegisterCommand("attr", cmd) +} + +func (c *attrCmd) Usage() { + fmt.Fprintf(os.Stderr, "Usage: camput [globalopts] attr [attroption] \n Attr options: \n") + c.flags.PrintDefaults() +} + +func (c *attrCmd) RunCommand(up *Uploader, args []string) os.Error { + if err := c.flags.Parse(args); err != nil { + return ErrUsage + } + args = c.flags.Args() + if len(args) != 3 { + return os.NewError("Attr takes 3 args: ") + } + var err os.Error + + pn := blobref.Parse(args[0]) + if pn == nil { + return fmt.Errorf("Error parsing blobref %q", args[0]) + } + m := schema.NewSetAttributeClaim(pn, args[1], args[2]) + if c.add { + if c.del { + return os.NewError("Add and del options are exclusive") + } + m = schema.NewAddAttributeClaim(pn, args[1], args[2]) + } else { + // TODO: del + if c.del { + return os.NewError("del not yet implemented") + } + } + put, err := up.UploadAndSignMap(m) + handleResult(m["claimType"].(string), put, err) + return nil +} diff --git a/clients/go/camput/camput.go b/clients/go/camput/camput.go index 2d9952c0e..4bcddc323 100644 --- a/clients/go/camput/camput.go +++ b/clients/go/camput/camput.go @@ -481,20 +481,5 @@ func main() { log.Printf("Error removing blobs %s: %s", strings.Join(flag.Args(), ","), err) wereErrors = true } - case *flagAddAttr || *flagSetAttr: - if flag.NArg() != 3 { - log.Fatalf("--set-attr and --add-attr take 3 args: ") - } - pn := blobref.Parse(flag.Arg(0)) - if pn == nil { - log.Fatalf("Error parsing blobref %q", flag.Arg(0)) - } - m := schema.NewSetAttributeClaim(pn, flag.Arg(1), flag.Arg(2)) - if *flagAddAttr { - m = schema.NewAddAttributeClaim(pn, flag.Arg(1), flag.Arg(2)) - } - put, err := up.UploadAndSignMap(m) - handleResult(m["claimType"].(string), put, err) -} */