-
Notifications
You must be signed in to change notification settings - Fork 141
/
platinum_searcher.go
138 lines (115 loc) · 2.6 KB
/
platinum_searcher.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package the_platinum_searcher
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"github.com/jessevdk/go-flags"
"github.com/monochromegane/conflag"
"github.com/monochromegane/go-home"
"github.com/monochromegane/terminal"
)
const version = "2.2.0"
const (
ExitCodeOK = iota
ExitCodeError
)
var opts Option
type PlatinumSearcher struct {
Out, Err io.Writer
}
func (p PlatinumSearcher) Run(args []string) int {
parser := newOptionParser(&opts)
conflag.LongHyphen = true
conflag.BoolValue = false
for _, c := range [...]string{
filepath.Join(xdgConfigHomeDir(), "pt", "config.toml"),
filepath.Join(home.Dir(), ".ptconfig.toml"),
".ptconfig.toml",
} {
if args, err := conflag.ArgsFrom(c); err == nil {
parser.ParseArgs(args)
}
}
args, err := parser.ParseArgs(args)
if err != nil {
if ferr, ok := err.(*flags.Error); ok && ferr.Type == flags.ErrHelp {
return ExitCodeOK
}
return ExitCodeError
}
if opts.Version {
fmt.Printf("pt version %s\n", version)
return ExitCodeOK
}
if len(args) == 0 && !opts.SearchOption.EnableFilesWithRegexp {
parser.WriteHelp(p.Err)
return ExitCodeError
}
if !terminal.IsTerminal(os.Stdout) {
if !opts.OutputOption.ForceColor {
opts.OutputOption.EnableColor = false
}
if !opts.OutputOption.ForceGroup {
opts.OutputOption.EnableGroup = false
}
}
if p.givenStdin() && p.noRootPathIn(args) {
opts.SearchOption.SearchStream = true
}
if opts.SearchOption.EnableFilesWithRegexp {
args = append([]string{""}, args...)
}
if opts.OutputOption.Count {
opts.OutputOption.Before = 0
opts.OutputOption.After = 0
opts.OutputOption.Context = 0
}
search := search{
roots: p.rootsFrom(args),
out: p.Out,
}
if err = search.start(p.patternFrom(args)); err != nil {
fmt.Fprintf(p.Err, "%s\n", err)
return ExitCodeError
}
return ExitCodeOK
}
func (p PlatinumSearcher) patternFrom(args []string) string {
return args[0]
}
func (p PlatinumSearcher) rootsFrom(args []string) []string {
if len(args) > 1 {
return args[1:]
} else {
return []string{"."}
}
}
func (p PlatinumSearcher) givenStdin() bool {
fi, err := os.Stdin.Stat()
if runtime.GOOS == "windows" {
if err == nil {
return true
}
} else {
if err != nil {
return false
}
mode := fi.Mode()
if (mode&os.ModeNamedPipe != 0) || mode.IsRegular() {
return true
}
}
return false
}
func (p PlatinumSearcher) noRootPathIn(args []string) bool {
return len(args) == 1
}
func xdgConfigHomeDir() string {
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome == "" {
xdgConfigHome = filepath.Join(home.Dir(), ".config")
}
return xdgConfigHome
}