mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-23 09:43:19 +00:00
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
![]() |
package caddycmd
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"runtime/debug"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/caddyserver/caddy/v2"
|
||
|
)
|
||
|
|
||
|
type moduleInfo struct {
|
||
|
caddyModuleID string
|
||
|
goModule *debug.Module
|
||
|
err error
|
||
|
}
|
||
|
|
||
|
func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
|
||
|
bi, ok := debug.ReadBuildInfo()
|
||
|
if !ok {
|
||
|
err = fmt.Errorf("no build info")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
for _, modID := range caddy.Modules() {
|
||
|
modInfo, err := caddy.GetModule(modID)
|
||
|
if err != nil {
|
||
|
// that's weird, shouldn't happen
|
||
|
unknown = append(unknown, moduleInfo{caddyModuleID: modID, err: err})
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// to get the Caddy plugin's version info, we need to know
|
||
|
// the package that the Caddy module's value comes from; we
|
||
|
// can use reflection but we need a non-pointer value (I'm
|
||
|
// not sure why), and since New() should return a pointer
|
||
|
// value, we need to dereference it first
|
||
|
iface := any(modInfo.New())
|
||
|
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
|
||
|
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
|
||
|
}
|
||
|
modPkgPath := reflect.TypeOf(iface).PkgPath()
|
||
|
|
||
|
// now we find the Go module that the Caddy module's package
|
||
|
// belongs to; we assume the Caddy module package path will
|
||
|
// be prefixed by its Go module path, and we will choose the
|
||
|
// longest matching prefix in case there are nested modules
|
||
|
var matched *debug.Module
|
||
|
for _, dep := range bi.Deps {
|
||
|
if strings.HasPrefix(modPkgPath, dep.Path) {
|
||
|
if matched == nil || len(dep.Path) > len(matched.Path) {
|
||
|
matched = dep
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
caddyModGoMod := moduleInfo{caddyModuleID: modID, goModule: matched}
|
||
|
|
||
|
if strings.HasPrefix(modPkgPath, caddy.ImportPath) {
|
||
|
standard = append(standard, caddyModGoMod)
|
||
|
} else {
|
||
|
nonstandard = append(nonstandard, caddyModGoMod)
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|