github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_simplefs_search.go (about)

     1  // Copyright 2020 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package client
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/keybase/cli"
    11  	"github.com/keybase/client/go/libcmdline"
    12  	"github.com/keybase/client/go/libkb"
    13  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    14  )
    15  
    16  const (
    17  	defaultNumFSSearchResults = 10
    18  )
    19  
    20  // CmdSimpleFSSearch is the 'fs search' command.
    21  type CmdSimpleFSSearch struct {
    22  	libkb.Contextified
    23  
    24  	query        string
    25  	numResults   int
    26  	startingFrom int
    27  }
    28  
    29  // NewCmdSimpleFSSearch creates a new cli.Command.
    30  func NewCmdSimpleFSSearch(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    31  	return cli.Command{
    32  		Name:         "search",
    33  		ArgumentHelp: "<query>",
    34  		Usage:        "[disabled] search locally-synced folders",
    35  		Action: func(c *cli.Context) {
    36  			cl.ChooseCommand(&CmdSimpleFSSearch{
    37  				Contextified: libkb.NewContextified(g)}, "search", c)
    38  			cl.SetNoStandalone()
    39  		},
    40  		Flags: []cli.Flag{
    41  			cli.IntFlag{
    42  				Name:  "n, num-results",
    43  				Usage: "how many results to return",
    44  				Value: defaultNumFSSearchResults,
    45  			},
    46  			cli.IntFlag{
    47  				Name:  "s, start-from",
    48  				Usage: "what number result to start from (for paging)",
    49  				Value: 0,
    50  			},
    51  		},
    52  	}
    53  }
    54  
    55  // Run runs the command in client/server mode.
    56  func (c *CmdSimpleFSSearch) Run() error {
    57  	cli, err := GetSimpleFSClient(c.G())
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	arg := keybase1.SimpleFSSearchArg{
    63  		Query:        c.query,
    64  		NumResults:   c.numResults,
    65  		StartingFrom: c.startingFrom,
    66  	}
    67  	res, err := cli.SimpleFSSearch(context.TODO(), arg)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	ui := c.G().UI.GetTerminalUI()
    73  	if len(res.Hits) == 0 {
    74  		ui.Printf("No results\n")
    75  		return nil
    76  	}
    77  
    78  	for _, hit := range res.Hits {
    79  		ui.Printf("%s\n", hit.Path)
    80  	}
    81  	if res.NextResult != -1 {
    82  		ui.Printf(
    83  			"(For more results, use `--start-from %d` next time.)\n",
    84  			res.NextResult)
    85  	}
    86  	return nil
    87  }
    88  
    89  // ParseArgv gets the optional flags and the query.
    90  func (c *CmdSimpleFSSearch) ParseArgv(ctx *cli.Context) error {
    91  	if len(ctx.Args()) != 1 {
    92  		return fmt.Errorf("wrong number of arguments")
    93  	}
    94  
    95  	c.query = ctx.Args().Get(0)
    96  	c.numResults = ctx.Int("num-results")
    97  	if c.numResults == 0 {
    98  		c.numResults = defaultNumFSSearchResults
    99  	}
   100  	c.startingFrom = ctx.Int("start-from")
   101  
   102  	return nil
   103  }
   104  
   105  // GetUsage says what this command needs to operate.
   106  func (c *CmdSimpleFSSearch) GetUsage() libkb.Usage {
   107  	return libkb.Usage{
   108  		Config:    true,
   109  		KbKeyring: true,
   110  		API:       true,
   111  	}
   112  }