Skip to content

Commit

Permalink
(feat): Add warning capability to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Nov 23, 2023
1 parent ba5bedf commit dc0fc5a
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 102 deletions.
23 changes: 14 additions & 9 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ const (
// immidiately on the file-system. Therefore a delay is not required
// for actions that peform file changes
DefaultFileActionDelay = 0
DisabledWarning = ""
FormatActionWarning = "Formatting larger disks can take several seconds ⌛ Please wait patiently 🙇"
)

type Action interface {
Execute(rc *utils.RunnerCache) error
GetdeviceName() string
GetDeviceName() string
IsTrusted() bool
GetDelay() time.Duration
Success() string
Prompt() string
Refuse() string
IsTrusted() bool
GetDelay() time.Duration
Warning() string
}

type ActionExecutor struct {
Expand All @@ -48,7 +51,7 @@ func NewActionExecutor(rc *utils.RunnerCache, c *config.Config) *ActionExecutor
}

func (ae *ActionExecutor) ExecuteAction(action Action) error {
name := action.GetdeviceName()
name := action.GetDeviceName()
mode, err := ae.config.GetMode(name)
if err != nil {
return err
Expand All @@ -60,25 +63,27 @@ func (ae *ActionExecutor) ExecuteAction(action Action) error {
return fmt.Errorf("🔴 Action rejected. %s", action.Refuse())
}
case config.Healtcheck:
// Special handling for trusted actions when device is under
// healthcheck mode
// Special handling for trusted actions
if action.IsTrusted() {
if !ae.config.GetAllowTrustedActions() {
log.Printf("🙅 Skipped trusted action. %s", action.Refuse())
return nil
}
break
break;
}
return fmt.Errorf("🔴%s: Healthcheck mode enabled. %s", name, action.Refuse())
return fmt.Errorf("🔴 %s: Healthcheck mode enabled. %s", name, action.Refuse())
}
return ae.executeAction(action)
}

