Skip to content

Commit

Permalink
🔍 implementation of describe command for the custom resources (cyclop…
Browse files Browse the repository at this point in the history
…s-ui#292)

* ft(cyctl): Describe module functionality

* ft(cyctl): Describe templateauthrule functionality

* ft(cyctl): Describe templatestore functionality

* ft(cyctl): added describe command to define resources

* ft(cyctl): base64 encoder and decoder

* ft(cyctl): describer utility for resources defination

* ft(cyctl): updated dependencies

* ft(cyctl): removed line spacing

* ft(cyctl): return within the error handling

* ft(cyctl): removed base64 encoding

* ft(cyctl): removed base64 methods

* ft(cyctl): added indentation and removed printing on nextline annotation

* ft(cyctl): refactored to single describe metadata func

* ft(cyctl): typo removed
  • Loading branch information
siddhantprateek authored May 25, 2024
1 parent 8a97d03 commit 0e68c2c
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 1 deletion.
34 changes: 34 additions & 0 deletions cyctl/cmd/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/cyclops-ui/cycops-cyctl/internal/describe"
"github.com/spf13/cobra"
)

var (
describeExample = `
# Describe one or more modules
cyctl describe modules [module_name]
# Describe one or more templates
cyctl describe templates [template_name]
# Describe one or more templateauthrules.
cyctl describe templateauthrules [templateauthrules_name]`
)

var describeCMD = &cobra.Command{
Use: "describe",
Short: "Describe custom resources like modules, templates, and templateauthrules",
Long: "Describe custom resources like modules, templates, and templateauthrules",
Example: describeExample,
Args: cobra.MinimumNArgs(1),
}

func init() {
describeCMD.AddCommand(describe.DescribeModule)
describeCMD.AddCommand(describe.DescribeTemplateAuthRule)
describeCMD.AddCommand(describe.DescribeTemplate)

RootCmd.AddCommand(describeCMD)
}
2 changes: 1 addition & 1 deletion cyctl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/spf13/cobra v1.8.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -40,5 +41,4 @@ require (
sigs.k8s.io/controller-runtime v0.15.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
86 changes: 86 additions & 0 deletions cyctl/internal/describe/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package describe

import (
"fmt"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/cyclops-ui/cycops-cyctl/utility"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
describeModuleExample = `
# Describe a single module
cyctl describe modules module1
# Describe multiple modules
cyctl describe modules module1 module2 module3`
)

// describeModules describe specified module from the Cyclops API.
func describeModules(clientset *client.CyclopsV1Alpha1Client, moduleNames []string) {
if len(moduleNames) == 0 {
modules, err := clientset.Modules("cyclops").List(v1.ListOptions{})
if err != nil {
fmt.Printf("Error fetching modules: %v\n", err)
return
}
for _, module := range modules {
moduleNames = append(moduleNames, module.Name)
}
}

for _, moduleName := range moduleNames {
module, err := clientset.Modules("cyclops").Get(moduleName)
if err != nil {
fmt.Printf("Error from server (NotFound): %v\n", err)
return
}
// Describe the module
describe := utility.Describe(func(d *utility.Describer) {
d.DescribeMetaData(module.ObjectMeta)

d.Printf("Creation:\t%s\n", module.CreationTimestamp)

d.Printf("Status:\t%s\n", module.Status.ReconciliationStatus.Status)

d.Println()
d.Printf("Template:\t\n")
d.Printf(" Repository:\t%s\n", module.Spec.TemplateRef.URL)
d.Printf(" Relative Path:\t%s\n", module.Spec.TemplateRef.Path)
d.Printf(" Branch:\t%s\n", module.Spec.TemplateRef.Version)

d.Println()
if len(module.Spec.Values.Raw) > 0 {
yamlData, err := utility.JsonToYAMLDescriber(module.Spec.Values.Raw)
if err != nil {
d.Printf("Values:\n%s\n", module.Spec.Values.Raw)
} else {
d.Printf("Values:\n%s\n", yamlData)
}
} else {
d.Printf("Values:\n<none>\n")
}
})

fmt.Printf("%s", describe)
fmt.Println("-------------------------------")

}
}

var (
DescribeModule = &cobra.Command{
Use: "modules [module_name]",
Short: "Describe one or more modules",
Long: "The describe modules command allows you to remove one or more modules from the Cyclops API.",
Example: describeModuleExample,
Aliases: []string{"module"},
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
describeModules(kubeconfig.Moduleset, args)
},
}
)
76 changes: 76 additions & 0 deletions cyctl/internal/describe/template_auth_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package describe

