diff --git a/internal/service/filesystem.go b/internal/service/filesystem.go index cff7ee7..1a44bf8 100644 --- a/internal/service/filesystem.go +++ b/internal/service/filesystem.go @@ -25,23 +25,29 @@ type FileSystemServiceFactory interface { type LinuxFileSystemServiceFactory struct { RunnerFactory utils.RunnerFactory + services map[model.FileSystem]FileSystemService } func NewLinuxFileSystemServiceFactory(rc utils.RunnerFactory) *LinuxFileSystemServiceFactory { return &LinuxFileSystemServiceFactory{ RunnerFactory: rc, + services: map[model.FileSystem]FileSystemService{}, } } func (fsf *LinuxFileSystemServiceFactory) Select(fs model.FileSystem) (FileSystemService, error) { - var fss FileSystemService - switch fs { - case model.Ext4: - fss = NewExt4Service(fsf.RunnerFactory) - case model.Xfs: - fss = NewXfsService(fsf.RunnerFactory) - case model.Unformatted: - return nil, fmt.Errorf("🔴 An unsupported filesystem was encountered") + // Check if the instance is already in the services cache + fss, exists := fsf.services[fs] + if !exists { + switch fs { + case model.Ext4: + fss = NewExt4Service(fsf.RunnerFactory) + case model.Xfs: + fss = NewXfsService(fsf.RunnerFactory) + case model.Unformatted: + return nil, fmt.Errorf("🔴 An unsupported filesystem was encountered") + } + fsf.services[fs] = fss } return fss, nil }