diff --git a/pkg/generate/generate.go b/pkg/generate/generate.go index b9b7637..7e2a6c0 100644 --- a/pkg/generate/generate.go +++ b/pkg/generate/generate.go @@ -15,7 +15,6 @@ import ( "log/slog" "os" "path/filepath" - "regexp" "strings" ) @@ -80,21 +79,6 @@ func Run(opts Options) error { func autogenerateConfigs(opts Options, jsonData []byte) error { slog.SetDefault(slog.With("target-distro", opts.Distro)) - distroFilter := func(distro string) bool { - matched, _ := regexp.MatchString(opts.Distro, distro) - return matched - } - - kernelreleaseFilter := func(kernelrelease string) bool { - matched, _ := regexp.MatchString(opts.KernelRelease, kernelrelease) - return matched - } - - kernelversionFilter := func(kernelversion string) bool { - matched, _ := regexp.MatchString(opts.KernelVersion, kernelversion) - return matched - } - // Generate a dynamic struct with all needed distros // NOTE: we might need a single distro when `lastDistro` is != "*"; // else, we will add all SupportedDistros found in constants.go @@ -102,7 +86,7 @@ func autogenerateConfigs(opts Options, jsonData []byte) error { instanceBuilder := dynamicstruct.NewStruct() for distro, _ := range root.SupportedDistros { distroStr := string(distro) - if distroFilter(distroStr) { + if opts.DistroFilter(distroStr) { tag := fmt.Sprintf(`json:"%s"`, distroStr) instanceBuilder.AddField(distroStr, []validate.KernelEntry{}, tag) distroCtr++ @@ -133,10 +117,10 @@ func autogenerateConfigs(opts Options, jsonData []byte) error { errGrp.Go(func() error { for _, kernelEntry := range kernelEntries { // Skip unneeded kernel entries - if !kernelreleaseFilter(kernelEntry.KernelRelease) { + if !opts.KernelVersionFilter(kernelEntry.KernelRelease) { continue } - if !kernelversionFilter(kernelEntry.KernelVersion) { + if !opts.KernelVersionFilter(kernelEntry.KernelVersion) { continue } diff --git a/pkg/generate/generate_test.go b/pkg/generate/generate_test.go index fbf6498..adb6bb7 100644 --- a/pkg/generate/generate_test.go +++ b/pkg/generate/generate_test.go @@ -9,7 +9,30 @@ import ( "testing" ) -func TestAutogenerate(t *testing.T) { +func BenchmarkAutogenerate(b *testing.B) { + testCacheData = true // enable json data caching for subsequent tests + opts := Options{ + Options: root.Options{ + RepoRoot: "./test/", + Architecture: "x86_64", + DriverVersion: []string{"5.0.1+driver"}, + }, + DriverName: "falco", + Auto: true, + } + + b.StopTimer() + + for n := 0; n < b.N; n++ { + b.StartTimer() + err := Run(opts) + assert.NoError(b, err) + b.StopTimer() + _ = os.RemoveAll("./test/") + } +} + +func TestGenerate(t *testing.T) { testCacheData = true // enable json data caching for subsequent tests tests := map[string]struct { opts Options diff --git a/pkg/root/types.go b/pkg/root/types.go index c3a58f9..c8b912a 100644 --- a/pkg/root/types.go +++ b/pkg/root/types.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/spf13/viper" "log/slog" + "regexp" ) type Target struct { @@ -34,6 +35,21 @@ func (t Target) ToGlob() string { return fmt.Sprintf("%s_%s_%s.yaml", t.Distro, t.KernelRelease, t.KernelVersion) } +func (t Target) DistroFilter(distro string) bool { + matched, _ := regexp.MatchString(t.Distro, distro) + return matched +} + +func (t Target) KernelReleaseFilter(kernelrelease string) bool { + matched, _ := regexp.MatchString(t.KernelRelease, kernelrelease) + return matched +} + +func (t Target) KernelVersionFilter(kernelversion string) bool { + matched, _ := regexp.MatchString(t.KernelVersion, kernelversion) + return matched +} + type Options struct { DryRun bool RepoRoot string diff --git a/pkg/utils/s3.go b/pkg/utils/s3.go index 62cf1b5..4ed2bde 100644 --- a/pkg/utils/s3.go +++ b/pkg/utils/s3.go @@ -46,21 +46,6 @@ func LoopBucketFiltered(client *s3.Client, dkDistro := kDistro.ToDriverkitDistro() opts.Distro = string(dkDistro) - distroFilter := func(distro string) bool { - matched, _ := regexp.MatchString(opts.Distro, distro) - return matched - } - - kernelreleaseFilter := func(kernelrelease string) bool { - matched, _ := regexp.MatchString(opts.KernelRelease, kernelrelease) - return matched - } - - kernelversionFilter := func(kernelversion string) bool { - matched, _ := regexp.MatchString(opts.KernelVersion, kernelversion) - return matched - } - prefix := filepath.Join("driver", driverVersion, opts.Architecture) params := &s3.ListObjectsV2Input{ Bucket: aws.String(S3Bucket), @@ -93,15 +78,15 @@ func LoopBucketFiltered(client *s3.Client, if i > 0 && i <= len(matches) { switch name { case "Distro": - if !distroFilter(matches[i]) { + if !opts.DistroFilter(matches[i]) { continue keyLoop } case "KernelRelease": - if !kernelreleaseFilter(matches[i]) { + if !opts.KernelReleaseFilter(matches[i]) { continue keyLoop } case "KernelVersion": - if !kernelversionFilter(matches[i]) { + if !opts.KernelVersionFilter(matches[i]) { continue keyLoop } }