From 40979dfe3cbf979d55fb8bae8fdb2c75c4eb688f Mon Sep 17 00:00:00 2001 From: Lasith Koswatta Gamage Date: Sun, 26 Nov 2023 08:16:50 +0000 Subject: [PATCH] (feat): Further abstraction --- cmd/ebs-bootstrap.go | 2 +- internal/action/action.go | 10 +++---- internal/action/file.go | 2 +- internal/action/mount.go | 2 +- internal/service/device.go | 16 +++++----- internal/service/filesystem.go | 54 +++++++++++++++++++--------------- internal/utils/exec.go | 20 ++++++++----- 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/cmd/ebs-bootstrap.go b/cmd/ebs-bootstrap.go index cb03eba..212ffff 100644 --- a/cmd/ebs-bootstrap.go +++ b/cmd/ebs-bootstrap.go @@ -16,7 +16,7 @@ func main() { log.SetFlags(0) // Services - rc := utils.NewRunnerCache() + rc := utils.NewExecRunnerFactory() ufs := service.NewUnixFileService() lds := service.NewLinuxDeviceService(rc) uos := service.NewUnixOwnerService() diff --git a/internal/action/action.go b/internal/action/action.go index a9f15ef..1f8ef2f 100644 --- a/internal/action/action.go +++ b/internal/action/action.go @@ -38,14 +38,14 @@ type Action interface { } type ActionExecutor struct { - runnerCache *utils.RunnerCache - config *config.Config + RunnerFactory utils.RunnerFactory + config *config.Config } -func NewActionExecutor(rc *utils.RunnerCache, c *config.Config) *ActionExecutor { +func NewActionExecutor(rc utils.RunnerFactory, c *config.Config) *ActionExecutor { return &ActionExecutor{ - runnerCache: rc, - config: c, + RunnerFactory: rc, + config: c, } } diff --git a/internal/action/file.go b/internal/action/file.go index 8e1e0a7..0a58762 100644 --- a/internal/action/file.go +++ b/internal/action/file.go @@ -119,7 +119,7 @@ func (a *ChangePermissionsAction) Execute() error { return os.Chmod(a.path, mode) } -func (a *ChangePermissionsAction) Preflight(rc *utils.RunnerCache) error { +func (a *ChangePermissionsAction) Preflight(rc *utils.ExecRunnerFactory) error { return nil } diff --git a/internal/action/mount.go b/internal/action/mount.go index d5d98a3..f7ee965 100644 --- a/internal/action/mount.go +++ b/internal/action/mount.go @@ -73,7 +73,7 @@ func (a *UnmountDeviceAction) Execute() error { return a.deviceService.Umount(a.source, a.target) } -func (a *UnmountDeviceAction) Preflight(rc *utils.RunnerCache) error { +func (a *UnmountDeviceAction) Preflight(rc *utils.ExecRunnerFactory) error { return nil } diff --git a/internal/service/device.go b/internal/service/device.go index c9cc366..5839704 100644 --- a/internal/service/device.go +++ b/internal/service/device.go @@ -22,7 +22,7 @@ type DeviceService interface { // Device Service Interface [END] type LinuxDeviceService struct { - runnerCache *utils.RunnerCache + RunnerFactory utils.RunnerFactory } type LsblkBlockDeviceResponse struct { @@ -37,14 +37,14 @@ type LsblkBlockDevice struct { MountPoint *string `json:"mountpoint"` } -func NewLinuxDeviceService(rc *utils.RunnerCache) *LinuxDeviceService { +func NewLinuxDeviceService(rc utils.RunnerFactory) *LinuxDeviceService { return &LinuxDeviceService{ - runnerCache: rc, + RunnerFactory: rc, } } func (du *LinuxDeviceService) GetSize(name string) (int, error) { - r := du.runnerCache.GetRunner(utils.BlockDev) + r := du.RunnerFactory.Select(utils.BlockDev) output, err := r.Command("--getsize64", name) if err != nil { return -1, err @@ -57,7 +57,7 @@ func (du *LinuxDeviceService) GetSize(name string) (int, error) { } func (du *LinuxDeviceService) GetBlockDevices() ([]string, error) { - r := du.runnerCache.GetRunner(utils.Lsblk) + r := du.RunnerFactory.Select(utils.Lsblk) output, err := r.Command("--nodeps", "-o", "NAME,LABEL,FSTYPE,MOUNTPOINT", "-J") if err != nil { return nil, err @@ -75,7 +75,7 @@ func (du *LinuxDeviceService) GetBlockDevices() ([]string, error) { } func (du *LinuxDeviceService) GetBlockDevice(name string) (*model.BlockDevice, error) { - r := du.runnerCache.GetRunner(utils.Lsblk) + r := du.RunnerFactory.Select(utils.Lsblk) output, err := r.Command("--nodeps", "-o", "NAME,UUID,LABEL,FSTYPE,MOUNTPOINT", "-J", name) if err != nil { return nil, err @@ -102,7 +102,7 @@ func (du *LinuxDeviceService) GetBlockDevice(name string) (*model.BlockDevice, e } func (du *LinuxDeviceService) Mount(source string, target string, fs model.FileSystem, options model.MountOptions) error { - r := du.runnerCache.GetRunner(utils.Mount) + r := du.RunnerFactory.Select(utils.Mount) _, err := r.Command(source, "-t", string(fs), "-o", options.String(), target) if err != nil { return err @@ -111,7 +111,7 @@ func (du *LinuxDeviceService) Mount(source string, target string, fs model.FileS } func (du *LinuxDeviceService) Umount(source string, target string) error { - r := du.runnerCache.GetRunner(utils.Umount) + r := du.RunnerFactory.Select(utils.Umount) _, err := r.Command(target) if err != nil { return err diff --git a/internal/service/filesystem.go b/internal/service/filesystem.go index 0834d17..1d6eb5f 100644 --- a/internal/service/filesystem.go +++ b/internal/service/filesystem.go @@ -23,32 +23,40 @@ type FileSystemServiceFactory interface { } type LinuxFileSystemServiceFactory struct { - services map[model.FileSystem]FileSystemService + services map[model.FileSystem]FileSystemService + RunnerFactory utils.RunnerFactory } -func NewLinuxFileSystemServiceFactory(rc *utils.RunnerCache) *LinuxFileSystemServiceFactory { +func NewLinuxFileSystemServiceFactory(rc utils.RunnerFactory) *LinuxFileSystemServiceFactory { return &LinuxFileSystemServiceFactory{ - services: map[model.FileSystem]FileSystemService{ - model.Ext4: NewExt4Service(rc), - model.Xfs: NewXfsService(rc), - }, + services: map[model.FileSystem]FileSystemService{}, + RunnerFactory: rc, } } func (fsf *LinuxFileSystemServiceFactory) Select(fs model.FileSystem) (FileSystemService, error) { fss, exists := fsf.services[fs] - if !exists { - return nil, fmt.Errorf("🔴 An unsupported filesystem was provided") - } + if exists { + return fss, nil + } + 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 } type Ext4Service struct { - runnerCache *utils.RunnerCache + RunnerFactory utils.RunnerFactory } -func NewExt4Service(rc *utils.RunnerCache) *Ext4Service { - return &Ext4Service{runnerCache: rc} +func NewExt4Service(rc utils.RunnerFactory) *Ext4Service { + return &Ext4Service{RunnerFactory: rc} } func (es *Ext4Service) GetFileSystem() model.FileSystem { @@ -56,7 +64,7 @@ func (es *Ext4Service) GetFileSystem() model.FileSystem { } func (es *Ext4Service) Format(name string) error { - r := es.runnerCache.GetRunner(utils.MkfsExt4) + r := es.RunnerFactory.Select(utils.MkfsExt4) _, err := r.Command(name) if err != nil { return err @@ -68,7 +76,7 @@ func (es *Ext4Service) Label(name string, label string) error { if len(label) > 16 { return fmt.Errorf("🔴 %s: Label '%s'cannot exceed 16 characters for the ext4 file system", name, label) } - r := es.runnerCache.GetRunner(utils.E2Label) + r := es.RunnerFactory.Select(utils.E2Label) _, err := r.Command(name, label) if err != nil { return err @@ -77,7 +85,7 @@ func (es *Ext4Service) Label(name string, label string) error { } func (es *Ext4Service) Resize(name string) error { - r := es.runnerCache.GetRunner(utils.Resize2fs) + r := es.RunnerFactory.Select(utils.Resize2fs) _, err := r.Command(name) if err != nil { return err @@ -86,7 +94,7 @@ func (es *Ext4Service) Resize(name string) error { } func (es *Ext4Service) GetSize(name string) (int, error) { - r := es.runnerCache.GetRunner(utils.Tune2fs) + r := es.RunnerFactory.Select(utils.Tune2fs) output, err := r.Command("-l", name) if err != nil { return 0, err @@ -128,11 +136,11 @@ func (es *Ext4Service) DoesResizeRequiresMount() bool { } type XfsService struct { - runnerCache *utils.RunnerCache + RunnerFactory utils.RunnerFactory } -func NewXfsService(rc *utils.RunnerCache) *XfsService { - return &XfsService{runnerCache: rc} +func NewXfsService(rc utils.RunnerFactory) *XfsService { + return &XfsService{RunnerFactory: rc} } func (es *XfsService) GetFileSystem() model.FileSystem { @@ -140,7 +148,7 @@ func (es *XfsService) GetFileSystem() model.FileSystem { } func (xs *XfsService) Format(name string) error { - r := xs.runnerCache.GetRunner(utils.MkfsXfs) + r := xs.RunnerFactory.Select(utils.MkfsXfs) _, err := r.Command(name) if err != nil { return err @@ -152,7 +160,7 @@ func (xs *XfsService) Label(name string, label string) error { if len(label) > 12 { return fmt.Errorf("🔴 %s: Label '%s' cannot exceed 12 characters for the XFS file system", name, label) } - r := xs.runnerCache.GetRunner(utils.XfsAdmin) + r := xs.RunnerFactory.Select(utils.XfsAdmin) _, err := r.Command("-L", label, name) if err != nil { return err @@ -161,7 +169,7 @@ func (xs *XfsService) Label(name string, label string) error { } func (es *XfsService) Resize(name string) error { - r := es.runnerCache.GetRunner(utils.XfsGrowfs) + r := es.RunnerFactory.Select(utils.XfsGrowfs) _, err := r.Command(name) if err != nil { return err @@ -170,7 +178,7 @@ func (es *XfsService) Resize(name string) error { } func (xs *XfsService) GetSize(name string) (int, error) { - r := xs.runnerCache.GetRunner(utils.XfsInfo) + r := xs.RunnerFactory.Select(utils.XfsInfo) output, err := r.Command(name) if err != nil { return 0, err diff --git a/internal/utils/exec.go b/internal/utils/exec.go index 965954d..b7bc0d9 100644 --- a/internal/utils/exec.go +++ b/internal/utils/exec.go @@ -23,21 +23,25 @@ const ( XfsGrowfs Binary = "xfs_growfs" ) -type RunnerCache struct { - cache map[Binary]Runner +type RunnerFactory interface { + Select(binary Binary) Runner } -func NewRunnerCache() *RunnerCache { - return &RunnerCache{ - cache: map[Binary]Runner{}, +type ExecRunnerFactory struct { + runners map[Binary]Runner +} + +func NewExecRunnerFactory() *ExecRunnerFactory { + return &ExecRunnerFactory{ + runners: map[Binary]Runner{}, } } -func (rc *RunnerCache) GetRunner(binary Binary) Runner { - r, exists := rc.cache[binary] +func (rc *ExecRunnerFactory) Select(binary Binary) Runner { + r, exists := rc.runners[binary] if !exists { r = NewExecRunner(binary) - rc.cache[binary] = r + rc.runners[binary] = r } return r }