cmd: Organize list-modules output; --packages flag (#3925)

This commit is contained in:
Matt Holt 2021-01-04 11:11:56 -07:00 committed by GitHub
parent c8557dc00b
commit 144b65cf99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 13 deletions

View File

@ -360,12 +360,37 @@ func cmdBuildInfo(fl Flags) (int, error) {
} }
func cmdListModules(fl Flags) (int, error) { func cmdListModules(fl Flags) (int, error) {
packages := fl.Bool("packages")
versions := fl.Bool("versions") versions := fl.Bool("versions")
type moduleInfo struct {
caddyModuleID string
goModule *debug.Module
err error
}
printModuleInfo := func(mi moduleInfo) {
fmt.Print(mi.caddyModuleID)
if versions && mi.goModule != nil {
fmt.Print(" " + mi.goModule.Version)
}
if packages && mi.goModule != nil {
fmt.Print(" " + mi.goModule.Path)
if mi.goModule.Replace != nil {
fmt.Print(" => " + mi.goModule.Replace.Path)
}
}
if mi.err != nil {
fmt.Printf(" [%v]", mi.err)
}
fmt.Println()
}
// organize modules by whether they come with the standard distribution
var standard, nonstandard, unknown []moduleInfo
bi, ok := debug.ReadBuildInfo() bi, ok := debug.ReadBuildInfo()
if !ok || !versions { if !ok {
// if there's no build information, // oh well, just print the module IDs and exit
// just print out the modules
for _, m := range caddy.Modules() { for _, m := range caddy.Modules() {
fmt.Println(m) fmt.Println(m)
} }
@ -375,8 +400,8 @@ func cmdListModules(fl Flags) (int, error) {
for _, modID := range caddy.Modules() { for _, modID := range caddy.Modules() {
modInfo, err := caddy.GetModule(modID) modInfo, err := caddy.GetModule(modID)
if err != nil { if err != nil {
// that's weird // that's weird, shouldn't happen
fmt.Println(modID) unknown = append(unknown, moduleInfo{caddyModuleID: modID, err: err})
continue continue
} }
@ -404,16 +429,40 @@ func cmdListModules(fl Flags) (int, error) {
} }
} }
// if we could find no matching module, just print out caddyModGoMod := moduleInfo{caddyModuleID: modID, goModule: matched}
// the module ID instead
if matched == nil {
fmt.Println(modID)
continue
}
fmt.Printf("%s %s\n", modID, matched.Version) if strings.HasPrefix(modPkgPath, caddy.ImportPath) {
standard = append(standard, caddyModGoMod)
} else {
nonstandard = append(nonstandard, caddyModGoMod)
}
} }
if len(standard) > 0 {
for _, mod := range standard {
printModuleInfo(mod)
}
}
fmt.Printf("\n Standard modules: %d\n", len(standard))
if len(nonstandard) > 0 {
if len(standard) > 0 {
fmt.Println()
}
for _, mod := range nonstandard {
printModuleInfo(mod)
}
}
fmt.Printf("\n Non-standard modules: %d\n", len(nonstandard))
if len(unknown) > 0 {
if len(standard) > 0 || len(nonstandard) > 0 {
fmt.Println()
}
for _, mod := range unknown {
printModuleInfo(mod)
}
}
fmt.Printf("\n Unknown modules: %d\n", len(unknown))
return caddy.ExitCodeSuccess, nil return caddy.ExitCodeSuccess, nil
} }

View File

@ -191,10 +191,11 @@ config file; otherwise the default is assumed.`,
RegisterCommand(Command{ RegisterCommand(Command{
Name: "list-modules", Name: "list-modules",
Func: cmdListModules, Func: cmdListModules,
Usage: "[--versions]", Usage: "[--packages] [--versions]",
Short: "Lists the installed Caddy modules", Short: "Lists the installed Caddy modules",
Flags: func() *flag.FlagSet { Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("list-modules", flag.ExitOnError) fs := flag.NewFlagSet("list-modules", flag.ExitOnError)
fs.Bool("packages", false, "Print package paths")
fs.Bool("versions", false, "Print version information") fs.Bool("versions", false, "Print version information")
return fs return fs
}(), }(),