From bcdce02b7815532f3ef0c09ad4e973dd40cc9e36 Mon Sep 17 00:00:00 2001
From: Stefan Prodan <stefan.prodan@gmail.com>
Date: Fri, 15 Jan 2021 13:18:04 +0200
Subject: [PATCH] Add image tags regex filter arg to policy command

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
---
 cmd/flux/create_image_policy.go      | 12 ++++++++++--
 docs/cmd/flux_create_image_policy.md |  7 ++++---
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/cmd/flux/create_image_policy.go b/cmd/flux/create_image_policy.go
index 1c77113c..287e3dcd 100644
--- a/cmd/flux/create_image_policy.go
+++ b/cmd/flux/create_image_policy.go
@@ -38,8 +38,9 @@ the status of the object.`,
 	RunE: createImagePolicyRun}
 
 type imagePolicyFlags struct {
-	imageRef string
-	semver   string
+	imageRef    string
+	semver      string
+	filterRegex string
 }
 
 var imagePolicyArgs = imagePolicyFlags{}
@@ -48,6 +49,7 @@ func init() {
 	flags := createImagePolicyCmd.Flags()
 	flags.StringVar(&imagePolicyArgs.imageRef, "image-ref", "", "the name of an image repository object")
 	flags.StringVar(&imagePolicyArgs.semver, "semver", "", "a semver range to apply to tags; e.g., '1.x'")
+	flags.StringVar(&imagePolicyArgs.filterRegex, "filter-regex", "", " regular expression pattern used to filter the image tags")
 
 	createImageCmd.AddCommand(createImagePolicyCmd)
 }
@@ -95,6 +97,12 @@ func createImagePolicyRun(cmd *cobra.Command, args []string) error {
 		return fmt.Errorf("a policy must be provided with --semver")
 	}
 
+	if imagePolicyArgs.filterRegex != "" {
+		policy.Spec.FilterTags = &imagev1.TagFilter{
+			Pattern: imagePolicyArgs.filterRegex,
+		}
+	}
+
 	if export {
 		return printExport(exportImagePolicy(&policy))
 	}
diff --git a/docs/cmd/flux_create_image_policy.md b/docs/cmd/flux_create_image_policy.md
index c311ed16..5a9db5f6 100644
--- a/docs/cmd/flux_create_image_policy.md
+++ b/docs/cmd/flux_create_image_policy.md
@@ -18,9 +18,10 @@ flux create image policy <name> [flags]
 ### Options
 
 ```
-  -h, --help               help for policy
-      --image-ref string   the name of an image repository object
-      --semver string      a semver range to apply to tags; e.g., '1.x'
+      --filter-regex string    regular expression pattern used to filter the image tags
+  -h, --help                  help for policy
+      --image-ref string      the name of an image repository object
+      --semver string         a semver range to apply to tags; e.g., '1.x'
 ```
 
 ### Options inherited from parent commands
-- 
GitLab