github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camtool/describe.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 "time" 27 28 "camlistore.org/pkg/blob" 29 "camlistore.org/pkg/client" 30 "camlistore.org/pkg/cmdmain" 31 "camlistore.org/pkg/search" 32 "camlistore.org/pkg/types" 33 ) 34 35 type desCmd struct { 36 src string 37 depth int 38 39 logger *log.Logger 40 } 41 42 func init() { 43 cmdmain.RegisterCommand("describe", func(flags *flag.FlagSet) cmdmain.CommandRunner { 44 cmd := new(desCmd) 45 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.") 46 flags.IntVar(&cmd.depth, "depth", 1, "Depth to follow in describe request") 47 return cmd 48 }) 49 } 50 51 func (c *desCmd) Describe() string { 52 return "Ask the search system to describe one or more blobs." 53 } 54 55 func (c *desCmd) Usage() { 56 fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] describe [--depth=n] blobref [blobref, blobref...]\n") 57 } 58 59 func (c *desCmd) Examples() []string { 60 return []string{} 61 } 62 63 func (c *desCmd) RunCommand(args []string) error { 64 if len(args) == 0 { 65 return cmdmain.UsageError("requires blobref") 66 } 67 var blobs []blob.Ref 68 for _, arg := range args { 69 br, ok := blob.Parse(arg) 70 if !ok { 71 return cmdmain.UsageError(fmt.Sprintf("invalid blobref %q", arg)) 72 } 73 blobs = append(blobs, br) 74 } 75 var at time.Time // TODO: implement. from "2 days ago" "-2d", "-2h", "2013-02-05", etc 76 77 cl := c.client() 78 res, err := cl.Describe(&search.DescribeRequest{ 79 BlobRefs: blobs, 80 Depth: c.depth, 81 At: types.Time3339(at), 82 }) 83 if err != nil { 84 return err 85 } 86 resj, err := json.MarshalIndent(res, "", " ") 87 if err != nil { 88 return err 89 } 90 resj = append(resj, '\n') 91 _, err = os.Stdout.Write(resj) 92 return err 93 } 94 95 func (c *desCmd) client() *client.Client { 96 var cl *client.Client 97 if c.src == "" { 98 cl = client.NewOrFail() 99 } else { 100 cl = client.New(c.src) 101 } 102 cl.SetLogger(c.logger) 103 cl.SetHTTPClient(&http.Client{ 104 Transport: cl.TransportForConfig(nil), 105 }) 106 cl.SetupAuth() 107 return cl 108 }