cmd/search.go
changeset 0 5abace724584
child 13 f862af8faf17
equal deleted inserted replaced
-1:000000000000 0:5abace724584
       
     1 // Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
       
     2 //
       
     3 // Licensed under the MIT license.
       
     4 // Please see the LICENSE file is this directory.
       
     5 
       
     6 package cmd
       
     7 
       
     8 import (
       
     9 	"errors"
       
    10 	"strings"
       
    11 
       
    12 	"github.com/spf13/cobra"
       
    13 )
       
    14 
       
    15 var searchOpts struct {
       
    16 	resolve bool
       
    17 }
       
    18 
       
    19 // searchCmd represents the search command
       
    20 var searchCmd = &cobra.Command{
       
    21 	Use:   "search [--resolve] STRING",
       
    22 	Short: "Search for contents (accounts or statuses)",
       
    23 	//Long: `TBW...`,
       
    24 	RunE: searchRunE,
       
    25 }
       
    26 
       
    27 func init() {
       
    28 	RootCmd.AddCommand(searchCmd)
       
    29 
       
    30 	searchCmd.Flags().BoolVar(&searchOpts.resolve, "resolve", false, "Resolve non-local accounts")
       
    31 }
       
    32 
       
    33 func searchRunE(cmd *cobra.Command, args []string) error {
       
    34 	opt := searchOpts
       
    35 
       
    36 	if len(args) == 0 {
       
    37 		return errors.New("no search string provided")
       
    38 	}
       
    39 
       
    40 	if err := madonInit(true); err != nil {
       
    41 		return err
       
    42 	}
       
    43 
       
    44 	results, err := gClient.Search(strings.Join(args, " "), opt.resolve)
       
    45 	if err != nil {
       
    46 		errPrint("Error: %s", err.Error())
       
    47 		return nil
       
    48 	}
       
    49 
       
    50 	p, err := getPrinter()
       
    51 	if err != nil {
       
    52 		return err
       
    53 	}
       
    54 	return p.PrintObj(results, nil, "")
       
    55 }