Skip to content

Commit

Permalink
✨ feat :Add template flag to cyctl on module creation (#456)
Browse files Browse the repository at this point in the history
* Add template flag to cyctl on module creation

* Either template or (repo & path) as flag

* small fix

* small fix

* comment fix
  • Loading branch information
shivamrazorpay authored Jul 21, 2024
1 parent e1f3d08 commit 5af8e75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions cyctl/internal/create/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ var (
--path '/path/to/charts' \
--version 'main'
`
valuesFile string
valuesFile string
templateName string
)

// createModule allows you to create module Custom Resource.
func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, path, version, namespace, valuesFile string) {
func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, path, version, namespace, valuesFile, templateName string) {

values, err := os.ReadFile(valuesFile)
if err != nil {
Expand All @@ -37,6 +38,17 @@ func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, pat
log.Fatalf("Error converting values file to JSON: %v", err)
}

if templateName != "" && (repo == "" && path == "" && version == "") {
temp, err := clientset.TemplateStore("cyclops").Get(templateName)
if err != nil {
fmt.Printf("Error from server (Template NotFound): %v\n", err)
return
}
repo = temp.Spec.URL
path = temp.Spec.Path
version = temp.Spec.Version
}

// Define a new Module object
newModule := v1alpha1.Module{
TypeMeta: v1.TypeMeta{
Expand Down Expand Up @@ -74,7 +86,12 @@ var (
Args: cobra.ExactArgs(1),
Aliases: []string{"modules"},
Run: func(cmd *cobra.Command, args []string) {
createModule(kubeconfig.Moduleset, args[0], repo, path, version, namespace, valuesFile)
// Custom validation:
// Either templateName or (repo, path and version) must be provided, if one is provided the other must be empty
if (templateName != "" && (repo != "" || path != "" || version != "")) || (templateName == "" && (repo == "" || path == "" || version == "")) {
log.Fatalf("Error: Either template or (repo, path and version) must be provided.")
}
createModule(kubeconfig.Moduleset, args[0], repo, path, version, namespace, valuesFile, templateName)
},
}
)
Expand All @@ -85,7 +102,6 @@ func init() {
CreateModule.Flags().StringVarP(&path, "path", "p", "", "Path to the module charts")
CreateModule.Flags().StringVarP(&version, "version", "v", "", "Version of the module")
CreateModule.Flags().StringVarP(&valuesFile, "file", "f", "", "Path to the values.yaml file")
CreateModule.MarkFlagRequired("repo")
CreateModule.MarkFlagRequired("path")
CreateModule.Flags().StringVarP(&templateName, "template", "t", "", "Name of the template to use for the module creation")
CreateModule.MarkFlagRequired("file")
}
2 changes: 1 addition & 1 deletion cyctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

0 comments on commit 5af8e75

Please sign in to comment.