This repository has been archived by the owner on Mar 2, 2020. It is now read-only.
/
filter.go
179 lines (166 loc) · 4.38 KB
/
filter.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package run
import (
"fmt"
"os"
"strings"
"github.com/kardianos/govendor/context"
"github.com/kardianos/govendor/internal/pathos"
"github.com/kardianos/govendor/pkgspec"
)
var (
outside = []context.Status{
{Location: context.LocationExternal},
{Presence: context.PresenceMissing},
}
normal = []context.Status{
{Location: context.LocationExternal},
{Location: context.LocationVendor},
{Location: context.LocationLocal},
{Location: context.LocationNotFound},
}
all = []context.Status{
{Location: context.LocationStandard},
{Location: context.LocationExternal},
{Location: context.LocationVendor},
{Location: context.LocationLocal},
{Location: context.LocationNotFound},
}
)
func statusGroupFromList(list []context.Status, and, not bool) context.StatusGroup {
sg := context.StatusGroup{
Not: not,
And: and,
}
for _, s := range list {
sg.Status = append(sg.Status, s)
}
return sg
}
const notOp = "^"
func parseStatusGroup(statusString string) (sg context.StatusGroup, err error) {
ss := strings.Split(statusString, ",")
sg.And = true
for _, s := range ss {
st := context.Status{}
if strings.HasPrefix(s, notOp) {
st.Not = true
s = strings.TrimPrefix(s, notOp)
}
var list []context.Status
switch {
case strings.HasPrefix("external", s):
st.Location = context.LocationExternal
case strings.HasPrefix("vendor", s):
st.Location = context.LocationVendor
case strings.HasPrefix("unused", s):
st.Presence = context.PresenceUnused
case strings.HasPrefix("missing", s):
st.Presence = context.PresenceMissing
case strings.HasPrefix("xcluded", s):
st.Presence = context.PresenceExcluded
case len(s) >= 3 && strings.HasPrefix("excluded", s): // len >= 3 to distinguish from "external"
st.Presence = context.PresenceExcluded
case strings.HasPrefix("local", s):
st.Location = context.LocationLocal
case strings.HasPrefix("program", s):
st.Type = context.TypeProgram
case strings.HasPrefix("std", s):
st.Location = context.LocationStandard
case strings.HasPrefix("standard", s):
st.Location = context.LocationStandard
case strings.HasPrefix("all", s):
list = all
case strings.HasPrefix("normal", s):
list = normal
case strings.HasPrefix("outside", s):
list = outside
default:
err = fmt.Errorf("unknown status %q", s)
return
}
if len(list) == 0 {
sg.Status = append(sg.Status, st)
} else {
sg.Group = append(sg.Group, statusGroupFromList(list, false, st.Not))
}
}
return
}
type filter struct {
Status context.StatusGroup
Import []*pkgspec.Pkg
}
func (f filter) String() string {
return fmt.Sprintf("status %q, import: %q", f.Status, f.Import)
}
func (f filter) HasStatus(item context.StatusItem) bool {
return item.Status.MatchGroup(f.Status)
}
func (f filter) FindImport(item context.StatusItem) *pkgspec.Pkg {
for _, imp := range f.Import {
if imp.Path == item.Local || imp.Path == item.Pkg.Path {
return imp
}
if imp.MatchTree {
if strings.HasPrefix(item.Local, imp.Path) || strings.HasPrefix(item.Pkg.Path, imp.Path) {
return imp
}
}
}
return nil
}
func currentGoPath(ctx *context.Context) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
wdpath := pathos.FileTrimPrefix(wd, ctx.RootGopath)
wdpath = pathos.SlashToFilepath(wdpath)
wdpath = strings.Trim(wdpath, "/")
return wdpath, nil
}
func parseFilter(currentGoPath string, args []string) (filter, error) {
f := filter{
Import: make([]*pkgspec.Pkg, 0, len(args)),
}
for _, a := range args {
if len(a) == 0 {
continue
}
// Check if item is a status.
if a[0] == '+' {
sg, err := parseStatusGroup(a[1:])
if err != nil {
return f, err
}
f.Status.Group = append(f.Status.Group, sg)
} else {
pkg, err := pkgspec.Parse(currentGoPath, a)
if err != nil {
return f, err
}
f.Import = append(f.Import, pkg)
}
}
return f, nil
}
func insertListToAllNot(sg *context.StatusGroup, list []context.Status) {
if len(sg.Group) == 0 {
allStatusNot := true
for _, s := range sg.Status {
if s.Not == false {
allStatusNot = false
break
}
}
if allStatusNot {
sg.Group = append(sg.Group, statusGroupFromList(list, false, false))
}
}
for i := range sg.Group {
insertListToAllNot(&sg.Group[i], list)
}
}