| 59 | } |
| 60 | |
| 61 | func (m Metadata) Validate() error { |
| 62 | var errs []error |
| 63 | |
| 64 | if !validPluginName.MatchString(m.Name) { |
| 65 | errs = append(errs, fmt.Errorf("invalid plugin name %q: must contain only a-z, A-Z, 0-9, _ and -", m.Name)) |
| 66 | } |
| 67 | |
| 68 | // Require version to be valid semver if specified |
| 69 | if m.Version != "" && !isValidSemver(m.Version) { |
| 70 | errs = append(errs, fmt.Errorf("invalid plugin version %q: must be valid semver", m.Version)) |
| 71 | } |
| 72 | |
| 73 | if m.APIVersion == "" { |
| 74 | errs = append(errs, errors.New("empty APIVersion")) |
| 75 | } |
| 76 | |
| 77 | if m.Type == "" { |
| 78 | errs = append(errs, errors.New("empty type field")) |
| 79 | } |
| 80 | |
| 81 | if m.Runtime == "" { |
| 82 | errs = append(errs, errors.New("empty runtime field")) |
| 83 | } |
| 84 | |
| 85 | if m.Config == nil { |
| 86 | errs = append(errs, errors.New("missing config field")) |
| 87 | } |
| 88 | |
| 89 | if m.RuntimeConfig == nil { |
| 90 | errs = append(errs, errors.New("missing runtimeConfig field")) |
| 91 | } |
| 92 | |
| 93 | // Validate the config itself |
| 94 | if m.Config != nil { |
| 95 | if err := m.Config.Validate(); err != nil { |
| 96 | errs = append(errs, fmt.Errorf("config validation failed: %w", err)) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Validate the runtime config itself |
| 101 | if m.RuntimeConfig != nil { |
| 102 | if err := m.RuntimeConfig.Validate(); err != nil { |
| 103 | errs = append(errs, fmt.Errorf("runtime config validation failed: %w", err)) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if len(errs) > 0 { |
| 108 | return errors.Join(errs...) |
| 109 | } |
| 110 | |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | func fromMetadataLegacy(m MetadataLegacy) *Metadata { |
| 115 | pluginType := "cli/v1" |