1package hotload
2
3import (
4	"fmt"
5	"plugin"
6
7	"github.com/go-critic/go-critic/framework/linter"
8)
9
10// CheckersFromDylib loads checkers provided by a dynamic lybrary found under path.
11//
12// The returned info slice must be re-assigned to the original info slice,
13// since there will be new entries there.
14func CheckersFromDylib(infoList []*linter.CheckerInfo, path string) ([]*linter.CheckerInfo, error) {
15	if path == "" {
16		return infoList, nil // Nothing to do
17	}
18	checkersBefore := len(infoList)
19	// Open plugin only for side-effects (init functions).
20	_, err := plugin.Open(path)
21	if err != nil {
22		return infoList, err
23	}
24	maybeUpdatedList := linter.GetCheckersInfo()
25	checkersAfter := len(maybeUpdatedList)
26	if checkersBefore == checkersAfter {
27		return infoList, fmt.Errorf("loaded plugin doesn't provide any gocritic-compatible checkers")
28	}
29	return maybeUpdatedList, nil
30}
31