/
utils.go
41 lines (35 loc) · 892 Bytes
/
utils.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
package main
import (
"go/ast"
"os"
"path/filepath"
"strings"
"github.com/quasilyte/gogrep"
"github.com/quasilyte/gogrep/filters"
)
func capturedByName(m gogrep.MatchData, name string) (ast.Node, bool) {
if filters.IsRootVarname(name) {
return m.Node, true
}
n, ok := m.CapturedByName(name)
return n, ok
}
func isGoFilename(filename string) bool {
return strings.HasSuffix(filename, ".go") ||
strings.HasSuffix(filename, ".go2")
}
func envVarOrDefault(envKey, defaultValue string) string {
if v := os.Getenv(envKey); v != "" {
return v
}
return defaultValue
}
// filepathAbs is a faster and error-free version of filepath.Abs.
// If workdir is already available, there is no need to do a os.Getwd for
// every filepath.Abs call.
func filepathAbs(wd, filename string) string {
if filepath.IsAbs(filename) {
return filename
}
return filepath.Join(wd, filename)
}