func (ae *ActionExecutor) executeAction(action Action) error {
if w := action.Warning(); w != DisabledWarning {
log.Printf("🟠 %s: %s", action.GetDeviceName(), w)
}
if err := action.Execute(ae.runnerCache); err != nil {
return err
}
log.Printf("⭐ %s: %s", action.GetdeviceName(), action.Success())
log.Printf("⭐ %s: %s", action.GetDeviceName(), action.Success())
// Add a delay because from experience, the operating system needs some time
// to catch up to device changes performed through C tools like e2label and mkfs.ext4
time.Sleep(action.GetDelay())
Expand Down
70 changes: 43 additions & 27 deletions internal/action/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ func (a *CreateDirectoryAction) Execute(rc *utils.RunnerCache) error {
return os.MkdirAll(a.path, DefaultDirectoryPermissions)
}

func (a *CreateDirectoryAction) Prompt() string {
return fmt.Sprintf("Would you like to recursively create directory %s", a.path)
func (a *CreateDirectoryAction) GetDeviceName() string {
return a.deviceName
}

func (a *CreateDirectoryAction) GetdeviceName() string {
return a.deviceName
func (a *CreateDirectoryAction) IsTrusted() bool {
return false
}

func (a *CreateDirectoryAction) GetDelay() time.Duration {
return DefaultFileActionDelay
}

func (a *CreateDirectoryAction) Prompt() string {
return fmt.Sprintf("Would you like to recursively create directory %s", a.path)
}

func (a *CreateDirectoryAction) Refuse() string {
Expand All @@ -45,12 +53,8 @@ func (a *CreateDirectoryAction) Success() string {
return fmt.Sprintf("Successfully created directory %s", a.path)
}

func (a *CreateDirectoryAction) IsTrusted() bool {
return false
}

func (a *CreateDirectoryAction) GetDelay() time.Duration {
return DefaultFileActionDelay
func (a *CreateDirectoryAction) Warning() string {
return DisabledWarning
}

type ChangeOwnerAction struct {
Expand All @@ -73,13 +77,22 @@ func (a *ChangeOwnerAction) Execute(rc *utils.RunnerCache) error {
return os.Chown(a.path, a.uid, a.gid)
}

func (a *ChangeOwnerAction) GetDeviceName() string {
return a.deviceName
}

func (a *ChangeOwnerAction) IsTrusted() bool {
return false
}

func (a *ChangeOwnerAction) GetDelay() time.Duration {
return DefaultFileActionDelay
}

func (a *ChangeOwnerAction) Prompt() string {
return fmt.Sprintf("Would you like to change ownership (%d:%d) of %s", a.uid, a.gid, a.path)
}

func (a *ChangeOwnerAction) GetdeviceName() string {
return a.deviceName
}

func (a *ChangeOwnerAction) Refuse() string {
return fmt.Sprintf("Refused to to change ownership (%d:%d) of %s", a.uid, a.gid, a.path)
Expand All @@ -89,12 +102,8 @@ func (a *ChangeOwnerAction) Success() string {
return fmt.Sprintf("Successfully changed ownership (%d:%d) of %s", a.uid, a.gid, a.path)
}

func (a *ChangeOwnerAction) IsTrusted() bool {
return false
}

func (a *ChangeOwnerAction) GetDelay() time.Duration {
return DefaultFileActionDelay
func (a *ChangeOwnerAction) Warning() string {
return DisabledWarning
}

type ChangePermissionsAction struct {
Expand All @@ -119,13 +128,22 @@ func (a *ChangePermissionsAction) Execute(rc *utils.RunnerCache) error {
return os.Chmod(a.path, mode)
}

func (a *ChangePermissionsAction) GetDeviceName() string {
return a.deviceName
}

func (a *ChangePermissionsAction) IsTrusted() bool {
return false
}

func (a *ChangePermissionsAction) GetDelay() time.Duration {
return DefaultFileActionDelay
}

func (a *ChangePermissionsAction) Prompt() string {
return fmt.Sprintf("Would you like to change permissions of %s to %s", a.path, a.perms)
}

func (a *ChangePermissionsAction) GetdeviceName() string {
return a.deviceName
}

func (a *ChangePermissionsAction) Refuse() string {
return fmt.Sprintf("Refused to to change permissions of %s to %s", a.path, a.perms)
Expand All @@ -135,10 +153,8 @@ func (a *ChangePermissionsAction) Success() string {
return fmt.Sprintf("Successfully change permissions of %s to %s", a.path, a.perms)
}

func (a *ChangePermissionsAction) IsTrusted() bool {
return false
func (a *ChangePermissionsAction) Warning() string {
return DisabledWarning
}

func (a *ChangePermissionsAction) GetDelay() time.Duration {
return DefaultFileActionDelay
}

44 changes: 27 additions & 17 deletions internal/action/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ func (a *FormatExt4Action) Execute(rc *utils.RunnerCache) error {
return nil
}

func (a *FormatExt4Action) Prompt() string {
return fmt.Sprintf("Would you like to format %s to ext4", a.deviceName)
func (a *FormatExt4Action) GetDeviceName() string {
return a.deviceName
}

func (a *FormatExt4Action) GetdeviceName() string {
return a.deviceName
func (a *FormatExt4Action) IsTrusted() bool {
return false
}

func (a *FormatExt4Action) GetDelay() time.Duration {
return DefaultDeviceActionDelay
}

func (a *FormatExt4Action) Prompt() string {
return fmt.Sprintf("Would you like to format %s to ext4", a.deviceName)
}

func (a *FormatExt4Action) Refuse() string {
Expand All @@ -42,13 +50,10 @@ func (a *FormatExt4Action) Success() string {
return "Successfully formated to ext4"
}

func (a *FormatExt4Action) IsTrusted() bool {
return false
func (a *FormatExt4Action) Warning() string {
return FormatActionWarning
}

func (a *FormatExt4Action) GetDelay() time.Duration {
return DefaultDeviceActionDelay
}

type FormatXfsAction struct {
deviceName string
Expand All @@ -61,6 +66,7 @@ func NewFormatXfsAction(dn string) *FormatXfsAction {
}

func (a *FormatXfsAction) Execute(rc *utils.RunnerCache) error {
fmt.Println("⌛ Note: Formatting larger disks can take several seconds. Please wait patiently 🙇")
r := rc.GetRunner(utils.MkfsXfs)
_, err := r.Command(a.deviceName)
if err != nil {
Expand All @@ -69,12 +75,20 @@ func (a *FormatXfsAction) Execute(rc *utils.RunnerCache) error {
return nil
}

func (a *FormatXfsAction) GetDeviceName() string {
return a.deviceName
}

func (a *FormatXfsAction) Prompt() string {
return fmt.Sprintf("Would you like to format %s to XFS?", a.deviceName)
}

func (a *FormatXfsAction) GetdeviceName() string {
return a.deviceName
func (a *FormatXfsAction) IsTrusted() bool {
return false
}

func (a *FormatXfsAction) GetDelay() time.Duration {
return DefaultDeviceActionDelay
}

func (a *FormatXfsAction) Refuse() string {
Expand All @@ -85,10 +99,6 @@ func (a *FormatXfsAction) Success() string {
return "Successfully formated to XFS"
}

func (a *FormatXfsAction) IsTrusted() bool {
return false
}

func (a *FormatXfsAction) GetDelay() time.Duration {
return DefaultDeviceActionDelay
func (a *FormatXfsAction) Warning() string {
return FormatActionWarning
}
47 changes: 28 additions & 19 deletions internal/action/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ func (a *LabelExt4Action) Execute(rc *utils.RunnerCache) error {
return nil
}

func (a *LabelExt4Action) GetDeviceName() string {
return a.deviceName
}

func (a *LabelExt4Action) IsTrusted() bool {
return false
}

func (a *LabelExt4Action) GetDelay() time.Duration {
return DefaultDeviceActionDelay
}

func (a *LabelExt4Action) Prompt() string {
return fmt.Sprintf("Would you like to label device %s to %s", a.deviceName, a.label)
}

func (a *LabelExt4Action) GetdeviceName() string {
return a.deviceName
}

func (a *LabelExt4Action) Refuse() string {
return fmt.Sprintf("Refused to label to %s", a.label)
Expand All @@ -47,12 +56,8 @@ func (a *LabelExt4Action) Success() string {
return fmt.Sprintf("Successfully labelled to %s", a.label)
}

func (a *LabelExt4Action) IsTrusted() bool {
return false
}

func (a *LabelExt4Action) GetDelay() time.Duration {
return DefaultDeviceActionDelay
func (a *LabelExt4Action) Warning() string {
return DisabledWarning
}

type LabelXfsAction struct {
Expand All @@ -79,12 +84,20 @@ func (a *LabelXfsAction) Execute(rc *utils.RunnerCache) error {
return nil
}

func (a *LabelXfsAction) Prompt() string {
return fmt.Sprintf("Would you like to label device %s to %s", a.deviceName, a.label)
func (a *LabelXfsAction) GetDeviceName() string {
return a.deviceName
}

func (a *LabelXfsAction) GetdeviceName() string {
return a.deviceName
func (a *LabelXfsAction) IsTrusted() bool {
return false
}

func (a *LabelXfsAction) GetDelay() time.Duration {
return DefaultDeviceActionDelay
}

func (a *LabelXfsAction) Prompt() string {
return fmt.Sprintf("Would you like to label device %s to %s", a.deviceName, a.label)
}

func (a *LabelXfsAction) Refuse() string {
Expand All @@ -95,10 +108,6 @@ func (a *LabelXfsAction) Success() string {
return fmt.Sprintf("Successfully labelled to %s", a.label)
}

func (a *LabelXfsAction) IsTrusted() bool {
return false
}

func (a *LabelXfsAction) GetDelay() time.Duration {
return DefaultDeviceActionDelay
func (a *LabelXfsAction) Warning() string {
return DisabledWarning
}
Loading

0 comments on commit dc0fc5a

Please sign in to comment.