Skip to content

Commit ee51e3d

Browse files
author
Jay Conrod
committed
cmd/go: refactor modload.ListModules to accept bit flags
Instead of accepting bool flags, ListModules now accepts ListMode, a set of bit flags. Four flags are defined. listRetracted is split into ListRetracted and ListRetractedVersion to avoid ambiguity with -u, -retracted, and -versions. For #40357 Change-Id: Ibbbe44dc1e285ed17f27a6581f3392679f2124fb Reviewed-on: https://go-review.googlesource.com/c/go/+/306331 Trust: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 4230a6e commit ee51e3d

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

src/cmd/go/internal/list/list.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,20 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
451451
}
452452
}
453453

454-
mods, err := modload.ListModules(ctx, args, *listU, *listVersions, *listRetracted)
454+
var mode modload.ListMode
455+
if *listU {
456+
mode |= modload.ListU | modload.ListRetracted
457+
}
458+
if *listRetracted {
459+
mode |= modload.ListRetracted
460+
}
461+
if *listVersions {
462+
mode |= modload.ListVersions
463+
if *listRetracted {
464+
mode |= modload.ListRetractedVersions
465+
}
466+
}
467+
mods, err := modload.ListModules(ctx, args, mode)
455468
if !*listE {
456469
for _, m := range mods {
457470
if m.Error != nil {
@@ -686,9 +699,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
686699
}
687700

688701
if len(args) > 0 {
689-
listU := false
690-
listVersions := false
691-
rmods, err := modload.ListModules(ctx, args, listU, listVersions, *listRetracted)
702+
var mode modload.ListMode
703+
if *listRetracted {
704+
mode |= modload.ListRetracted
705+
}
706+
rmods, err := modload.ListModules(ctx, args, mode)
692707
if err != nil && !*listE {
693708
base.Errorf("go list -retracted: %v", err)
694709
}

src/cmd/go/internal/modcmd/download.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,9 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
132132
}
133133

134134
var mods []*moduleJSON
135-
listU := false
136-
listVersions := false
137-
listRetractions := false
138135
type token struct{}
139136
sem := make(chan token, runtime.GOMAXPROCS(0))
140-
infos, infosErr := modload.ListModules(ctx, args, listU, listVersions, listRetractions)
137+
infos, infosErr := modload.ListModules(ctx, args, 0)
141138
for _, info := range infos {
142139
if info.Replace != nil {
143140
info = info.Replace

src/cmd/go/internal/modcmd/why.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,13 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
7676
}
7777

7878
if *whyM {
79-
listU := false
80-
listVersions := false
81-
listRetractions := false
8279
for _, arg := range args {
8380
if strings.Contains(arg, "@") {
8481
base.Fatalf("go mod why: module query not allowed")
8582
}
8683
}
8784

88-
mods, err := modload.ListModules(ctx, args, listU, listVersions, listRetractions)
85+
mods, err := modload.ListModules(ctx, args, 0)
8986
if err != nil {
9087
base.Fatalf("go mod why: %v", err)
9188
}

src/cmd/go/internal/modload/build.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,17 @@ func PackageModuleInfo(ctx context.Context, pkgpath string) *modinfo.ModulePubli
6060
}
6161

6262
rs := LoadModFile(ctx)
63-
listRetracted := false
64-
return moduleInfo(ctx, rs, m, listRetracted)
63+
return moduleInfo(ctx, rs, m, 0)
6564
}
6665

6766
func ModuleInfo(ctx context.Context, path string) *modinfo.ModulePublic {
6867
if !Enabled() {
6968
return nil
7069
}
7170

72-
listRetracted := false
7371
if i := strings.Index(path, "@"); i >= 0 {
7472
m := module.Version{Path: path[:i], Version: path[i+1:]}
75-
return moduleInfo(ctx, nil, m, listRetracted)
73+
return moduleInfo(ctx, nil, m, 0)
7674
}
7775

7876
rs := LoadModFile(ctx)
@@ -101,7 +99,7 @@ func ModuleInfo(ctx context.Context, path string) *modinfo.ModulePublic {
10199
}
102100
}
103101

104-
return moduleInfo(ctx, rs, module.Version{Path: path, Version: v}, listRetracted)
102+
return moduleInfo(ctx, rs, module.Version{Path: path, Version: v}, 0)
105103
}
106104

107105
// addUpdate fills in m.Update if an updated version is available.
@@ -157,7 +155,7 @@ func addRetraction(ctx context.Context, m *modinfo.ModulePublic) {
157155
// moduleInfo returns information about module m, loaded from the requirements
158156
// in rs (which may be nil to indicate that m was not loaded from a requirement
159157
// graph).
160-
func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, listRetracted bool) *modinfo.ModulePublic {
158+
func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode ListMode) *modinfo.ModulePublic {
161159
if m == Target {
162160
info := &modinfo.ModulePublic{
163161
Path: m.Path,
@@ -226,7 +224,7 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, listRet
226224
}
227225
}
228226

229-
if listRetracted {
227+
if mode&ListRetracted != 0 {
230228
addRetraction(ctx, m)
231229
}
232230
}

src/cmd/go/internal/modload/list.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,36 @@ import (
2020
"golang.org/x/mod/module"
2121
)
2222

23+
type ListMode int
24+
25+
const (
26+
ListU ListMode = 1 << iota
27+
ListRetracted
28+
ListVersions
29+
ListRetractedVersions
30+
)
31+
2332
// ListModules returns a description of the modules matching args, if known,
2433
// along with any error preventing additional matches from being identified.
2534
//
2635
// 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)
2938

3039
type token struct{}
3140
sem := make(chan token, runtime.GOMAXPROCS(0))
32-
if listU || listVersions || listRetracted {
41+
if mode != 0 {
3342
for _, m := range mods {
3443
add := func(m *modinfo.ModulePublic) {
3544
sem <- token{}
3645
go func() {
37-
if listU {
46+
if mode&ListU != 0 {
3847
addUpdate(ctx, m)
3948
}
40-
if listVersions {
41-
addVersions(ctx, m, listRetracted)
49+
if mode&ListVersions != 0 {
50+
addVersions(ctx, m, mode&ListRetractedVersions != 0)
4251
}
43-
if listRetracted || listU {
52+
if mode&ListRetracted != 0 {
4453
addRetraction(ctx, m)
4554
}
4655
<-sem
@@ -64,7 +73,7 @@ func ListModules(ctx context.Context, args []string, listU, listVersions, listRe
6473
return mods, err
6574
}
6675

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) {
6877
var mg *ModuleGraph
6978
if go117LazyTODO {
7079
// Pull the args-loop below into another (new) loop.
@@ -78,7 +87,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
7887
}
7988

8089
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
8291
}
8392

8493
matchedModule := map[module.Version]bool{}
@@ -93,7 +102,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
93102
if arg == "all" || strings.Contains(arg, "...") {
94103
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
95104
}
96-
if !listVersions && !strings.Contains(arg, "@") {
105+
if mode&ListVersions == 0 && !strings.Contains(arg, "@") {
97106
base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot)
98107
}
99108
}
@@ -112,7 +121,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
112121
}
113122

114123
allowed := CheckAllowed
115-
if IsRevisionQuery(vers) || listRetracted {
124+
if IsRevisionQuery(vers) || mode&ListRetracted != 0 {
116125
// Allow excluded and retracted versions if the user asked for a
117126
// specific revision or used 'go list -retracted'.
118127
allowed = nil
@@ -131,7 +140,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
131140
// *Requirements instead.
132141
var noRS *Requirements
133142

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)
135144
mods = append(mods, mod)
136145
continue
137146
}
@@ -153,7 +162,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
153162
continue
154163
}
155164
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))
157166
} else if cfg.BuildMod == "vendor" {
158167
// In vendor mode, we can't determine whether a missing module is “a
159168
// known dependency” because the module graph is incomplete.
@@ -162,7 +171,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, listVersi
162171
Path: arg,
163172
Error: modinfoError(arg, "", errors.New("can't resolve module using the vendor directory\n\t(Use -mod=mod or -mod=readonly to bypass.)")),
164173
})
165-
} else if listVersions {
174+
} else if mode&ListVersions != 0 {
166175
// Don't make the user provide an explicit '@latest' when they're
167176
// explicitly asking what the available versions are. Instead, return a
168177
// 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
182191
matched = true
183192
if !matchedModule[m] {
184193
matchedModule[m] = true
185-
mods = append(mods, moduleInfo(ctx, rs, m, listRetracted))
194+
mods = append(mods, moduleInfo(ctx, rs, m, mode))
186195
}
187196
}
188197
}

0 commit comments

Comments
 (0)