Skip to content

Commit

Permalink
(feat): Further abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Nov 26, 2023
1 parent 8a860b1 commit 40979df
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmd/ebs-bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/action/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/action/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
16 changes: 8 additions & 8 deletions internal/service/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DeviceService interface {
// Device Service Interface [END]

type LinuxDeviceService struct {
runnerCache *utils.RunnerCache
RunnerFactory utils.RunnerFactory
}

type LsblkBlockDeviceResponse struct {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
54 changes: 31 additions & 23 deletions internal/service/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,48 @@ 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 {
return model.Ext4
}

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -128,19 +136,19 @@ 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 {
return model.Xfs
}

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
20 changes: 12 additions & 8 deletions internal/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 40979df

Please sign in to comment.