github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camput/delete.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  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 deleteCmd struct {
    29  	up *Uploader
    30  }
    31  
    32  func init() {
    33  	cmdmain.RegisterCommand("delete", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    34  		cmd := new(deleteCmd)
    35  		return cmd
    36  	})
    37  }
    38  
    39  func (c *deleteCmd) Describe() string {
    40  	return "Create and upload a delete claim."
    41  }
    42  
    43  func (c *deleteCmd) Usage() {
    44  	cmdmain.Errorf("Usage: camput [globalopts] delete <blobref1> [blobref2]...")
    45  }
    46  
    47  func (c *deleteCmd) RunCommand(args []string) error {
    48  	if len(args) < 1 {
    49  		return cmdmain.UsageError("Need at least one blob to delete.")
    50  	}
    51  	if err := delete(args); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  func delete(args []string) error {
    58  	for _, arg := range args {
    59  		br, ok := blob.Parse(arg)
    60  		if !ok {
    61  			return fmt.Errorf("Error parsing blobref %q", arg)
    62  		}
    63  		bb := schema.NewDeleteClaim(br)
    64  		put, err := getUploader().UploadAndSignBlob(bb)
    65  		if err := handleResult(bb.Type(), put, err); err != nil {
    66  			return err
    67  		}
    68  	}
    69  	return nil
    70  }