MCPcopy
hub / github.com/caddyserver/caddy / cmdListModules

Function cmdListModules

cmd/commandfuncs.go:438–539  ·  view source on GitHub ↗
(fl Flags)

Source from the content-addressed store, hash-verified

436}
437
438func cmdListModules(fl Flags) (int, error) {
439 packages := fl.Bool("packages")
440 versions := fl.Bool("versions")
441 skipStandard := fl.Bool("skip-standard")
442 jsonOutput := fl.Bool("json")
443
444 // Organize modules by whether they come with the standard distribution
445 standard, nonstandard, unknown, err := getModules()
446 if err != nil {
447 // If module info can't be fetched, just print the IDs and exit
448 for _, m := range caddy.Modules() {
449 fmt.Println(m)
450 }
451 return caddy.ExitCodeSuccess, nil
452 }
453
454 // Logic for JSON output
455 if jsonOutput {
456 output := []jsonModuleInfo{}
457
458 // addToOutput is a helper to convert internal module info to the JSON-serializable struct
459 addToOutput := func(list []moduleInfo, moduleType string) {
460 for _, mi := range list {
461 item := jsonModuleInfo{
462 ModuleName: mi.caddyModuleID,
463 ModuleType: moduleType, // Mapping the type here
464 }
465 if mi.goModule != nil {
466 item.Version = mi.goModule.Version
467 item.PackageURL = mi.goModule.Path
468 }
469 output = append(output, item)
470 }
471 }
472
473 // Pass the respective type for each category
474 if !skipStandard {
475 addToOutput(standard, "standard")
476 }
477 addToOutput(nonstandard, "non-standard")
478 addToOutput(unknown, "unknown")
479
480 jsonBytes, err := json.MarshalIndent(output, "", " ")
481 if err != nil {
482 return caddy.ExitCodeFailedQuit, err
483 }
484 fmt.Println(string(jsonBytes))
485 return caddy.ExitCodeSuccess, nil
486 }
487
488 // Logic for Text output (Fallback)
489 printModuleInfo := func(mi moduleInfo) {
490 fmt.Print(mi.caddyModuleID)
491 if versions && mi.goModule != nil {
492 fmt.Print(" " + mi.goModule.Version)
493 }
494 if packages && mi.goModule != nil {
495 fmt.Print(" " + mi.goModule.Path)

Callers

nothing calls this directly

Calls 4

getModulesFunction · 0.85
ModulesMethod · 0.80
PrintlnMethod · 0.80
BoolMethod · 0.45

Tested by

no test coverage detected