import (
"fmt"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/cyclops-ui/cycops-cyctl/utility"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
describeTemplateAuthRuleExample = `
# Describe a single templateauthrule
cyctl describe templateauthrule templateauthrule1
# Describe multiple templateauthrule
cyctl describe templateauthrule templateauthrule1 templateauthrule2 templateauthrule3`
)

// describeTemplateAuthRules describe a specified templateauthrules from the Cyclops API.
func describeTemplateAuthRules(clientset *client.CyclopsV1Alpha1Client, templateAuthNames []string) {
if len(templateAuthNames) == 0 {
templateauthrules, err := clientset.TemplateAuthRules("cyclops").List(v1.ListOptions{})
if err != nil {
fmt.Printf("Error fetching templateauthrules: %v\n", err)
return
}
for _, templateAuth := range templateauthrules {
templateAuthNames = append(templateAuthNames, templateAuth.Name)
}
}

for _, templateAuthName := range templateAuthNames {
templateAuth, err := clientset.TemplateAuthRules("cyclops").Get(templateAuthName)
if err != nil {
fmt.Printf("Error from server (NotFound): %v\n", err)
return
}
// Describe the templateAuthRule
describe := utility.Describe(func(d *utility.Describer) {
d.DescribeMetaData(templateAuth.ObjectMeta)

d.Printf("Creation:\t%s\n", templateAuth.CreationTimestamp)

d.Println()
d.Printf("Repository:\t%s\n", templateAuth.Spec.Repo)

d.Println()
d.Printf("Credentails:\t\n")
d.Printf("Username:\t%s\n", templateAuth.Spec.Username.Key)
d.Printf("Password:\t%s\n", templateAuth.Spec.Password.Key)

d.Println()
})

fmt.Printf("%s", describe)
fmt.Println("-------------------------------")

}
}

var (
DescribeTemplateAuthRule = &cobra.Command{
Use: "templateauthrules [template_auth_rule_name]",
Short: "Describe one or more templateauthrule",
Long: "The describe templateauthrule command allows you to remove one or more modules from the Cyclops API.",
Example: describeTemplateAuthRuleExample,
Aliases: []string{"templateauthrule"},
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
describeTemplateAuthRules(kubeconfig.Moduleset, args)
},
}
)
72 changes: 72 additions & 0 deletions cyctl/internal/describe/template_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package describe

import (
"fmt"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/cyclops-ui/cycops-cyctl/utility"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
describeTemplateExample = `
# Describe a single template
cyctl describe templates template1
# Describe multiple templates
cyctl describe templates template1 template2 template3`
)

// describeTemplate descrbe a specified templatestore from the Cyclops API.
func describeTemplate(clientset *client.CyclopsV1Alpha1Client, templateNames []string) {
if len(templateNames) == 0 {
templates, err := clientset.TemplateStore("cyclops").List(v1.ListOptions{})
if err != nil {
fmt.Printf("Error fetching templateauthrules: %v\n", err)
return
}
for _, template := range templates {
templateNames = append(templateNames, template.Name)
}
}

for _, templateName := range templateNames {
template, err := clientset.TemplateStore("cyclops").Get(templateName)
if err != nil {
fmt.Printf("Error from server (NotFound): %v\n", err)
return
}
// Describe the template
describe := utility.Describe(func(d *utility.Describer) {
d.DescribeMetaData(template.ObjectMeta)

d.Printf("Creation:\t%s\n", template.CreationTimestamp)

d.Println()
d.Printf("Repository:\t%s\n", template.Spec.URL)
d.Printf("Branch:\t%s\n", template.Spec.Version)
d.Printf("RelativePath:\t%s\n", template.Spec.Path)

})

fmt.Printf("%s", describe)
fmt.Println("-------------------------------")

}
}

var (
DescribeTemplate = &cobra.Command{
Use: "template [template_auth_rule_name]",
Short: "Describe one or more templateauthrule",
Long: "The describe template command allows you to remove one or more modules from the Cyclops API.",
Example: describeTemplateExample,
Aliases: []string{"template"},
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
describeTemplate(kubeconfig.Moduleset, args)
},
}
)
Loading

0 comments on commit 0e68c2c

Please sign in to comment.