Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions cyctl/internal/create/modules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package create

import (
"encoding/json"
"fmt"
"log"
"os"
Expand All @@ -24,10 +25,11 @@ var (
`
valuesFile string
templateName string
outputFormat string
)

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

values, err := os.ReadFile(valuesFile)
if err != nil {
Expand Down Expand Up @@ -69,12 +71,35 @@ func createModule(clientset *client.CyclopsV1Alpha1Client, moduleName, repo, pat
},
}

module, err := clientset.Modules(namespace).Create(&newModule)
if err != nil {
fmt.Printf("Error creating template: %v\n", err)
return
if outputFormat == "yaml" {
// Marshal the newModule object to JSON
jsonOutput, err := json.Marshal(newModule)
if err != nil {
log.Fatalf("Error marshalling module to JSON: %v", err)
}
// Convert JSON to YAML
yamlOutput, err := yaml.JSONToYAML(jsonOutput)
if err != nil {
log.Fatalf("Error converting module to YAML: %v", err)
}
fmt.Printf("---\n%s\n", yamlOutput)
} else if outputFormat == "json" {
output, err := json.MarshalIndent(newModule, "", " ")
if err != nil {
log.Fatalf("Error converting module to JSON: %v", err)
}
fmt.Printf("%s\n", output)
} else if outputFormat == "" {
// Proceed with creation if no output format is specified
module, err := clientset.Modules(namespace).Create(&newModule)
if err != nil {
fmt.Printf("Error creating module: %v\n", err)
return
}
fmt.Printf("%v created successfully.\n", module.Name)
} else {
log.Fatalf("Invalid output format: %s. Supported formats are 'yaml' and 'json'.", outputFormat)
}
fmt.Printf("%v created successfully.\n", module.Name)
}

var (
Expand All @@ -91,7 +116,7 @@ var (
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)
createModule(kubeconfig.Moduleset, args[0], repo, path, version, namespace, valuesFile, templateName, outputFormat)
},
}
)
Expand All @@ -103,5 +128,6 @@ func init() {
CreateModule.Flags().StringVarP(&version, "version", "v", "", "Version of the module")
CreateModule.Flags().StringVarP(&valuesFile, "file", "f", "", "Path to the values.yaml file")
CreateModule.Flags().StringVarP(&templateName, "template", "t", "", "Name of the template to use for the module creation")
CreateModule.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format (yaml or json)")
CreateModule.MarkFlagRequired("file")
}
38 changes: 32 additions & 6 deletions cyctl/internal/create/template_auth_rules.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package create

import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
Expand All @@ -10,6 +12,7 @@ import (
"github.com/spf13/cobra"
v1Spec "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

var (
Expand Down Expand Up @@ -87,13 +90,35 @@ func createTemplateAuthRule(clientset *client.CyclopsV1Alpha1Client, templateAut
},
}

templateAuthRule, err := clientset.TemplateAuthRules(namespace).Create(&newTemplateAuthRule)
if err != nil {
fmt.Printf("Error creating templateauthrule: %v\n", err)
return
if outputFormat == "yaml" {
// Marshal the newTemplateAuthRule object to JSON
jsonOutput, err := json.Marshal(newTemplateAuthRule)
if err != nil {
log.Fatalf("Error marshalling templateauthrule to JSON: %v", err)
}
// Convert JSON to YAML
yamlOutput, err := yaml.JSONToYAML(jsonOutput)
if err != nil {
log.Fatalf("Error converting templateauthrule to YAML: %v", err)
}
fmt.Printf("---\n%s\n", yamlOutput)
} else if outputFormat == "json" {
output, err := json.MarshalIndent(newTemplateAuthRule, "", " ")
if err != nil {
log.Fatalf("Error converting templateauthrule to JSON: %v", err)
}
fmt.Printf("%s\n", output)
} else if outputFormat == "" {
// Proceed with creation if no output format is specified
templateAuthRule, err := clientset.TemplateAuthRules(namespace).Create(&newTemplateAuthRule)
if err != nil {
fmt.Printf("Error creating templateauthrule: %v\n", err)
return
}
fmt.Printf("%v created successfully.\n", templateAuthRule.Name)
} else {
log.Fatalf("Invalid output format: %s. Supported formats are 'yaml' and 'json'.", outputFormat)
}
fmt.Printf("%v created successfully.\n", templateAuthRule.Name)

}

var (
Expand All @@ -115,4 +140,5 @@ func init() {
CreateTemplateAuthRule.Flags().StringVarP(&username, "username", "u", "", "Username in the format 'name:key'")
CreateTemplateAuthRule.Flags().StringVarP(&password, "password", "p", "", "Password in the format 'name:key'")
CreateTemplateAuthRule.Flags().StringVarP(&namespace, "namespace", "n", "cyclops", "Namespace where the templateauthrule will be created")
CreateTemplateAuthRule.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format (yaml or json)")
}
46 changes: 36 additions & 10 deletions cyctl/internal/create/template_store.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package create

import (
"encoding/json"
"fmt"
"log"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"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"
"sigs.k8s.io/yaml"
)

var (
Expand All @@ -26,8 +29,7 @@ var (
version string
)

// DeleteTemplateAuthRule deletes a specified template auth rule from the TemplateAuthRule Custom Resource.
func createTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, version, namespace string) {
func createTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path, version, namespace, outputFormat string) {
// Define a new TemplateStore object
newTemplate := v1alpha1.TemplateStore{
TypeMeta: v1.TypeMeta{
Expand All @@ -45,24 +47,47 @@ func createTemplate(clientset *client.CyclopsV1Alpha1Client, templateName, path,
},
}

template, err := clientset.TemplateStore(namespace).Create(&newTemplate)
if err != nil {
fmt.Printf("Error creating template: %v\n", err)
return
if outputFormat == "yaml" {
// Marshal the newTemplate object to JSON
jsonOutput, err := json.Marshal(newTemplate)
if err != nil {
log.Fatalf("Error marshalling template to JSON: %v", err)
}
// Convert JSON to YAML
yamlOutput, err := yaml.JSONToYAML(jsonOutput)
if err != nil {
log.Fatalf("Error converting template to YAML: %v", err)
}
fmt.Printf("---\n%s\n", yamlOutput)
} else if outputFormat == "json" {
output, err := json.MarshalIndent(newTemplate, "", " ")
if err != nil {
log.Fatalf("Error converting template to JSON: %v", err)
}
fmt.Printf("%s\n", output)
} else if outputFormat == "" {
// Proceed with creation if no output format is specified
template, err := clientset.TemplateStore(namespace).Create(&newTemplate)
if err != nil {
fmt.Printf("Error creating template: %v\n", err)
return
}
fmt.Printf("%v created successfully.\n", template.Name)
} else {
log.Fatalf("Invalid output format: %s. Supported formats are 'yaml' and 'json'.", outputFormat)
}
fmt.Printf("%v created successfully.\n", template.Name)
}

var (
CreateTemplate = &cobra.Command{
Use: "templates NAME --repo=repo --path=path --version=version",
Use: "template NAME --repo=repo --path=path --version=version",
Short: "Create template",
Long: "The create template command allows you to create templatestore from the Cyclops API.",
Example: createTemplateExample,
Args: cobra.ExactArgs(1),
Aliases: []string{"template"},
Aliases: []string{"templates"},
Run: func(cmd *cobra.Command, args []string) {
createTemplate(kubeconfig.Moduleset, args[0], path, version, namespace)
createTemplate(kubeconfig.Moduleset, args[0], path, version, namespace, outputFormat)
},
}
)
Expand All @@ -72,4 +97,5 @@ func init() {
CreateTemplate.Flags().StringVarP(&path, "path", "p", "", "Path to the charts in the repository")
CreateTemplate.Flags().StringVarP(&version, "version", "v", "", "Version of the template")
CreateTemplate.Flags().StringVarP(&namespace, "namespace", "n", "cyclops", "Namespace where the template will be created")
CreateTemplate.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format (yaml|json)")
}