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

     1  /*
     2  Copyright 2014 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  	"encoding/json"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  
    25  	"camlistore.org/pkg/blob"
    26  	"camlistore.org/pkg/cmdmain"
    27  	"camlistore.org/pkg/search"
    28  )
    29  
    30  type claimsCmd struct {
    31  	server string
    32  	attr   string
    33  }
    34  
    35  func init() {
    36  	cmdmain.RegisterCommand("claims", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    37  		cmd := new(claimsCmd)
    38  		flags.StringVar(&cmd.server, "server", "", "Server to fetch claims from. "+serverFlagHelp)
    39  		flags.StringVar(&cmd.attr, "attr", "", "Filter claims about a specific attribute. If empty, all claims are returned.")
    40  		return cmd
    41  	})
    42  }
    43  
    44  func (c *claimsCmd) Describe() string {
    45  	return "Ask the search system to list the claims that modify a permanode."
    46  }
    47  
    48  func (c *claimsCmd) Usage() {
    49  	fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] claims [--depth=n] [--attr=s] permanodeBlobRef\n")
    50  }
    51  
    52  func (c *claimsCmd) Examples() []string {
    53  	return []string{}
    54  }
    55  
    56  func (c *claimsCmd) RunCommand(args []string) error {
    57  	if len(args) != 1 {
    58  		return cmdmain.UsageError("requires 1 blobref")
    59  	}
    60  	br, ok := blob.Parse(args[0])
    61  	if !ok {
    62  		return cmdmain.UsageError("invalid blobref")
    63  	}
    64  	cl := newClient(c.server)
    65  	res, err := cl.GetClaims(&search.ClaimsRequest{
    66  		Permanode:  br,
    67  		AttrFilter: c.attr,
    68  	})
    69  	if err != nil {
    70  		return err
    71  	}
    72  	resj, err := json.MarshalIndent(res, "", "  ")
    73  	if err != nil {
    74  		return err
    75  	}
    76  	resj = append(resj, '\n')
    77  	_, err = os.Stdout.Write(resj)
    78  	return err
    79  }