From 9d70e09a57c96739d45f77dc83729bd51a7266ef Mon Sep 17 00:00:00 2001
From: Hidde Beydals <hello@hidde.co>
Date: Fri, 12 Feb 2021 15:37:25 +0100
Subject: [PATCH] Add numeric selector to create image policy cmd

Signed-off-by: Hidde Beydals <hello@hidde.co>
---
 cmd/flux/create_image_policy.go      | 20 +++++++++++++++-----
 docs/cmd/flux_create_image_policy.md |  1 +
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/cmd/flux/create_image_policy.go b/cmd/flux/create_image_policy.go
index 777fcdad..13fd35d0 100644
--- a/cmd/flux/create_image_policy.go
+++ b/cmd/flux/create_image_policy.go
@@ -43,11 +43,13 @@ the status of the object.`,
 	RunE: createImagePolicyRun}
 
 type imagePolicyFlags struct {
-	imageRef      string
-	semver        string
-	alpha         string
-	filterRegex   string
-	filterExtract string
+	imageRef        string
+	semver          string
+	alpha           string
+	numeric         string
+	filterRegex     string
+	filterExtract   string
+	filterNumerical string
 }
 
 var imagePolicyArgs = imagePolicyFlags{}
@@ -57,6 +59,7 @@ func init() {
 	flags.StringVar(&imagePolicyArgs.imageRef, "image-ref", "", "the name of an image repository object")
 	flags.StringVar(&imagePolicyArgs.semver, "select-semver", "", "a semver range to apply to tags; e.g., '1.x'")
 	flags.StringVar(&imagePolicyArgs.alpha, "select-alpha", "", "use alphabetical sorting to select image; either \"asc\" meaning select the last, or \"desc\" meaning select the first")
+	flags.StringVar(&imagePolicyArgs.numeric, "select-numeric", "", "use numeric sorting to select image; either \"asc\" meaning select the last, or \"desc\" meaning select the first")
 	flags.StringVar(&imagePolicyArgs.filterRegex, "filter-regex", "", "regular expression pattern used to filter the image tags")
 	flags.StringVar(&imagePolicyArgs.filterExtract, "filter-extract", "", "replacement pattern (using capture groups from --filter-regex) to use for sorting")
 
@@ -111,6 +114,13 @@ func createImagePolicyRun(cmd *cobra.Command, args []string) error {
 		policy.Spec.Policy.Alphabetical = &imagev1.AlphabeticalPolicy{
 			Order: imagePolicyArgs.alpha,
 		}
+	case imagePolicyArgs.numeric != "":
+		if imagePolicyArgs.numeric != "desc" && imagePolicyArgs.numeric != "asc" {
+			return fmt.Errorf("--select-numeric must be one of [\"asc\", \"desc\"]")
+		}
+		policy.Spec.Policy.Numerical = &imagev1.NumericalPolicy{
+			Order: imagePolicyArgs.numeric,
+		}
 	default:
 		return fmt.Errorf("a policy must be provided with either --select-semver or --select-alpha")
 	}
diff --git a/docs/cmd/flux_create_image_policy.md b/docs/cmd/flux_create_image_policy.md
index 9733f86f..6790ca78 100644
--- a/docs/cmd/flux_create_image_policy.md
+++ b/docs/cmd/flux_create_image_policy.md
@@ -23,6 +23,7 @@ flux create image policy <name> [flags]
   -h, --help                    help for policy
       --image-ref string        the name of an image repository object
       --select-alpha string     use alphabetical sorting to select image; either "asc" meaning select the last, or "desc" meaning select the first
+      --select-numeric string   use numeric sorting to select image; either "asc" meaning select the last, or "desc" meaning select the first
       --select-semver string    a semver range to apply to tags; e.g., '1.x'
 ```
 
-- 
GitLab