Skip to content

Commit 03cb423

Browse files
committed
--deployment-strategy: add custom tyhpe
1 parent 66f07b4 commit 03cb423

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

pkg/koyeb/services.go

+36-38
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ func (h *ServiceHandler) addServiceDefinitionFlagsForAllSources(flags *pflag.Fla
318318
"To delete an environment variable, prefix its name with '!', for example --env '!FOO'\n",
319319
)
320320
flags.String("instance-type", "nano", "Instance type")
321-
flags.String("deployment-strategy", "rolling", "Deployment strategy")
321+
322+
var strategy DeploymentStrategy
323+
flags.Var(&strategy, "deployment-strategy", "Deployment strategy")
324+
322325
flags.Int64("scale", 1, "Set both min-scale and max-scale")
323326
flags.Int64("min-scale", 1, "Min scale")
324327
flags.Int64("max-scale", 1, "Max scale")
@@ -594,45 +597,13 @@ func (h *ServiceHandler) parseInstanceType(flags *pflag.FlagSet, currentInstance
594597
// Parse --deployment-strategy
595598
func (h *ServiceHandler) parseDeploymentStrategy(flags *pflag.FlagSet, currentStrategy koyeb.DeploymentStrategy) (koyeb.DeploymentStrategy, error) {
596599
if !flags.Lookup("deployment-strategy").Changed {
597-
// New service: return the default value
598-
if currentStrategy.Type == nil {
599-
value := flags.Lookup("deployment-strategy").DefValue
600-
enum := fmt.Sprintf("DEPLOYMENT_STRATEGY_TYPE_%s", strings.ToUpper(strings.ReplaceAll(value, "-", "_")))
601-
kind, err := koyeb.NewDeploymentStrategyTypeFromValue(enum)
602-
if err != nil {
603-
panic(err)
604-
}
605-
ret := koyeb.DeploymentStrategy{
606-
Type: kind,
607-
}
608-
return ret, nil
609-
}
610-
// Existing service: return the strategy currently configured
611600
return currentStrategy, nil
612601
}
613-
614-
value, _ := flags.GetString("deployment-strategy")
615-
enum := fmt.Sprintf("DEPLOYMENT_STRATEGY_TYPE_%s", strings.ToUpper(strings.ReplaceAll(value, "-", "_")))
616-
kind, err := koyeb.NewDeploymentStrategyTypeFromValue(enum)
617-
if err != nil {
618-
return koyeb.DeploymentStrategy{}, &errors.CLIError{
619-
What: "Error while updating the service",
620-
Why: "the --deployment-strategy flag is not valid",
621-
Additional: []string{
622-
"The --deployment-strategy flag must be one of the following:",
623-
" - rolling",
624-
" - canary",
625-
" - blue-green",
626-
" - immediate",
627-
},
628-
Orig: nil,
629-
Solution: "Fix the --deployment-strategy flag and try again",
630-
}
631-
}
632-
ret := koyeb.DeploymentStrategy{
633-
Type: kind,
634-
}
635-
return ret, nil
602+
flagValue := flags.Lookup("deployment-strategy").Value.(*DeploymentStrategy)
603+
strategy := koyeb.DeploymentStrategyType(*flagValue)
604+
return koyeb.DeploymentStrategy{
605+
Type: &strategy,
606+
}, nil
636607
}
637608

638609
// parseListFlags is the generic function parsing --env, --port, --routes, --checks, --regions and --volumes
@@ -1753,3 +1724,30 @@ func (h *ServiceHandler) parseVolumes(ctx *CLIContext, flags *pflag.FlagSet, cur
17531724

17541725
return parseListFlags("volumes", flags_list.GetNewVolumeListFromFlags(wrappedResolveVolumeId), flags, currentVolumes)
17551726
}
1727+
1728+
// DeploymentStrategy is a type alias for koyeb.DeploymentStrategyType which implements the pflag.Value interface.
1729+
type DeploymentStrategy koyeb.DeploymentStrategyType
1730+
1731+
func (s *DeploymentStrategy) String() string {
1732+
return string(*s)
1733+
}
1734+
1735+
func (s *DeploymentStrategy) Set(value string) error {
1736+
switch value {
1737+
case "rolling":
1738+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_ROLLING)
1739+
case "canary":
1740+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_CANARY)
1741+
case "blue-green":
1742+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_BLUE_GREEN)
1743+
case "immediate":
1744+
*s = DeploymentStrategy(koyeb.DEPLOYMENTSTRATEGYTYPE_IMMEDIATE)
1745+
default:
1746+
return fmt.Errorf("invalid deployment strategy: %s. Valid values are: rolling, canary, blue-green, immediate.", value)
1747+
}
1748+
return nil
1749+
}
1750+
1751+
func (s *DeploymentStrategy) Type() string {
1752+
return "deployment-strategy"
1753+
}

0 commit comments

Comments
 (0)