mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-26 20:01:02 +00:00
d815b20c54
This argument is pretty useless, so let's remove it and let addons get their config on their own. Note that this is a breaking change, but as [per docs](https://woodpecker-ci.org/docs/next/administration/addons/overview) the addon implementation can change at any time.
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package addon
|
|
|
|
import (
|
|
"errors"
|
|
"plugin"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/addon/types"
|
|
)
|
|
|
|
var pluginCache = map[string]*plugin.Plugin{}
|
|
|
|
type Addon[T any] struct {
|
|
Type types.Type
|
|
Value T
|
|
}
|
|
|
|
func Load[T any](files []string, t types.Type) (*Addon[T], error) {
|
|
for _, file := range files {
|
|
if _, has := pluginCache[file]; !has {
|
|
p, err := plugin.Open(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pluginCache[file] = p
|
|
}
|
|
|
|
typeLookup, err := pluginCache[file].Lookup("Type")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if addonType, is := typeLookup.(*types.Type); !is {
|
|
return nil, errors.New("addon type is incorrect")
|
|
} else if *addonType != t {
|
|
continue
|
|
}
|
|
|
|
mainLookup, err := pluginCache[file].Lookup("Addon")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
main, is := mainLookup.(func(zerolog.Logger) (T, error))
|
|
if !is {
|
|
return nil, errors.New("addon main function has incorrect type")
|
|
}
|
|
|
|
logger := log.Logger.With().Str("addon", file).Logger()
|
|
|
|
mainOut, err := main(logger)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Addon[T]{
|
|
Type: t,
|
|
Value: mainOut,
|
|
}, nil
|
|
}
|
|
|
|
return nil, nil
|
|
}
|