From 03bcc2d40963a8487fdf3de2680ae3bd03352468 Mon Sep 17 00:00:00 2001 From: lasith-kg Date: Mon, 25 Dec 2023 01:02:46 +0000 Subject: [PATCH] (feat): Move mode to model --- internal/action/action.go | 11 ++++---- internal/action/file.go | 25 +++++++++--------- internal/action/format.go | 10 +++---- internal/action/label.go | 10 +++---- internal/action/mount.go | 17 ++++++------ internal/action/resize.go | 10 +++---- internal/config/config.go | 51 +++++++++++------------------------- internal/config/validator.go | 6 ++--- internal/model/action.go | 22 ++++++++++++++++ 9 files changed, 82 insertions(+), 80 deletions(-) create mode 100644 internal/model/action.go diff --git a/internal/action/action.go b/internal/action/action.go index bf1a169..4f3b6f5 100644 --- a/internal/action/action.go +++ b/internal/action/action.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/reecetech/ebs-bootstrap/internal/config" + "github.com/reecetech/ebs-bootstrap/internal/model" ) type Action interface { @@ -13,8 +14,8 @@ type Action interface { Success() string Prompt() string Refuse() string - GetMode() config.Mode - SetMode(mode config.Mode) Action + GetMode() model.Mode + SetMode(mode model.Mode) Action } type ActionExecutor interface { @@ -48,13 +49,13 @@ func (dae *DefaultActionExecutor) Execute(actions []Action) error { func (dae *DefaultActionExecutor) execute(action Action) error { switch action.GetMode() { - case config.Force: + case model.Force: break - case config.Prompt: + case model.Prompt: if !dae.shouldProceed(action) { return fmt.Errorf("🔴 Action rejected. %s", action.Refuse()) } - case config.Healthcheck: + case model.Healthcheck: return fmt.Errorf("🔴 Healthcheck mode enabled. %s", action.Refuse()) default: return fmt.Errorf("🔴 Unexpected mode detected. %s", action.Refuse()) diff --git a/internal/action/file.go b/internal/action/file.go index d99ba6b..a58003e 100644 --- a/internal/action/file.go +++ b/internal/action/file.go @@ -3,21 +3,20 @@ package action import ( "fmt" - "github.com/reecetech/ebs-bootstrap/internal/config" "github.com/reecetech/ebs-bootstrap/internal/model" "github.com/reecetech/ebs-bootstrap/internal/service" ) type CreateDirectoryAction struct { path string - mode config.Mode + mode model.Mode fileService service.FileService } func NewCreateDirectoryAction(p string, fs service.FileService) *CreateDirectoryAction { return &CreateDirectoryAction{ path: p, - mode: config.Empty, + mode: model.Empty, fileService: fs, } } @@ -26,11 +25,11 @@ func (a *CreateDirectoryAction) Execute() error { return a.fileService.CreateDirectory(a.path) } -func (a *CreateDirectoryAction) GetMode() config.Mode { +func (a *CreateDirectoryAction) GetMode() model.Mode { return a.mode } -func (a *CreateDirectoryAction) SetMode(mode config.Mode) Action { +func (a *CreateDirectoryAction) SetMode(mode model.Mode) Action { a.mode = mode return a } @@ -51,7 +50,7 @@ type ChangeOwnerAction struct { path string uid int gid int - mode config.Mode + mode model.Mode fileService service.FileService } @@ -60,7 +59,7 @@ func NewChangeOwnerAction(p string, uid int, gid int, fs service.FileService) *C path: p, uid: uid, gid: gid, - mode: config.Empty, + mode: model.Empty, fileService: fs, } } @@ -69,11 +68,11 @@ func (a *ChangeOwnerAction) Execute() error { return a.fileService.ChangeOwner(a.path, a.uid, a.gid) } -func (a *ChangeOwnerAction) GetMode() config.Mode { +func (a *ChangeOwnerAction) GetMode() model.Mode { return a.mode } -func (a *ChangeOwnerAction) SetMode(mode config.Mode) Action { +func (a *ChangeOwnerAction) SetMode(mode model.Mode) Action { a.mode = mode return a } @@ -93,7 +92,7 @@ func (a *ChangeOwnerAction) Success() string { type ChangePermissionsAction struct { path string perms model.FilePermissions - mode config.Mode + mode model.Mode fileService service.FileService } @@ -101,7 +100,7 @@ func NewChangePermissions(p string, perms model.FilePermissions, fs service.File return &ChangePermissionsAction{ path: p, perms: perms, - mode: config.Empty, + mode: model.Empty, fileService: fs, } } @@ -110,11 +109,11 @@ func (a *ChangePermissionsAction) Execute() error { return a.fileService.ChangePermissions(a.path, a.perms) } -func (a *ChangePermissionsAction) GetMode() config.Mode { +func (a *ChangePermissionsAction) GetMode() model.Mode { return a.mode } -func (a *ChangePermissionsAction) SetMode(mode config.Mode) Action { +func (a *ChangePermissionsAction) SetMode(mode model.Mode) Action { a.mode = mode return a } diff --git a/internal/action/format.go b/internal/action/format.go index f623b54..185e137 100644 --- a/internal/action/format.go +++ b/internal/action/format.go @@ -3,21 +3,21 @@ package action import ( "fmt" - "github.com/reecetech/ebs-bootstrap/internal/config" + "github.com/reecetech/ebs-bootstrap/internal/model" "github.com/reecetech/ebs-bootstrap/internal/service" ) type FormatDeviceAction struct { device string fileSystemService service.FileSystemService - mode config.Mode + mode model.Mode } func NewFormatDeviceAction(d string, fileSystemService service.FileSystemService) *FormatDeviceAction { return &FormatDeviceAction{ device: d, fileSystemService: fileSystemService, - mode: config.Empty, + mode: model.Empty, } } @@ -25,11 +25,11 @@ func (a *FormatDeviceAction) Execute() error { return a.fileSystemService.Format(a.device) } -func (a *FormatDeviceAction) GetMode() config.Mode { +func (a *FormatDeviceAction) GetMode() model.Mode { return a.mode } -func (a *FormatDeviceAction) SetMode(mode config.Mode) Action { +func (a *FormatDeviceAction) SetMode(mode model.Mode) Action { a.mode = mode return a } diff --git a/internal/action/label.go b/internal/action/label.go index 8def265..088dbc4 100644 --- a/internal/action/label.go +++ b/internal/action/label.go @@ -3,7 +3,7 @@ package action import ( "fmt" - "github.com/reecetech/ebs-bootstrap/internal/config" + "github.com/reecetech/ebs-bootstrap/internal/model" "github.com/reecetech/ebs-bootstrap/internal/service" ) @@ -11,7 +11,7 @@ type LabelDeviceAction struct { device string label string fileSystemService service.FileSystemService - mode config.Mode + mode model.Mode } func NewLabelDeviceAction(d string, label string, fileSystemService service.FileSystemService) *LabelDeviceAction { @@ -19,7 +19,7 @@ func NewLabelDeviceAction(d string, label string, fileSystemService service.File device: d, label: label, fileSystemService: fileSystemService, - mode: config.Empty, + mode: model.Empty, } } @@ -27,11 +27,11 @@ func (a *LabelDeviceAction) Execute() error { return a.fileSystemService.Label(a.device, a.label) } -func (a *LabelDeviceAction) GetMode() config.Mode { +func (a *LabelDeviceAction) GetMode() model.Mode { return a.mode } -func (a *LabelDeviceAction) SetMode(mode config.Mode) Action { +func (a *LabelDeviceAction) SetMode(mode model.Mode) Action { a.mode = mode return a } diff --git a/internal/action/mount.go b/internal/action/mount.go index fa4da92..c5b6706 100644 --- a/internal/action/mount.go +++ b/internal/action/mount.go @@ -3,7 +3,6 @@ package action import ( "fmt" - "github.com/reecetech/ebs-bootstrap/internal/config" "github.com/reecetech/ebs-bootstrap/internal/model" "github.com/reecetech/ebs-bootstrap/internal/service" "github.com/reecetech/ebs-bootstrap/internal/utils" @@ -15,7 +14,7 @@ type MountDeviceAction struct { fileSystem model.FileSystem options model.MountOptions deviceService service.DeviceService - mode config.Mode + mode model.Mode } func NewMountDeviceAction(source string, target string, fileSystem model.FileSystem, options model.MountOptions, deviceService service.DeviceService) *MountDeviceAction { @@ -25,7 +24,7 @@ func NewMountDeviceAction(source string, target string, fileSystem model.FileSys fileSystem: fileSystem, options: options, deviceService: deviceService, - mode: config.Empty, + mode: model.Empty, } } @@ -33,11 +32,11 @@ func (a *MountDeviceAction) Execute() error { return a.deviceService.Mount(a.source, a.target, a.fileSystem, a.options) } -func (a *MountDeviceAction) GetMode() config.Mode { +func (a *MountDeviceAction) GetMode() model.Mode { return a.mode } -func (a *MountDeviceAction) SetMode(mode config.Mode) Action { +func (a *MountDeviceAction) SetMode(mode model.Mode) Action { a.mode = mode return a } @@ -58,7 +57,7 @@ type UnmountDeviceAction struct { source string target string deviceService service.DeviceService - mode config.Mode + mode model.Mode } func NewUnmountDeviceAction(source string, target string, deviceService service.DeviceService) *UnmountDeviceAction { @@ -66,7 +65,7 @@ func NewUnmountDeviceAction(source string, target string, deviceService service. source: source, target: target, deviceService: deviceService, - mode: config.Empty, + mode: model.Empty, } } @@ -78,11 +77,11 @@ func (a *UnmountDeviceAction) Preflight(rc *utils.ExecRunnerFactory) error { return nil } -func (a *UnmountDeviceAction) GetMode() config.Mode { +func (a *UnmountDeviceAction) GetMode() model.Mode { return a.mode } -func (a *UnmountDeviceAction) SetMode(mode config.Mode) Action { +func (a *UnmountDeviceAction) SetMode(mode model.Mode) Action { a.mode = mode return a } diff --git a/internal/action/resize.go b/internal/action/resize.go index 163e02e..fcdbdfd 100644 --- a/internal/action/resize.go +++ b/internal/action/resize.go @@ -3,7 +3,7 @@ package action import ( "fmt" - "github.com/reecetech/ebs-bootstrap/internal/config" + "github.com/reecetech/ebs-bootstrap/internal/model" "github.com/reecetech/ebs-bootstrap/internal/service" ) @@ -11,7 +11,7 @@ type ResizeDeviceAction struct { device string target string fileSystemService service.FileSystemService - mode config.Mode + mode model.Mode } func NewResizeDeviceAction(d string, target string, fileSystemService service.FileSystemService) *ResizeDeviceAction { @@ -19,7 +19,7 @@ func NewResizeDeviceAction(d string, target string, fileSystemService service.Fi device: d, target: target, fileSystemService: fileSystemService, - mode: config.Empty, + mode: model.Empty, } } @@ -27,11 +27,11 @@ func (a *ResizeDeviceAction) Execute() error { return a.fileSystemService.Resize(a.target) } -func (a *ResizeDeviceAction) GetMode() config.Mode { +func (a *ResizeDeviceAction) GetMode() model.Mode { return a.mode } -func (a *ResizeDeviceAction) SetMode(mode config.Mode) Action { +func (a *ResizeDeviceAction) SetMode(mode model.Mode) Action { a.mode = mode return a } diff --git a/internal/config/config.go b/internal/config/config.go index 70ea758..785f66d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,25 +19,6 @@ const ( DefaultResizeThreshold = 99.999 ) -type Mode string - -const ( - Empty Mode = "" - Healthcheck Mode = "healthcheck" - Prompt Mode = "prompt" - Force Mode = "force" -) - -func ParseMode(s string) (Mode, error) { - m := Mode(s) - switch m { - case Empty, Healthcheck, Prompt, Force: - return m, nil - default: - return m, fmt.Errorf("🔴 %s: Mode is not supported", s) - } -} - type Flag struct { Config string Mode string @@ -54,24 +35,24 @@ type Device struct { Group string `yaml:"group"` Label string `yaml:"label"` Permissions model.FilePermissions `yaml:"permissions"` - Mode Mode `yaml:"mode"` + Mode model.Mode `yaml:"mode"` Remount bool `yaml:"remount"` ResizeFs bool `yaml:"resizeFs"` ResizeThreshold float64 `yaml:"resizeThreshold"` } type Defaults struct { - Mode Mode `yaml:"mode"` - Remount bool `yaml:"remount"` - ResizeFs bool `yaml:"resizeFs"` - ResizeThreshold float64 `yaml:"resizeThreshold"` + Mode model.Mode `yaml:"mode"` + Remount bool `yaml:"remount"` + ResizeFs bool `yaml:"resizeFs"` + ResizeThreshold float64 `yaml:"resizeThreshold"` } type Overrides struct { - Mode Mode `yaml:"mode"` - Remount bool `yaml:"remount"` - ResizeFs bool `yaml:"resizeFs"` - ResizeThreshold float64 `yaml:"resizeThreshold"` + Mode model.Mode `yaml:"mode"` + Remount bool `yaml:"remount"` + ResizeFs bool `yaml:"resizeFs"` + ResizeThreshold float64 `yaml:"resizeThreshold"` } // We don't export "overrides" as this is an attribute that is used @@ -136,28 +117,28 @@ func parseFlags(program string, args []string) (*Flag, error) { } func (c *Config) setOverrides(f *Flag) *Config { - c.overrides.Mode = Mode(f.Mode) + c.overrides.Mode = model.Mode(f.Mode) c.overrides.Remount = f.Remount c.overrides.ResizeFs = f.ResizeFs c.overrides.ResizeThreshold = f.ResizeThreshold return c } -func (c *Config) GetMode(name string) Mode { +func (c *Config) GetMode(name string) model.Mode { cd, found := c.Devices[name] if !found { - return Healthcheck + return model.Healthcheck } - if c.overrides.Mode != Empty { + if c.overrides.Mode != model.Empty { return c.overrides.Mode } - if cd.Mode != Empty { + if cd.Mode != model.Empty { return cd.Mode } - if c.Defaults.Mode != Empty { + if c.Defaults.Mode != model.Empty { return c.Defaults.Mode } - return Healthcheck + return model.Healthcheck } func (c *Config) GetRemount(name string) bool { diff --git a/internal/config/validator.go b/internal/config/validator.go index e643c4d..c699c72 100644 --- a/internal/config/validator.go +++ b/internal/config/validator.go @@ -59,20 +59,20 @@ func NewModeValidator() *ModeValidator { func (fsv *ModeValidator) Validate(c *Config) error { mode := string(c.Defaults.Mode) - _, err := ParseMode(mode) + _, err := model.ParseMode(mode) if err != nil { return fmt.Errorf("🔴 %s is not a supported global mode", mode) } mode = string(c.overrides.Mode) - _, err = ParseMode(mode) + _, err = model.ParseMode(mode) if err != nil { return fmt.Errorf("🔴 %s is not a supported overrides mode", mode) } for name, device := range c.Devices { mode := string(device.Mode) - _, err := ParseMode(mode) + _, err := model.ParseMode(mode) if err != nil { return fmt.Errorf("🔴 %s: %s is not a supported mode", name, mode) } diff --git a/internal/model/action.go b/internal/model/action.go new file mode 100644 index 0000000..d8c923a --- /dev/null +++ b/internal/model/action.go @@ -0,0 +1,22 @@ +package model + +import "fmt" + +type Mode string + +const ( + Empty Mode = "" + Healthcheck Mode = "healthcheck" + Prompt Mode = "prompt" + Force Mode = "force" +) + +func ParseMode(s string) (Mode, error) { + m := Mode(s) + switch m { + case Empty, Healthcheck, Prompt, Force: + return m, nil + default: + return m, fmt.Errorf("🔴 %s: Mode is not supported", s) + } +}