Skip to content

Commit

Permalink
chore: add validation to flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Pragliola committed Jul 18, 2024
1 parent 8fda680 commit b1c8dd5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ type ClusterCmdFlags struct {
ClusterSkipsCmdFlags
}

var ErrDownloadDependenciesFailed = errors.New("dependencies download failed")
var (
ErrDownloadDependenciesFailed = errors.New("dependencies download failed")
ErrPhaseInvalid = errors.New("phase is not valid")
)

func NewApplyCmd() *cobra.Command {
var cmdEvent analytics.Event
Expand Down Expand Up @@ -330,6 +333,12 @@ func getCreateClusterCmdFlags() (ClusterCmdFlags, error) {
)
}

postApplyPhases := viper.GetStringSlice("post-apply-phases")

if err := validatePostApplyPhasesFlag(postApplyPhases); err != nil {
return ClusterCmdFlags{}, fmt.Errorf("%w: %s %w", ErrParsingFlag, "post-apply-phases", err)
}

return ClusterCmdFlags{
Debug: viper.GetBool("debug"),
FuryctlPath: viper.GetString("config"),
Expand All @@ -352,10 +361,20 @@ func getCreateClusterCmdFlags() (ClusterCmdFlags, error) {
UpgradeNode: upgradeNode,
DistroPatchesLocation: viper.GetString("distro-patches"),
ClusterSkipsCmdFlags: skips,
PostApplyPhases: viper.GetStringSlice("post-apply-phases"),
PostApplyPhases: postApplyPhases,
}, nil
}

func validatePostApplyPhasesFlag(phases []string) error {
for _, phase := range phases {
if err := cluster.ValidateMainPhases(phase); err != nil {
return fmt.Errorf("%w: %s", ErrPhaseInvalid, phase)
}
}

return nil
}

func setupCreateClusterCmdFlags(cmd *cobra.Command) {
cmd.Flags().StringP(
"config",
Expand Down
13 changes: 13 additions & 0 deletions internal/cluster/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ func ValidateOperationPhase(phase string) error {
}
}

func ValidateMainPhases(phase string) error {
switch phase {
case OperationPhaseInfrastructure,
OperationPhaseKubernetes,
OperationPhaseDistribution,
OperationPhasePlugins:
return nil

default:
return ErrUnsupportedPhase
}
}

func GetPhasesOrder() []string {
return []string{
"PreInfrastructure",
Expand Down

0 comments on commit b1c8dd5

Please sign in to comment.