forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
190 lines (172 loc) · 4.84 KB
/
run.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
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 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 main
// imports for this file should not contain "os".
import (
"errors"
"flag"
"fmt"
"io"
"strings"
"github.com/kardianos/vendor/rewrite"
)
var help = `vendor: copy go packages locally and re-write imports.
vendor init
vendor list [status]
vendor {add, update, remove} [-status] <import-path or status>
init
create a vendor file if it does not exist.
add
copy one or more packages into the internal folder and re-write paths.
update
update one or more packages from GOPATH into the internal folder.
remove
remove one or more packages from the internal folder and re-write packages to vendor paths.
Expanding "..."
A package import path may be expanded to other paths that
show up in "vendor list" be ending the "import-path" with "...".
NOTE: this uses the import tree from "vendor list" and NOT the file system.
Status list:
external - package does not share root path
internal - in vendor file; copied locally
unused - the package has been copied locally, but isn't used
local - shares the root path and is not a vendor package
missing - referenced but not found in GOROOT or GOPATH
std - standard library package
Status can be referenced by their initial letters.
"st" == "std"
"e" == "external"
Example:
vendor add github.com/kardianos/osext
vendor update github.com/kardianos/...
vendor add -status external
vendor update -status ext
vendor remove -status internal
`
func parseStatus(s string) (status []rewrite.ListStatus, err error) {
switch {
case strings.HasPrefix("external", s):
status = []rewrite.ListStatus{rewrite.StatusExternal}
case strings.HasPrefix("internal", s):
status = []rewrite.ListStatus{rewrite.StatusInternal}
case strings.HasPrefix("unused", s):
status = []rewrite.ListStatus{rewrite.StatusUnused}
case strings.HasPrefix("missing", s):
status = []rewrite.ListStatus{rewrite.StatusMissing}
case strings.HasPrefix("local", s):
status = []rewrite.ListStatus{rewrite.StatusLocal}
case strings.HasPrefix("std", s):
status = []rewrite.ListStatus{rewrite.StatusStd}
default:
err = fmt.Errorf("unknown status %q", s)
}
return
}
// run is isoloated from main and os.Args to help with testing.
// Shouldn't directly print to console, just write through w.
func run(w io.Writer, appArgs []string) (help bool, err error) {
if len(appArgs) == 1 {
return true, nil
}
cmd := appArgs[1]
switch cmd {
case "init":
err = rewrite.CmdInit()
case "list":
status := []rewrite.ListStatus{rewrite.StatusExternal, rewrite.StatusInternal, rewrite.StatusUnused, rewrite.StatusMissing, rewrite.StatusLocal}
// Parse status.
if len(appArgs) >= 3 {
status, err = parseStatus(appArgs[2])
if err != nil {
return true, err
}
}
// Print all listed status.
var list []rewrite.ListItem
list, err = rewrite.CmdList()
for _, item := range list {
print := false
for _, s := range status {
if item.Status == s {
print = true
break
}
}
if print {
fmt.Fprintln(w, item)
}
}
case "add", "update", "remove":
listFlags := flag.NewFlagSet("list", flag.ContinueOnError)
useStatus := listFlags.Bool("status", false, "")
err = listFlags.Parse(appArgs[2:])
if err != nil {
return true, err
}
args := listFlags.Args()
if len(args) == 0 {
return true, errors.New("missing status")
}
if *useStatus {
statusList, err := parseStatus(args[0])
if err != nil {
return true, err
}
status := statusList[0]
list, err := rewrite.CmdList()
if err != nil {
return true, err
}
for _, item := range list {
if item.Status != status {
continue
}
switch cmd {
case "add":
err = rewrite.CmdAdd(item.Path)
case "update":
err = rewrite.CmdUpdate(item.Path)
case "remove":
err = rewrite.CmdRemove(item.Path)
}
}
} else {
for _, arg := range args {
// Expand the list based on the analysis of the import tree.
if strings.HasSuffix(arg, "...") {
list, err := rewrite.CmdList()
if err != nil {
return true, err
}
base := strings.TrimSuffix(arg, "...")
for _, item := range list {
if strings.HasPrefix(item.Path, base) == false && strings.HasPrefix(item.VendorPath, base) == false {
continue
}
switch cmd {
case "add":
err = rewrite.CmdAdd(item.Path)
case "update":
err = rewrite.CmdUpdate(item.Path)
case "remove":
err = rewrite.CmdRemove(item.Path)
}
}
} else {
switch cmd {
case "add":
err = rewrite.CmdAdd(arg)
case "update":
err = rewrite.CmdUpdate(arg)
case "remove":
err = rewrite.CmdRemove(arg)
}
}
}
}
default:
return true, fmt.Errorf("Unknown command %q", cmd)
}
return false, err
}