forked from knqyf263/pet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
52 lines (44 loc) · 1.26 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cmd
import (
"fmt"
"strings"
"al.essio.dev/pkg/shellescape"
"github.com/kulack/pet/config"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
)
var delimiter string
// searchCmd represents the search command
var searchCmd = &cobra.Command{
Use: "search",
Short: "Search snippets",
Long: `Search snippets interactively (default filtering tool: peco)`,
RunE: search,
}
func search(cmd *cobra.Command, args []string) (err error) {
flag := config.Flag
var options []string
if flag.Query != "" {
options = append(options, fmt.Sprintf("--query %s", shellescape.Quote(flag.Query)))
}
commands, err := filter(options, flag.FilterTag)
if err != nil {
return err
}
fmt.Print(strings.Join(commands, flag.Delimiter))
if terminal.IsTerminal(1) {
fmt.Print("\n")
}
return nil
}
func init() {
RootCmd.AddCommand(searchCmd)
searchCmd.Flags().BoolVarP(&config.Flag.Color, "color", "", false,
`Enable colorized output (only fzf)`)
searchCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "",
`Initial value for query`)
searchCmd.Flags().StringVarP(&config.Flag.FilterTag, "tag", "t", "",
`Filter tag`)
searchCmd.Flags().StringVarP(&config.Flag.Delimiter, "delimiter", "d", "; ",
`Use delim as the command delimiter character`)
}