-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.go
54 lines (48 loc) · 1.28 KB
/
util.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
53
54
package cmd
import (
"bytes"
"errors"
"io"
"os"
"strings"
"github.com/DrakeW/corgi/config"
"github.com/DrakeW/corgi/snippet"
"github.com/DrakeW/corgi/util"
"github.com/fatih/color"
)
var MissingSnippetTitleError = errors.New("snippet title is not selected")
func loadConfigAndSnippetsMeta() (*config.Config, *snippet.SnippetsMeta, error) {
// load config
conf, err := config.Load()
if err != nil {
return nil, nil, err
}
// Load snippets
snippets, err := conf.LoadSnippetsMeta()
if err != nil {
return nil, nil, err
}
return conf, snippets, nil
}
func filter(filterCmd string, candidates []string) (string, error) {
var buf bytes.Buffer
inputs := strings.Join(candidates, "\n")
ws := io.MultiWriter(os.Stdout, &buf)
if err := util.Execute(filterCmd, strings.NewReader(inputs), ws); err != nil {
return "", err
}
result := strings.Trim(strings.TrimSpace(buf.String()), "\n")
return result, nil
}
func filterSnippetTitle(filterCmd string, titles []string) (string, error) {
if filterCmd != "" {
title, err := filter(filterCmd, titles)
if err != nil || title == "" {
return "", MissingSnippetTitleError
}
return title, nil
} else {
color.Red("Install a fuzzy finder (\"fzf\" or \"peco\") to enable interactive selection")
return "", MissingSnippetTitleError
}
}