|
| 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