Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
cmd: Organize list-modules output; --packages flag (#3925)
Matt Holt committed
Jan 4, 2021 at 11:11 UTC
144b65cf993f7078cccee9e1e119d18537922b81
2 files changed
+62
-12
cmd/commandfuncs.go
+60
-11
@@ -360,12 +360,37 @@ func cmdBuildInfo(fl Flags) (int, error) {
360
}
361
362
func cmdListModules(fl Flags) (int, error) {
363
+ packages := fl.Bool("packages")
364
versions := fl.Bool("versions")
365
366
+ type moduleInfo struct {
367
+ caddyModuleID string
368
+ goModule *debug.Module
369
+ err error
370
+ }
371
+ printModuleInfo := func(mi moduleInfo) {
372
+ fmt.Print(mi.caddyModuleID)
373
+ if versions && mi.goModule != nil {
374
+ fmt.Print(" " + mi.goModule.Version)
375
+ }
376
+ if packages && mi.goModule != nil {
377
+ fmt.Print(" " + mi.goModule.Path)
378
+ if mi.goModule.Replace != nil {
379
+ fmt.Print(" => " + mi.goModule.Replace.Path)
380
+ }
381
+ }
382
+ if mi.err != nil {
383
+ fmt.Printf(" [%v]", mi.err)
384
+ }
385
+ fmt.Println()
386
+ }
387
+
388
+ // organize modules by whether they come with the standard distribution
389
+ var standard, nonstandard, unknown []moduleInfo
390
+
391
bi, ok := debug.ReadBuildInfo()
366
- if !ok || !versions {
367
- // if there's no build information,
368
- // just print out the modules
392
+ if !ok {
393
+ // oh well, just print the module IDs and exit
394
for _, m := range caddy.Modules() {
395
fmt.Println(m)
396
}
@@ -375,8 +400,8 @@ func cmdListModules(fl Flags) (int, error) {
400
for _, modID := range caddy.Modules() {
401
modInfo, err := caddy.GetModule(modID)
402
if err != nil {
378
- // that's weird
379
- fmt.Println(modID)
403
+ // that's weird, shouldn't happen
404
+ unknown = append(unknown, moduleInfo{caddyModuleID: modID, err: err})
405
continue
406
}
407
@@ -404,15 +429,39 @@ func cmdListModules(fl Flags) (int, error) {
429
}
430
}
431
407
- // if we could find no matching module, just print out
408
- // the module ID instead
409
- if matched == nil {
410
- fmt.Println(modID)
411
- continue
432
+ caddyModGoMod := moduleInfo{caddyModuleID: modID, goModule: matched}
433
+
434
+ if strings.HasPrefix(modPkgPath, caddy.ImportPath) {
435
+ standard = append(standard, caddyModGoMod)
436
+ } else {
437
+ nonstandard = append(nonstandard, caddyModGoMod)
438
}
439
+ }
440
414
- fmt.Printf("%s %s\n", modID, matched.Version)
441
+ if len(standard) > 0 {
442
+ for _, mod := range standard {
443
+ printModuleInfo(mod)
444
+ }
445
+ }
446
+ fmt.Printf("\n Standard modules: %d\n", len(standard))
447
+ if len(nonstandard) > 0 {
448
+ if len(standard) > 0 {
449
+ fmt.Println()
450
+ }
451
+ for _, mod := range nonstandard {
452
+ printModuleInfo(mod)
453
+ }
454
+ }
455
+ fmt.Printf("\n Non-standard modules: %d\n", len(nonstandard))
456
+ if len(unknown) > 0 {
457
+ if len(standard) > 0 || len(nonstandard) > 0 {
458
+ fmt.Println()
459
+ }
460
+ for _, mod := range unknown {
461
+ printModuleInfo(mod)
462
+ }
463
}
464
+ fmt.Printf("\n Unknown modules: %d\n", len(unknown))
465
466
return caddy.ExitCodeSuccess, nil
467
}
cmd/commands.go
+2
-1
@@ -191,10 +191,11 @@ config file; otherwise the default is assumed.`,
191
RegisterCommand(Command{
192
Name: "list-modules",
193
Func: cmdListModules,
194
- Usage: "[--versions]",
194
+ Usage: "[--packages] [--versions]",
195
Short: "Lists the installed Caddy modules",
196
Flags: func() *flag.FlagSet {
197
fs := flag.NewFlagSet("list-modules", flag.ExitOnError)
198
+ fs.Bool("packages", false, "Print package paths")
199
fs.Bool("versions", false, "Print version information")
200
return fs
201
}(),