Skip to content

Commit

Permalink
chore(pkg): refactored LoopFilterX API, adding a new Looper interface.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Sep 6, 2023
1 parent 3d52809 commit 39c8aac
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 27 deletions.
6 changes: 3 additions & 3 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/cleanup/cleaner_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ 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 {
return "cleaning up local config files"
}

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)
})
}
4 changes: 2 additions & 2 deletions pkg/cleanup/cleaner_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions pkg/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
2 changes: 1 addition & 1 deletion pkg/publish/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
16 changes: 16 additions & 0 deletions pkg/root/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/root/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pkg/stats/statter_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/stats/statter_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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++
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/s3/s3utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const (
s3DriverNameRegexFmt = `^%s_(?P<Distro>[a-zA-Z-0-9.0-9]*)_(?P<KernelRelease>.*)_(?P<KernelVersion>.*)(\.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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/s3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 39c8aac

Please sign in to comment.