Skip to content

Commit

Permalink
🎯 support updating template store (#607)
Browse files Browse the repository at this point in the history
* feature: updates - Added the subcommand which accepts one argument and updates the template store

Signed-off-by: Abinand P <[email protected]>

* fix: updated the suggested modfications from the review

* Update cyctl/internal/update/template_store.go

Co-authored-by: Petar Cvitanović <[email protected]>

* fix: updated the new line after the success message

* Update cyctl/internal/update/template_store.go

Co-authored-by: Petar Cvitanović <[email protected]>

* updated the template store to support the command updates [alias]

---------

Signed-off-by: Abinand P <[email protected]>
Co-authored-by: Petar Cvitanović <[email protected]>
  • Loading branch information
Abiji-2020 and petar-cvit authored Oct 13, 2024
1 parent a15981e commit 4dacc0c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions cyctl/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ var (
func init() {
RootCmd.AddCommand(updateCMD)
updateCMD.AddCommand(update.UpdateModuleCMD)
updateCMD.AddCommand(update.UpdateTemplateStoreCMD)
}
93 changes: 93 additions & 0 deletions cyctl/internal/update/template_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 (
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'`
)

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 {
log.Fatal("Failed to fetch template store:", err.Error())
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 \n", templateName)

}

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=<path> --repo=<repo> --version=<version> --icon=<icon> ",
Example: updateTemplateStoreExample,
Aliases: []string{"templates"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateName := args[0]

if repo == "" && path == "" && version == "" && icon == "" {
log.Fatal("Error: At least on of --repo, --path, --version, or --icon must be provided.")
}

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)")
}

0 comments on commit 4dacc0c

Please sign in to comment.