github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/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 "log" 24 "net/http" 25 "os" 26 27 "camlistore.org/pkg/blob" 28 "camlistore.org/pkg/client" 29 "camlistore.org/pkg/cmdmain" 30 "camlistore.org/pkg/search" 31 ) 32 33 type claimsCmd struct { 34 src string 35 36 logger *log.Logger 37 } 38 39 func init() { 40 cmdmain.RegisterCommand("claims", func(flags *flag.FlagSet) cmdmain.CommandRunner { 41 cmd := new(claimsCmd) 42 flags.StringVar(&cmd.src, "src", "", "Source blobserver is either a URL prefix (with optional path), a host[:port], a path (starting with /, ./, or ../), or blank to use the Camlistore client config's default host.") 43 return cmd 44 }) 45 } 46 47 func (c *claimsCmd) Describe() string { 48 return "Ask the search system to describe one or more blobs." 49 } 50 51 func (c *claimsCmd) Usage() { 52 fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] describe [--depth=n] blobref [blobref, blobref...]\n") 53 } 54 55 func (c *claimsCmd) Examples() []string { 56 return []string{} 57 } 58 59 func (c *claimsCmd) RunCommand(args []string) error { 60 if len(args) != 1 { 61 return cmdmain.UsageError("requires 1 blobref") 62 } 63 br, ok := blob.Parse(args[0]) 64 if !ok { 65 return cmdmain.UsageError("invalid blobref") 66 } 67 cl := c.client() 68 res, err := cl.GetClaims(&search.ClaimsRequest{ 69 Permanode: br, 70 }) 71 if err != nil { 72 return err 73 } 74 resj, err := json.MarshalIndent(res, "", " ") 75 if err != nil { 76 return err 77 } 78 resj = append(resj, '\n') 79 _, err = os.Stdout.Write(resj) 80 return err 81 } 82 83 func (c *claimsCmd) client() *client.Client { 84 var cl *client.Client 85 if c.src == "" { 86 cl = client.NewOrFail() 87 } else { 88 cl = client.New(c.src) 89 } 90 cl.SetLogger(c.logger) 91 cl.SetHTTPClient(&http.Client{ 92 Transport: cl.TransportForConfig(nil), 93 }) 94 cl.SetupAuth() 95 return cl 96 }