@@ -20,27 +20,36 @@ import (
20
20
"golang.org/x/mod/module"
21
21
)
22
22
23
+ type ListMode int
24
+
25
+ const (
26
+ ListU ListMode = 1 << iota
27
+ ListRetracted
28
+ ListVersions
29
+ ListRetractedVersions
30
+ )
31
+
23
32
// ListModules returns a description of the modules matching args, if known,
24
33
// along with any error preventing additional matches from being identified.
25
34
//
26
35
// The returned slice can be nonempty even if the error is non-nil.
27
- func ListModules (ctx context.Context , args []string , listU , listVersions , listRetracted bool ) ([]* modinfo.ModulePublic , error ) {
28
- rs , mods , err := listModules (ctx , LoadModFile (ctx ), args , listVersions , listRetracted )
36
+ func ListModules (ctx context.Context , args []string , mode ListMode ) ([]* modinfo.ModulePublic , error ) {
37
+ rs , mods , err := listModules (ctx , LoadModFile (ctx ), args , mode )
29
38
30
39
type token struct {}
31
40
sem := make (chan token , runtime .GOMAXPROCS (0 ))
32
- if listU || listVersions || listRetracted {
41
+ if mode != 0 {
33
42
for _ , m := range mods {
34
43
add := func (m * modinfo.ModulePublic ) {
35
44
sem <- token {}
36
45
go func () {
37
- if listU {
46
+ if mode & ListU != 0 {
38
47
addUpdate (ctx , m )
39
48
}
40
- if listVersions {
41
- addVersions (ctx , m , listRetracted )
49
+ if mode & ListVersions != 0 {
50
+ addVersions (ctx , m , mode & ListRetractedVersions != 0 )
42
51
}
43
- if listRetracted || listU {
52
+ if mode & ListRetracted != 0 {
44
53
addRetraction (ctx , m )
45
54
}
46
55
<- sem
@@ -64,7 +73,7 @@ func ListModules(ctx context.Context, args []string, listU, listVersions, listRe
64
73
return mods , err
65
74
}
66
75
67
- func listModules (ctx context.Context , rs * Requirements , args []string , listVersions , listRetracted bool ) (_ * Requirements , mods []* modinfo.ModulePublic , mgErr error ) {
76
+ func listModules (ctx context.Context , rs * Requirements , args []string , mode ListMode ) (_ * Requirements , mods []* modinfo.ModulePublic , mgErr error ) {
68
77
var mg * ModuleGraph
69
78
if go117LazyTODO {
70
79
// Pull the args-loop below into another (new) loop.
@@ -78,7 +87,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
78
87
}
79
88
80
89
if len (args ) == 0 {
81
- return rs , []* modinfo.ModulePublic {moduleInfo (ctx , rs , Target , listRetracted )}, mgErr
90
+ return rs , []* modinfo.ModulePublic {moduleInfo (ctx , rs , Target , mode )}, mgErr
82
91
}
83
92
84
93
matchedModule := map [module.Version ]bool {}
@@ -93,7 +102,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
93
102
if arg == "all" || strings .Contains (arg , "..." ) {
94
103
base .Fatalf ("go: cannot match %q: %v" , arg , ErrNoModRoot )
95
104
}
96
- if ! listVersions && ! strings .Contains (arg , "@" ) {
105
+ if mode & ListVersions == 0 && ! strings .Contains (arg , "@" ) {
97
106
base .Fatalf ("go: cannot match %q without -versions or an explicit version: %v" , arg , ErrNoModRoot )
98
107
}
99
108
}
@@ -112,7 +121,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
112
121
}
113
122
114
123
allowed := CheckAllowed
115
- if IsRevisionQuery (vers ) || listRetracted {
124
+ if IsRevisionQuery (vers ) || mode & ListRetracted != 0 {
116
125
// Allow excluded and retracted versions if the user asked for a
117
126
// specific revision or used 'go list -retracted'.
118
127
allowed = nil
@@ -131,7 +140,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
131
140
// *Requirements instead.
132
141
var noRS * Requirements
133
142
134
- mod := moduleInfo (ctx , noRS , module.Version {Path : path , Version : info .Version }, listRetracted )
143
+ mod := moduleInfo (ctx , noRS , module.Version {Path : path , Version : info .Version }, mode )
135
144
mods = append (mods , mod )
136
145
continue
137
146
}
@@ -153,7 +162,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
153
162
continue
154
163
}
155
164
if v != "none" {
156
- mods = append (mods , moduleInfo (ctx , rs , module.Version {Path : arg , Version : v }, listRetracted ))
165
+ mods = append (mods , moduleInfo (ctx , rs , module.Version {Path : arg , Version : v }, mode ))
157
166
} else if cfg .BuildMod == "vendor" {
158
167
// In vendor mode, we can't determine whether a missing module is “a
159
168
// known dependency” because the module graph is incomplete.
@@ -162,7 +171,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
162
171
Path : arg ,
163
172
Error : modinfoError (arg , "" , errors .New ("can't resolve module using the vendor directory\n \t (Use -mod=mod or -mod=readonly to bypass.)" )),
164
173
})
165
- } else if listVersions {
174
+ } else if mode & ListVersions != 0 {
166
175
// Don't make the user provide an explicit '@latest' when they're
167
176
// explicitly asking what the available versions are. Instead, return a
168
177
// module with version "none", to which we can add the requested list.
@@ -182,7 +191,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
182
191
matched = true
183
192
if ! matchedModule [m ] {
184
193
matchedModule [m ] = true
185
- mods = append (mods , moduleInfo (ctx , rs , m , listRetracted ))
194
+ mods = append (mods , moduleInfo (ctx , rs , m , mode ))
186
195
}
187
196
}
188
197
}
0 commit comments