pluginsdk/plugin/load_once.go
2026-08-10 23:04:47 +08:00

27 lines
777 B
Go

package plugin
import (
"fmt"
"regexp"
)
const MaxLoadOnceKeyBytes = 128
var loadOnceKeyPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`)
// ValidateLoadOnceKeys checks the stable identity contract shared by plugin
// authors, the packer, and the host loader.
func ValidateLoadOnceKeys(keys []string) error {
seen := make(map[string]bool, len(keys))
for _, key := range keys {
if len(key) == 0 || len(key) > MaxLoadOnceKeyBytes || !loadOnceKeyPattern.MatchString(key) {
return fmt.Errorf("invalid load-once key %q: must be 1-%d bytes of lowercase letters, digits, dot, underscore, or hyphen, starting alphanumeric", key, MaxLoadOnceKeyBytes)
}
if seen[key] {
return fmt.Errorf("duplicate load-once key %q", key)
}
seen[key] = true
}
return nil
}