Skip to content

Commit 4dacc0c

Browse files
🎯 support updating template store (#607)
* 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]>
1 parent a15981e commit 4dacc0c

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

cyctl/cmd/update.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ var (
2727
func init() {
2828
RootCmd.AddCommand(updateCMD)
2929
updateCMD.AddCommand(update.UpdateModuleCMD)
30+
updateCMD.AddCommand(update.UpdateTemplateStoreCMD)
3031
}
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package update
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
8+
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
9+
"github.com/spf13/cobra"
10+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
)
12+
13+
var (
14+
updateTemplateStoreExample = `# updates template store
15+
16+
# Update template sample command
17+
cyctl update template NAME --repo='https://github.com/cyclops-ui/templates' --path='/path' --version='main' --icon='icon'`
18+
)
19+
20+
var (
21+
repo string
22+
path string
23+
icon string
24+
version string
25+
)
26+
27+
// updates he given template from cyclops API
28+
func updateTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, version, repo, icon string) {
29+
// Fetch the existing template store
30+
template, err := clientset.TemplateStore("cyclops").Get(templateName)
31+
if err != nil {
32+
log.Fatal("Failed to fetch template store:", err.Error())
33+
return
34+
}
35+
36+
// Update the template store fields if provided
37+
if repo != "" {
38+
template.Spec.URL = repo
39+
}
40+
if path != "" {
41+
template.Spec.Path = path
42+
}
43+
if version != "" {
44+
template.Spec.Version = version
45+
}
46+
if icon != "" {
47+
if template.ObjectMeta.Annotations == nil {
48+
template.ObjectMeta.Annotations = make(map[string]string)
49+
}
50+
template.ObjectMeta.Annotations["cyclops-ui.com/icon"] = icon
51+
}
52+
template.TypeMeta = v1.TypeMeta{
53+
APIVersion: "cyclops-ui.com/v1alpha1",
54+
Kind: "TemplateStore",
55+
}
56+
57+
// Update the template store in the cluster
58+
_, err = clientset.TemplateStore("cyclops").Update(template)
59+
if err != nil {
60+
fmt.Println("Failed to update template store ", err)
61+
return
62+
}
63+
64+
fmt.Printf("successfully updated %v \n", templateName)
65+
66+
}
67+
68+
var (
69+
UpdateTemplateStoreCMD = &cobra.Command{
70+
Use: "template",
71+
Short: "updates template values; takes template name as argument and updates values provided by flags",
72+
Long: "updates template values; takes template name as argument with flags --path=<path> --repo=<repo> --version=<version> --icon=<icon> ",
73+
Example: updateTemplateStoreExample,
74+
Aliases: []string{"templates"},
75+
Args: cobra.ExactArgs(1),
76+
Run: func(cmd *cobra.Command, args []string) {
77+
templateName := args[0]
78+
79+
if repo == "" && path == "" && version == "" && icon == "" {
80+
log.Fatal("Error: At least on of --repo, --path, --version, or --icon must be provided.")
81+
}
82+
83+
updateTemplate(kubeconfig.Moduleset, templateName, path, version, repo, icon)
84+
},
85+
}
86+
)
87+
88+
func init() {
89+
UpdateTemplateStoreCMD.Flags().StringVar(&repo, "repo", "", "Repository URL of the template store")
90+
UpdateTemplateStoreCMD.Flags().StringVar(&path, "path", "", "Path to the charts in the repository")
91+
UpdateTemplateStoreCMD.Flags().StringVar(&version, "version", "", "Version of the template store")
92+
UpdateTemplateStoreCMD.Flags().StringVar(&icon, "icon", "", "Icon for the template store (stored in metadata.annotations.cyclops-ui.com/icon)")
93+
}

0 commit comments

Comments
 (0)