From 1af2aa33cb60a7585ca95296db6f173129e58c6e Mon Sep 17 00:00:00 2001 From: Abinand P Date: Fri, 4 Oct 2024 12:28:32 +0000 Subject: [PATCH 1/6] feature: updates - Added the subcommand which accepts one argument and updates the template store Signed-off-by: Abinand P --- cyctl/cmd/update.go | 1 + cyctl/internal/update/template_store.go | 94 +++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 cyctl/internal/update/template_store.go diff --git a/cyctl/cmd/update.go b/cyctl/cmd/update.go index b4924122..cfaca5e0 100644 --- a/cyctl/cmd/update.go +++ b/cyctl/cmd/update.go @@ -27,4 +27,5 @@ var ( func init() { RootCmd.AddCommand(updateCMD) updateCMD.AddCommand(update.UpdateModuleCMD) + updateCMD.AddCommand(update.UpdateTemplateStoreCMD) } diff --git a/cyctl/internal/update/template_store.go b/cyctl/internal/update/template_store.go new file mode 100644 index 00000000..cd8cd479 --- /dev/null +++ b/cyctl/internal/update/template_store.go @@ -0,0 +1,94 @@ +package update + +import ( + "fmt" + + "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client" + "github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig" + "github.com/spf13/cobra" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +) + +var ( + updateTemplateStoreExample = `# updates template store; takes template name as an argument with flag --value + + # Update template sample command + cyctl update template NAME --repo='https://github.com/cyclops-ui/templates' --path='/path' --version='main' --icon='icon'` +) + +var ( + repo string + path string + icon string + version string +) + +// updates he given template from cyclops API +func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, version, repo, icon string) { + // Fetch the existing template store + template, err := clientset.TemplateStore("cyclops").Get(templateName) + if err != nil { + fmt.Println("Failed to fetchtemplate store ", err) + return + } + + // Update the template store fields if provided + if repo != "" { + template.Spec.URL = repo + } + if path != "" { + template.Spec.Path = path + } + if version != "" { + template.Spec.Version = version + } + if icon != "" { + if template.ObjectMeta.Annotations == nil { + template.ObjectMeta.Annotations = make(map[string]string) + } + template.ObjectMeta.Annotations["cyclops-ui.com/icon"] = icon + } + template.TypeMeta = v1.TypeMeta{ + APIVersion: "cyclops-ui.com/v1alpha1", + Kind: "TemplateStore", + + } + + // Update the template store in the cluster + _ , err = clientset.TemplateStore("cyclops").Update(template) + if err != nil { + fmt.Println("Failed to update template store ", err) + return + } + + fmt.Printf("successfully updated %v", templateName) +} + +var ( + UpdateTemplateStoreCMD = &cobra.Command{ + Use: "template", + Short: " updates template values; takes template name as an argument with flags for updation", + Long: " updates template values; takes template name as an argument with flags --path= --repo= --version= --icon= ", + Example: updateTemplateStoreExample, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string){ + templateName := args[0] + + if repo == "" && path == "" && version == "" && icon == "" { + fmt.Println("Error: At least one of --repo, --path, --version, or --icon must be provided.") + cmd.Usage() // Print usage information + return + } + + updateTemplate(kubeconfig.Moduleset, templateName, path, version, repo, icon ) + }, + } +) + +func init() { + UpdateTemplateStoreCMD.Flags().StringVar(&repo, "repo", "", "Repository URL of the template store") + UpdateTemplateStoreCMD.Flags().StringVar(&path, "path", "", "Path to the charts in the repository") + UpdateTemplateStoreCMD.Flags().StringVar(&version, "version", "", "Version of the template store") + UpdateTemplateStoreCMD.Flags().StringVar(&icon, "icon", "", "Icon for the template store (stored in metadata.annotations.cyclops-ui.com/icon)") +} From 9c72bac949c2adfe167105df1c0de6d70e98eec9 Mon Sep 17 00:00:00 2001 From: Abinand P Date: Sun, 6 Oct 2024 10:39:28 +0000 Subject: [PATCH 2/6] fix: updated the suggested modfications from the review --- cyctl/internal/update/template_store.go | 37 ++++++++++++------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/cyctl/internal/update/template_store.go b/cyctl/internal/update/template_store.go index cd8cd479..07a3ad73 100644 --- a/cyctl/internal/update/template_store.go +++ b/cyctl/internal/update/template_store.go @@ -1,13 +1,13 @@ -package update +package update import ( "fmt" + "log" "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client" "github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig" "github.com/spf13/cobra" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - ) var ( @@ -18,10 +18,10 @@ var ( ) var ( - repo string - path string - icon string - version string + repo string + path string + icon string + version string ) // updates he given template from cyclops API @@ -29,7 +29,7 @@ func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, // Fetch the existing template store template, err := clientset.TemplateStore("cyclops").Get(templateName) if err != nil { - fmt.Println("Failed to fetchtemplate store ", err) + log.Fatal("Failed to fetch template store:", err.Error()) return } @@ -51,15 +51,14 @@ func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, } template.TypeMeta = v1.TypeMeta{ APIVersion: "cyclops-ui.com/v1alpha1", - Kind: "TemplateStore", - + Kind: "TemplateStore", } // Update the template store in the cluster - _ , err = clientset.TemplateStore("cyclops").Update(template) + _, err = clientset.TemplateStore("cyclops").Update(template) if err != nil { fmt.Println("Failed to update template store ", err) - return + return } fmt.Printf("successfully updated %v", templateName) @@ -67,21 +66,19 @@ func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, var ( UpdateTemplateStoreCMD = &cobra.Command{ - Use: "template", - Short: " updates template values; takes template name as an argument with flags for updation", - Long: " updates template values; takes template name as an argument with flags --path= --repo= --version= --icon= ", + Use: "template", + Short: " updates template values; takes template name as argument and updates values provided by flags", + Long: " updates template values; takes template name as argument with flags --path= --repo= --version= --icon= ", Example: updateTemplateStoreExample, - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string){ + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { templateName := args[0] if repo == "" && path == "" && version == "" && icon == "" { - fmt.Println("Error: At least one of --repo, --path, --version, or --icon must be provided.") - cmd.Usage() // Print usage information - return + log.Fatal("Error: At least on of --repo, --path, --version, or --icon must be provided.") } - updateTemplate(kubeconfig.Moduleset, templateName, path, version, repo, icon ) + updateTemplate(kubeconfig.Moduleset, templateName, path, version, repo, icon) }, } ) From 80ed29996f748a44c052f0269b50a682424312c8 Mon Sep 17 00:00:00 2001 From: Abinand P Date: Sun, 6 Oct 2024 16:32:07 +0530 Subject: [PATCH 3/6] Update cyctl/internal/update/template_store.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Petar Cvitanović <72022639+petar-cvit@users.noreply.github.com> --- cyctl/internal/update/template_store.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cyctl/internal/update/template_store.go b/cyctl/internal/update/template_store.go index 07a3ad73..973f9a36 100644 --- a/cyctl/internal/update/template_store.go +++ b/cyctl/internal/update/template_store.go @@ -67,8 +67,8 @@ func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, var ( UpdateTemplateStoreCMD = &cobra.Command{ Use: "template", - Short: " updates template values; takes template name as argument and updates values provided by flags", - Long: " updates template values; takes template name as argument with flags --path= --repo= --version= --icon= ", + Short: "updates template values; takes template name as argument and updates values provided by flags", + Long: "updates template values; takes template name as argument with flags --path= --repo= --version= --icon= ", Example: updateTemplateStoreExample, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { From 48d1d175bdaf67c57631cd464423def4b51c3c48 Mon Sep 17 00:00:00 2001 From: Abinand P Date: Sun, 6 Oct 2024 11:05:37 +0000 Subject: [PATCH 4/6] fix: updated the new line after the success message --- cyctl/internal/update/template_store.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cyctl/internal/update/template_store.go b/cyctl/internal/update/template_store.go index 973f9a36..775db681 100644 --- a/cyctl/internal/update/template_store.go +++ b/cyctl/internal/update/template_store.go @@ -61,7 +61,8 @@ func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, return } - fmt.Printf("successfully updated %v", templateName) + fmt.Printf("successfully updated %v \n", templateName) + } var ( From 34dba956b310b8b77ede5436c0f3e154c7d19588 Mon Sep 17 00:00:00 2001 From: Abinand P Date: Thu, 10 Oct 2024 15:49:28 +0530 Subject: [PATCH 5/6] Update cyctl/internal/update/template_store.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Petar Cvitanović <72022639+petar-cvit@users.noreply.github.com> --- cyctl/internal/update/template_store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cyctl/internal/update/template_store.go b/cyctl/internal/update/template_store.go index 775db681..4e1b47e6 100644 --- a/cyctl/internal/update/template_store.go +++ b/cyctl/internal/update/template_store.go @@ -11,7 +11,7 @@ import ( ) var ( - updateTemplateStoreExample = `# updates template store; takes template name as an argument with flag --value + updateTemplateStoreExample = `# updates template store # Update template sample command cyctl update template NAME --repo='https://github.com/cyclops-ui/templates' --path='/path' --version='main' --icon='icon'` From 8e50fc81661aced8db98958c15b7ed77696830dc Mon Sep 17 00:00:00 2001 From: Abinand P Date: Thu, 10 Oct 2024 15:52:07 +0530 Subject: [PATCH 6/6] updated the template store to support the command updates [alias] --- cyctl/internal/update/template_store.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cyctl/internal/update/template_store.go b/cyctl/internal/update/template_store.go index 4e1b47e6..06bab968 100644 --- a/cyctl/internal/update/template_store.go +++ b/cyctl/internal/update/template_store.go @@ -71,6 +71,7 @@ var ( Short: "updates template values; takes template name as argument and updates values provided by flags", Long: "updates template values; takes template name as argument with flags --path= --repo= --version= --icon= ", Example: updateTemplateStoreExample, + Aliases: []string{"templates"}, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { templateName := args[0]