diff --git a/pkg/build/build.go b/pkg/build/build.go index 3e21f5c..335ad06 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -23,15 +23,15 @@ func Run(opts Options) error { err error ) if testClient == nil { - client, err = s3utils.NewClient(false, opts.AwsProfile) + client, err = s3utils.NewClient(opts.AwsProfile) if err != nil { return err } } else { client = testClient } - - return root.LoopPathFiltered(opts.Options, root.BuildConfigPath, "building driver", "config", func(driverVersion, configPath string) error { + looper := root.NewFsLooper(root.BuildConfigPath) + return looper.LoopFiltered(opts.Options, "building driver", "config", func(driverVersion, configPath string) error { return buildConfig(client, opts, driverVersion, configPath) }) } diff --git a/pkg/cleanup/cleaner_file.go b/pkg/cleanup/cleaner_file.go index d9366e0..82a6816 100644 --- a/pkg/cleanup/cleaner_file.go +++ b/pkg/cleanup/cleaner_file.go @@ -5,10 +5,12 @@ import ( "os" ) -type fileCleaner struct{} +type fileCleaner struct { + root.Looper +} func NewFileCleaner() Cleaner { - return &fileCleaner{} + return &fileCleaner{Looper: root.NewFsLooper(root.BuildConfigPath)} } func (f *fileCleaner) Info() string { @@ -16,7 +18,7 @@ func (f *fileCleaner) Info() string { } func (f *fileCleaner) Cleanup(opts Options) error { - return root.LoopPathFiltered(opts.Options, root.BuildConfigPath, "removing file", "config", func(driverVersion, configPath string) error { + return f.LoopFiltered(opts.Options, "removing file", "config", func(driverVersion, configPath string) error { return os.Remove(configPath) }) } diff --git a/pkg/cleanup/cleaner_s3.go b/pkg/cleanup/cleaner_s3.go index 934f537..977936f 100644 --- a/pkg/cleanup/cleaner_s3.go +++ b/pkg/cleanup/cleaner_s3.go @@ -12,7 +12,7 @@ type s3Cleaner struct { } func NewS3Cleaner(awsProfile string) (Cleaner, error) { - client, err := s3utils.NewClient(false, awsProfile) + client, err := s3utils.NewClient(awsProfile) if err != nil { return nil, err } @@ -24,7 +24,7 @@ func (s *s3Cleaner) Info() string { } func (s *s3Cleaner) Cleanup(opts Options) error { - return s.LoopDriversFiltered(opts.Options, "cleaning up remote driver file", "key", func(driverVersion, key string) error { + return s.LoopFiltered(opts.Options, "cleaning up remote driver file", "key", func(driverVersion, key string) error { _, err := s.DeleteObject(context.Background(), &s3.DeleteObjectInput{ Bucket: aws.String(s3utils.S3Bucket), Key: aws.String(key), diff --git a/pkg/publish/publish.go b/pkg/publish/publish.go index 6172995..86ab630 100644 --- a/pkg/publish/publish.go +++ b/pkg/publish/publish.go @@ -16,14 +16,15 @@ func Run(opts Options) error { err error ) if testClient == nil { - client, err = s3utils.NewClient(false, opts.AwsProfile) + client, err = s3utils.NewClient(opts.AwsProfile) if err != nil { return err } } else { client = testClient } - return root.LoopPathFiltered(opts.Options, root.BuildOutputPath, "publishing", "driver", func(driverVersion, path string) error { + looper := root.NewFsLooper(root.BuildOutputPath) + return looper.LoopFiltered(opts.Options, "publishing", "driver", func(driverVersion, path string) error { return client.PutDriver(opts.Options, driverVersion, path) }) } diff --git a/pkg/publish/publish_test.go b/pkg/publish/publish_test.go index 0f5c884..985ac5f 100644 --- a/pkg/publish/publish_test.go +++ b/pkg/publish/publish_test.go @@ -32,7 +32,7 @@ func TestPublish(t *testing.T) { assert.NoError(t, err) // Fetch an existing object metadata - realClient, err := s3utils.NewClient(true, "") + realClient, err := s3utils.NewClient("") assert.NoError(t, err) object, err := realClient.HeadObject(context.Background(), &s3.HeadObjectInput{ Bucket: aws.String(s3utils.S3Bucket), diff --git a/pkg/root/types.go b/pkg/root/types.go index 662fb87..39cfe65 100644 --- a/pkg/root/types.go +++ b/pkg/root/types.go @@ -9,6 +9,22 @@ import ( "regexp" ) +type RowWorker func(driverVersion, path string) error + +type PathBuilder func(opts Options, driverVersion, configName string) string + +type Looper interface { + LoopFiltered(opts Options, message, tag string, worker RowWorker) error +} + +type FsLooper struct { + builder PathBuilder +} + +func NewFsLooper(builder PathBuilder) Looper { + return &FsLooper{builder: builder} +} + type Target struct { Distro builder.Type KernelRelease string diff --git a/pkg/root/utils.go b/pkg/root/utils.go index e373720..b7ddba4 100644 --- a/pkg/root/utils.go +++ b/pkg/root/utils.go @@ -6,14 +6,10 @@ import ( "path/filepath" ) -type PathWorker func(driverVersion, path string) error - -type PathBuilder func(opts Options, driverVersion, configName string) string - -func LoopPathFiltered(opts Options, pathBuilder PathBuilder, message, tag string, worker PathWorker) error { +func (f *FsLooper) LoopFiltered(opts Options, message, tag string, worker RowWorker) error { configNameGlob := opts.Target.toGlob() for _, driverVersion := range opts.DriverVersion { - path := pathBuilder(opts, driverVersion, configNameGlob) + path := f.builder(opts, driverVersion, configNameGlob) files, err := filepath.Glob(path) if err != nil { return err diff --git a/pkg/stats/statter_file.go b/pkg/stats/statter_file.go index 30a489d..d49a720 100644 --- a/pkg/stats/statter_file.go +++ b/pkg/stats/statter_file.go @@ -9,10 +9,12 @@ import ( "os" ) -type fileStatter struct{} +type fileStatter struct { + root.Looper +} func NewFileStatter() Statter { - return &fileStatter{} + return &fileStatter{Looper: root.NewFsLooper(root.BuildConfigPath)} } func (f *fileStatter) Info() string { @@ -21,7 +23,7 @@ func (f *fileStatter) Info() string { func (f *fileStatter) GetDriverStats(opts root.Options) (driverStatsByDriverVersion, error) { driverStatsByVersion := make(driverStatsByDriverVersion) - err := root.LoopPathFiltered(opts, root.BuildConfigPath, "computing stats", "config", func(driverVersion, configPath string) error { + err := f.LoopFiltered(opts, "computing stats", "config", func(driverVersion, configPath string) error { dStats := driverStatsByVersion[driverVersion] err := getConfigStats(&dStats, configPath) driverStatsByVersion[driverVersion] = dStats diff --git a/pkg/stats/statter_s3.go b/pkg/stats/statter_s3.go index c4b83e2..9ad2e2c 100644 --- a/pkg/stats/statter_s3.go +++ b/pkg/stats/statter_s3.go @@ -12,7 +12,7 @@ type s3Statter struct { } func NewS3Statter() (Statter, error) { - client, err := s3utils.NewClient(true, "UNNEEDED") + client, err := s3utils.NewClient("") if err != nil { return nil, err } @@ -27,7 +27,7 @@ func (s *s3Statter) GetDriverStats(opts root.Options) (driverStatsByDriverVersio slog.SetDefault(slog.With("bucket", s3utils.S3Bucket)) driverStatsByVersion := make(driverStatsByDriverVersion) - err := s.LoopDriversFiltered(opts, "computing stats", "key", func(driverVersion, key string) error { + err := s.LoopFiltered(opts, "computing stats", "key", func(driverVersion, key string) error { dStats := driverStatsByVersion[driverVersion] if strings.HasSuffix(key, ".ko") { dStats.NumModules++ diff --git a/pkg/utils/s3/s3utils.go b/pkg/utils/s3/s3utils.go index 2256ecf..7d25d10 100644 --- a/pkg/utils/s3/s3utils.go +++ b/pkg/utils/s3/s3utils.go @@ -20,9 +20,9 @@ const ( s3DriverNameRegexFmt = `^%s_(?P[a-zA-Z-0-9.0-9]*)_(?P.*)_(?P.*)(\.o|\.ko)` ) -func (cl *Client) LoopDriversFiltered(opts root.Options, +func (cl *Client) LoopFiltered(opts root.Options, message, tag string, - keyProcessor func(driverVersion, key string) error, + keyProcessor root.RowWorker, ) error { s3DriverNameRegex := regexp.MustCompile(fmt.Sprintf(s3DriverNameRegexFmt, opts.DriverName)) for _, driverVersion := range opts.DriverVersion { diff --git a/pkg/utils/s3/types.go b/pkg/utils/s3/types.go index 9d7c657..76781d7 100644 --- a/pkg/utils/s3/types.go +++ b/pkg/utils/s3/types.go @@ -11,12 +11,12 @@ type Client struct { *s3.Client } -func NewClient(readOnly bool, awsProfile string) (*Client, error) { +func NewClient(awsProfile string) (*Client, error) { var ( cfg aws.Config err error ) - if !readOnly { + if awsProfile != "" { cfg, err = config.LoadDefaultConfig(context.Background(), config.WithRegion(S3Bucket), config.WithSharedConfigProfile(awsProfile)) if err != nil { return nil, err diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go index 1dc0504..5be677a 100644 --- a/pkg/validate/validate.go +++ b/pkg/validate/validate.go @@ -14,7 +14,8 @@ import ( func Run(opts Options) error { slog.Info("validate config files") - return root.LoopPathFiltered(opts.Options, root.BuildConfigPath, "validating", "config", func(driverVersion, configPath string) error { + looper := root.NewFsLooper(root.BuildConfigPath) + return looper.LoopFiltered(opts.Options, "validating", "config", func(driverVersion, configPath string) error { return validateConfig(configPath, opts, driverVersion) }) }