Skip to content

Commit

Permalink
(feat): Move mode to model
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 25, 2023
1 parent 9baddab commit 03bcc2d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 80 deletions.
11 changes: 6 additions & 5 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"strings"

"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/model"
)

type Action interface {
Execute() error
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 {
Expand Down Expand Up @@ -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())
Expand Down
25 changes: 12 additions & 13 deletions internal/action/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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
}
Expand All @@ -51,7 +50,7 @@ type ChangeOwnerAction struct {
path string
uid int
gid int
mode config.Mode
mode model.Mode
fileService service.FileService
}

Expand All @@ -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,
}
}
Expand All @@ -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
}
Expand All @@ -93,15 +92,15 @@ func (a *ChangeOwnerAction) Success() string {
type ChangePermissionsAction struct {
path string
perms model.FilePermissions
mode config.Mode
mode model.Mode
fileService service.FileService
}

func NewChangePermissions(p string, perms model.FilePermissions, fs service.FileService) *ChangePermissionsAction {
return &ChangePermissionsAction{
path: p,
perms: perms,
mode: config.Empty,
mode: model.Empty,
fileService: fs,
}
}
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions internal/action/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ 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,
}
}

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
}
Expand Down
10 changes: 5 additions & 5 deletions internal/action/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ 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 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 {
return &LabelDeviceAction{
device: d,
label: label,
fileSystemService: fileSystemService,
mode: config.Empty,
mode: model.Empty,
}
}

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
}
Expand Down
17 changes: 8 additions & 9 deletions internal/action/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -25,19 +24,19 @@ func NewMountDeviceAction(source string, target string, fileSystem model.FileSys
fileSystem: fileSystem,
options: options,
deviceService: deviceService,
mode: config.Empty,
mode: model.Empty,
}
}

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
}
Expand All @@ -58,15 +57,15 @@ 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 {
return &UnmountDeviceAction{
source: source,
target: target,
deviceService: deviceService,
mode: config.Empty,
mode: model.Empty,
}
}

Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions internal/action/resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ 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 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 {
return &ResizeDeviceAction{
device: d,
target: target,
fileSystemService: fileSystemService,
mode: config.Empty,
mode: model.Empty,
}
}

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
}
Expand Down
Loading

0 comments on commit 03bcc2d

Please sign in to comment.