@@ -10,14 +10,15 @@ import (
10
10
"go/ast"
11
11
"go/parser"
12
12
"go/token"
13
- "gopkg.in/yaml.v3"
14
13
"io/fs"
15
14
"log"
16
15
"os"
17
16
"path/filepath"
18
17
"sort"
19
18
"strings"
20
19
"unicode"
20
+
21
+ "gopkg.in/yaml.v3"
21
22
)
22
23
23
24
func main () {
@@ -76,7 +77,7 @@ func run(folder string, allowlistFilePath string) error {
76
77
}
77
78
78
79
var componentType string
79
- if _ , err : = os .Stat (filepath .Join (base , "metadata.yaml" )); errors .Is (err , os .ErrNotExist ) {
80
+ if _ , err = os .Stat (filepath .Join (base , "metadata.yaml" )); errors .Is (err , os .ErrNotExist ) {
80
81
componentType = "pkg"
81
82
} else {
82
83
m , err := os .ReadFile (filepath .Join (base , "metadata.yaml" ))
@@ -215,10 +216,10 @@ func walkFolder(folder string, componentType string) error {
215
216
return nil
216
217
}
217
218
218
- if len (result .Functions ) > 1 && componentType != "pkg" {
219
+ if len (result .Functions ) > 1 && componentType != "pkg" && componentType != "cmd" {
219
220
return fmt .Errorf ("%s has more than one function: %q" , folder , strings .Join (fnNames , "," ))
220
221
}
221
- if len (result .Functions ) == 1 && componentType != "pkg" {
222
+ if len (result .Functions ) == 1 && componentType != "pkg" && componentType != "cmd" {
222
223
if err := checkFactoryFunction (result .Functions [0 ], folder , componentType ); err != nil {
223
224
return err
224
225
}
@@ -233,6 +234,33 @@ func walkFolder(folder string, componentType string) error {
233
234
234
235
// check the only exported function of the module is NewFactory, matching the signature of the factory expected by the collector builder.
235
236
func checkFactoryFunction (newFactoryFn * function , folder string , componentType string ) error {
237
+ switch componentType {
238
+ case "provider" :
239
+ return checkProviderFactoryFunction (newFactoryFn , folder , componentType )
240
+ default :
241
+ return checkComponentFactoryFunction (newFactoryFn , folder , componentType )
242
+ }
243
+ }
244
+
245
+ func checkProviderFactoryFunction (newFactoryFn * function , folder string , componentType string ) error {
246
+ if newFactoryFn .Name != "NewFactory" {
247
+ return fmt .Errorf ("%s does not define a NewFactory function as a %s" , folder , componentType )
248
+ }
249
+ if newFactoryFn .Receiver != "" {
250
+ return fmt .Errorf ("%s associated NewFactory with a receiver type" , folder )
251
+ }
252
+ if len (newFactoryFn .ReturnTypes ) != 1 {
253
+ return fmt .Errorf ("%s NewFactory function returns more than one result" , folder )
254
+ }
255
+ returnType := newFactoryFn .ReturnTypes [0 ]
256
+
257
+ if returnType != "confmap.ProviderFactory" {
258
+ return fmt .Errorf ("%s NewFactory function does not return a valid type: %s, expected confmap.ProviderFactory" , folder , returnType )
259
+ }
260
+ return nil
261
+ }
262
+
263
+ func checkComponentFactoryFunction (newFactoryFn * function , folder string , componentType string ) error {
236
264
if newFactoryFn .Name != "NewFactory" {
237
265
return fmt .Errorf ("%s does not define a NewFactory function as a %s" , folder , componentType )
238
266
}
0 commit comments