Skip to content

Commit

Permalink
(feat): Simplify mode selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 2, 2023
1 parent 6c17911 commit 0af9001
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
4 changes: 1 addition & 3 deletions configs/ubuntu.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
defaults:
mode: healthcheck
devices:
/dev/vdb:
fs: xfs
label: lasith-rules
mountPoint: /ifmx/tempp
mountPoint: /ifmx/temp
mountOptions: defaults
group: ubuntu
user: ubuntu
Expand Down
6 changes: 1 addition & 5 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ func NewActionExecutor(rc utils.RunnerFactory, c *config.Config) *ActionExecutor

func (ae *ActionExecutor) ExecuteAction(action Action) error {
name := action.GetDeviceName()
mode, err := ae.config.GetMode(name)
if err != nil {
return err
}
switch mode {
switch ae.config.GetMode(name) {
case config.Prompt:
if !ae.ShouldProceed(action) {
return fmt.Errorf("🔴 Action rejected. %s", action.Refuse())
Expand Down
26 changes: 16 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ type Overrides struct {
ResizeFs bool `yaml:"resizeFs"`
}

// We don't export "overrides" as this is an attribute that is used
// internally to store the state of flag overrides
type Config struct {
Defaults Defaults `yaml:"defaults"`
Overrides Overrides `yaml:"overrides"`
Devices map[string]Device `yaml:"devices"`
overrides Overrides
}

func Parse(args []string) (*Config, *Flag, error) {
Expand Down Expand Up @@ -128,35 +130,39 @@ func parseFlags(program string, args []string) (*Flag, error) {
return flag, nil
}

func (c *Config) GetMode(name string) (Mode, error) {
func (c *Config) GetMode(name string) Mode {
cd, found := c.Devices[name]
if !found {
return Empty, fmt.Errorf("🔴 %s: Could not find device in config", name)
return Healtcheck
}
if c.Overrides.Mode != Empty {
return c.Overrides.Mode, nil
// Overrides handler
if c.overrides.Mode != Empty {
return c.overrides.Mode
}
// Defaults handler
if cd.Mode == Empty && c.Defaults.Mode != Empty {
return c.Defaults.Mode, nil
return c.Defaults.Mode
}
// Device handler
if cd.Mode != Empty {
return cd.Mode, nil
return cd.Mode
}
return Empty, fmt.Errorf("🔴 %s: Ensure that you have provided a supported mode locally or globally", name)
// Fallback
return Healtcheck
}

func (c *Config) GetRemount(name string) bool {
cd, found := c.Devices[name]
if !found {
return false
}
return c.Overrides.Remount || c.Defaults.Remount || cd.Remount
return c.overrides.Remount || c.Defaults.Remount || cd.Remount
}

func (c *Config) GetResizeFs(name string) bool {
cd, found := c.Devices[name]
if !found {
return false
}
return c.Overrides.ResizeFs || c.Defaults.ResizeFs || cd.ResizeFs
return c.overrides.ResizeFs || c.Defaults.ResizeFs || cd.ResizeFs
}
6 changes: 3 additions & 3 deletions internal/config/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (om *OverridesModifier) Modify(c *Config) error {
if err != nil {
return err
}
c.Overrides.Mode = mode
c.Overrides.Remount = om.flag.Remount
c.Overrides.ResizeFs = om.flag.ResizeFs
c.overrides.Mode = mode
c.overrides.Remount = om.flag.Remount
c.overrides.ResizeFs = om.flag.ResizeFs
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (fsv *ModeValidator) Validate(c *Config) error {
return fmt.Errorf("🔴 %s is not a supported global mode", mode)
}

mode = string(c.Overrides.Mode)
mode = string(c.overrides.Mode)
_, err = ParseMode(mode)
if err != nil {
return fmt.Errorf("🔴 %s is not a supported overrides mode", mode)
Expand Down

0 comments on commit 0af9001

Please sign in to comment.