github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/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  	"os"
    24  	"time"
    25  
    26  	"camlistore.org/pkg/blob"
    27  	"camlistore.org/pkg/cmdmain"
    28  	"camlistore.org/pkg/search"
    29  	"camlistore.org/pkg/types"
    30  )
    31  
    32  type desCmd struct {
    33  	server string
    34  	depth  int
    35  }
    36  
    37  func init() {
    38  	cmdmain.RegisterCommand("describe", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    39  		cmd := new(desCmd)
    40  		flags.StringVar(&cmd.server, "server", "", "Server to query. "+serverFlagHelp)
    41  		flags.IntVar(&cmd.depth, "depth", 1, "Depth to follow in describe request")
    42  		return cmd
    43  	})
    44  }
    45  
    46  func (c *desCmd) Describe() string {
    47  	return "Ask the search system to describe one or more blobs."
    48  }
    49  
    50  func (c *desCmd) Usage() {
    51  	fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] describe [--depth=n] blobref [blobref, blobref...]\n")
    52  }
    53  
    54  func (c *desCmd) Examples() []string {
    55  	return []string{}
    56  }
    57  
    58  func (c *desCmd) RunCommand(args []string) error {
    59  	if len(args) == 0 {
    60  		return cmdmain.UsageError("requires blobref")
    61  	}
    62  	var blobs []blob.Ref
    63  	for _, arg := range args {
    64  		br, ok := blob.Parse(arg)
    65  		if !ok {
    66  			return cmdmain.UsageError(fmt.Sprintf("invalid blobref %q", arg))
    67  		}
    68  		blobs = append(blobs, br)
    69  	}
    70  	var at time.Time // TODO: implement. from "2 days ago" "-2d", "-2h", "2013-02-05", etc
    71  
    72  	cl := newClient(c.server)
    73  	res, err := cl.Describe(&search.DescribeRequest{
    74  		BlobRefs: blobs,
    75  		Depth:    c.depth,
    76  		At:       types.Time3339(at),
    77  	})
    78  	if err != nil {
    79  		return err
    80  	}
    81  	resj, err := json.MarshalIndent(res, "", "  ")
    82  	if err != nil {
    83  		return err
    84  	}
    85  	resj = append(resj, '\n')
    86  	_, err = os.Stdout.Write(resj)
    87  	return err
    88  }