-
Notifications
You must be signed in to change notification settings - Fork 586
/
generator.go
75 lines (61 loc) · 2.09 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package compatibility
import (
"fmt"
"github.com/openshift/api/tools/codegen/pkg/generation"
"k8s.io/klog/v2"
)
// Options contains the configuration required for the compatibility generator.
type Options struct {
// Disabled indicates whether the compatibility generator is enabled or not.
// This default to false as the compatibility generator is enabled by default.
Disabled bool
// Verify determines whether the generator should verify the content instead
// of updating the generated file.
Verify bool
}
// generator implements the generation.Generator interface.
// It is designed to generate compatibility level comments for a particular API group.
type generator struct {
disabled bool
verify bool
}
// NewGenerator builds a new compatibility generator.
func NewGenerator(opts Options) generation.Generator {
return &generator{
disabled: opts.Disabled,
verify: opts.Verify,
}
}
// ApplyConfig creates returns a new generator based on the configuration passed.
// If the compatibility configuration is empty, the existing generation is returned.
func (g *generator) ApplyConfig(config *generation.Config) generation.Generator {
if config == nil || config.Compatibility == nil {
return g
}
return NewGenerator(Options{
Disabled: config.Compatibility.Disabled,
Verify: g.verify,
})
}
// Name returns the name of the generator.
func (g *generator) Name() string {
return "compatibility"
}
// GenGroup runs the compatibility generator against the given group context.
func (g *generator) GenGroup(groupCtx generation.APIGroupContext) error {
if g.disabled {
klog.V(2).Infof("Skipping compatibility generation for %s", groupCtx.Name)
return nil
}
for _, version := range groupCtx.Versions {
action := "Generating"
if g.verify {
action = "Verifying"
}
klog.V(1).Infof("%s compatibility level comments for %s/%s", action, groupCtx.Name, version.Name)
if err := insertCompatibilityLevelComments(version.Path, g.verify); err != nil {
return fmt.Errorf("could not insert compatibility level comments for %s/%s: %w", groupCtx.Name, version.Name, err)
}
}
return nil
}