Skip to content

Commit

Permalink
refactor: split ConfigureOrCheckCommand into multiple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-ibra committed Sep 11, 2024
1 parent b0976db commit df6138b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cmd/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ For more information about validator, see: https://github.com/validator-labs/val
if err := c.Save(""); err != nil {
return err
}
if err := validator.ConfigureOrCheckCommand(c, tc); err != nil {
if err := validator.ConfigureCommand(c, tc); err != nil {
return fmt.Errorf("failed to configure and apply validator rules: %w", err)
}
return nil
Expand Down Expand Up @@ -197,7 +197,7 @@ For more information about validator, see: https://github.com/validator-labs/val
if err := c.Save(""); err != nil {
return err
}
if err := validator.ConfigureOrCheckCommand(c, tc); err != nil {
if err := validator.CheckCommand(c, tc); err != nil {
if errors.Is(err, validator.ErrValidationFailed{}) {
cmd.SilenceUsage = true
}
Expand Down
111 changes: 68 additions & 43 deletions pkg/cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func InstallValidatorCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
if !configProvided {
tc.Reconfigure = true
}
if err := ConfigureOrCheckCommand(c, tc); err != nil {
if err := ConfigureCommand(c, tc); err != nil {
return err
}
}
Expand All @@ -178,7 +178,7 @@ func InstallValidatorCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
if !configProvided {
tc.Reconfigure = true
}
if err := ConfigureOrCheckCommand(c, tc); err != nil {
if err := ConfigureCommand(c, tc); err != nil {
return err
}
}
Expand All @@ -195,14 +195,43 @@ func InstallValidatorCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
return nil
}

// ConfigureOrCheckCommand configures and applies/executes validator plugin rules
// nolint:dupl
func ConfigureOrCheckCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
var vc *components.ValidatorConfig
var err error
var saveConfig bool
// ConfigureCommand configures and applies/executes validator plugin rules
func ConfigureCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
vc, err := configureValidatorConfig(c, tc)
if err != nil {
return err
}

if tc.CreateConfigOnly || tc.UpdatePasswords {
return nil
}

ok, invalidPlugins := vc.EnabledPluginsHaveRules()
if !ok {
log.FatalCLI("invalid validator configuration", "error",
fmt.Sprintf("the following plugins are enabled, but have no rules configured: %v", invalidPlugins),
)
}

// upgrade the validator helm release so that plugin rule secrets
// are created, e.g., OCI registry secrets, Network basic auth secrets, etc.
if err := applyValidator(c, vc); err != nil {
return err
}
if err := configurePlugins(c, vc, tc); err != nil {
return err
}
if tc.Wait {
log.Header("Waiting for validation to complete")
_, err := WatchValidationResults(tc)
return err
}
return nil
}

if tc.CustomResources != "" && tc.Direct {
// CheckCommand configures and applies/executes validator plugin rules
func CheckCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
if tc.CustomResources != "" {
pluginSpecs, err := readPluginSpecs(tc.CustomResources)
if err != nil {
return err
Expand All @@ -216,16 +245,40 @@ func ConfigureOrCheckCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
return executePlugins(c, pluginSpecs, nil)
}

vc, err := configureValidatorConfig(c, tc)
if err != nil {
return err
}

if tc.CreateConfigOnly || tc.UpdatePasswords {
return nil
}

ok, invalidPlugins := vc.EnabledPluginsHaveRules()
if !ok {
log.FatalCLI("invalid validator configuration", "error",
fmt.Sprintf("the following plugins are enabled, but have no rules configured: %v", invalidPlugins),
)
}

return executePlugins(c, toPluginSpecs(vc), vc.SinkConfig)
}

func configureValidatorConfig(c *cfg.Config, tc *cfg.TaskConfig) (*components.ValidatorConfig, error) {
var vc *components.ValidatorConfig
var err error
var saveConfig bool

if !tc.Reconfigure {
// Silent Mode
vc, err = components.NewValidatorFromConfig(tc)
if err != nil {
return errors.Wrap(err, "failed to load validator configuration file")
return nil, errors.Wrap(err, "failed to load validator configuration file")
}
if tc.UpdatePasswords {
log.Header("Updating plugin credentials in validator configuration file")
if err := validator.UpdateValidatorPluginCredentials(vc, tc); err != nil {
return err
return nil, err
}
saveConfig = true
}
Expand All @@ -237,53 +290,25 @@ func ConfigureOrCheckCommand(c *cfg.Config, tc *cfg.TaskConfig) error {
} else {
vc, err = components.NewValidatorFromConfig(tc)
if err != nil {
return errors.Wrap(err, "failed to load validator configuration file")
return nil, errors.Wrap(err, "failed to load validator configuration file")
}
}
if err := validator.ReadValidatorPluginConfig(c, tc, vc); err != nil {
return errors.Wrap(err, "failed to configure validator plugin(s)")
return nil, errors.Wrap(err, "failed to configure validator plugin(s)")
}
saveConfig = true
}

// save / print validator config file
if saveConfig {
if err := components.SaveValidatorConfig(vc, tc); err != nil {
return err
return nil, err
}
} else {
log.InfoCLI("validator configuration file: %s", tc.ConfigFile)
}

if tc.CreateConfigOnly || tc.UpdatePasswords {
return nil
}

ok, invalidPlugins := vc.EnabledPluginsHaveRules()
if !ok {
log.FatalCLI("invalid validator configuration", "error",
fmt.Sprintf("the following plugins are enabled, but have no rules configured: %v", invalidPlugins),
)
}

if tc.Direct {
return executePlugins(c, toPluginSpecs(vc), vc.SinkConfig)
}

// upgrade the validator helm release so that plugin rule secrets
// are created, e.g., OCI registry secrets, Network basic auth secrets, etc.
if err := applyValidator(c, vc); err != nil {
return err
}
if err := configurePlugins(c, vc, tc); err != nil {
return err
}
if tc.Wait {
log.Header("Waiting for validation to complete")
_, err := WatchValidationResults(tc)
return err
}
return nil
return vc, nil
}

func readPluginSpecs(path string) ([]plugins.PluginSpec, error) {
Expand Down

0 comments on commit df6138b

Please sign in to comment.