Skip to content

Commit

Permalink
👻 cyctl handle nonexistent resource (#324)
Browse files Browse the repository at this point in the history
* fix #323: cyctl get nonexistent resource

* cover all cases

* cover templates as well

* minor fix

* formatting
  • Loading branch information
thebigbone authored Jun 10, 2024
1 parent 4aa1a59 commit f0d052e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
28 changes: 25 additions & 3 deletions cyctl/internal/get/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
)

var (
Expand Down Expand Up @@ -42,26 +43,47 @@ func listModules(clientset *client.CyclopsV1Alpha1Client, moduleNames []string)
}

filteredModules := modules
notFoundModules := make([]string, 0)

if len(moduleNames) > 0 {
nameSet := make(map[string]struct{}, len(moduleNames))
for _, name := range moduleNames {
nameSet[name] = struct{}{}
}
filteredModules = []v1alpha1.Module{}
foundModules := make([]v1alpha1.Module, 0)

for _, module := range modules {
if _, found := nameSet[module.Name]; found {
filteredModules = append(filteredModules, module)
foundModules = append(foundModules, module)
delete(nameSet, module.Name)
}
}
for name := range nameSet {
notFoundModules = append(notFoundModules, name)
}
if len(notFoundModules) > 0 {
for _, name := range notFoundModules {
fmt.Printf("no module found with name: %s\n", name)
}
}
filteredModules = foundModules
}

headerSpacing := max(0, longestName-4)
fmt.Println("NAME" + strings.Repeat(" ", headerSpacing) + " AGE")
output := ""
if len(filteredModules) > 0 {
output += "NAME" + strings.Repeat(" ", headerSpacing) + " AGE\n"
}

fmt.Print(output)
for _, module := range filteredModules {
age := time.Since(module.CreationTimestamp.Time).Round(time.Second)
nameSpacing := max(0, longestName-len(module.Name))
fmt.Printf("%s"+strings.Repeat(" ", nameSpacing)+" %s\n", module.Name, age.String())
}
if len(notFoundModules) > 0 {
os.Exit(1)
}
}

var (
Expand Down
29 changes: 26 additions & 3 deletions cyctl/internal/get/template_auth_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
)

var (
Expand Down Expand Up @@ -42,26 +43,48 @@ func listTemplateAuthRules(clientset *client.CyclopsV1Alpha1Client, templateAuth
}

filteredTemplatesAuthRules := templates
notFoundTemplatesAuthRules := make([]string, 0)

if len(templateAuthNames) > 0 {
nameSet := make(map[string]struct{}, len(templateAuthNames))
for _, name := range templateAuthNames {
nameSet[name] = struct{}{}
}
filteredTemplatesAuthRules = []v1alpha1.TemplateAuthRule{}

foundTemplatesAuthRules := make([]v1alpha1.TemplateAuthRule, 0)

for _, template := range templates {
if _, found := nameSet[template.Name]; found {
filteredTemplatesAuthRules = append(filteredTemplatesAuthRules, template)
foundTemplatesAuthRules = append(foundTemplatesAuthRules, template)
delete(nameSet, template.Name)
}
}
for name := range nameSet {
notFoundTemplatesAuthRules = append(notFoundTemplatesAuthRules, name)
}
if len(notFoundTemplatesAuthRules) > 0 {
for _, name := range notFoundTemplatesAuthRules {
fmt.Printf("no template auth rules found with name: %s\n", name)
}
}
filteredTemplatesAuthRules = foundTemplatesAuthRules
}

headerSpacing := max(0, longestName-4)
fmt.Println("NAME" + strings.Repeat(" ", headerSpacing) + " AGE")
output := ""
if len(filteredTemplatesAuthRules) > 0 {
output += "NAME" + strings.Repeat(" ", headerSpacing) + " AGE\n"
}

fmt.Print(output)
for _, template := range filteredTemplatesAuthRules {
age := time.Since(template.CreationTimestamp.Time).Round(time.Second)
nameSpacing := max(0, longestName-len(template.Name))
fmt.Printf("%s"+strings.Repeat(" ", nameSpacing)+" %s\n", template.Name, age.String())
}
if len(notFoundTemplatesAuthRules) > 0 {
os.Exit(1)
}
}

var (
Expand Down
29 changes: 26 additions & 3 deletions cyctl/internal/get/template_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
)

var (
Expand Down Expand Up @@ -42,26 +43,48 @@ func listTemplate(clientset *client.CyclopsV1Alpha1Client, templateNames []strin
}

filteredTemplates := templates
notFoundTemplates := make([]string, 0)

if len(templateNames) > 0 {
nameSet := make(map[string]struct{}, len(templateNames))
for _, name := range templateNames {
nameSet[name] = struct{}{}
}
filteredTemplates = []v1alpha1.TemplateStore{}

foundTemplates := make([]v1alpha1.TemplateStore, 0)

for _, template := range templates {
if _, found := nameSet[template.Name]; found {
filteredTemplates = append(filteredTemplates, template)
foundTemplates = append(foundTemplates, template)
delete(nameSet, template.Name)
}
}
for name := range nameSet {
notFoundTemplates = append(notFoundTemplates, name)
}
if len(notFoundTemplates) > 0 {
for _, name := range notFoundTemplates {
fmt.Printf("no templates found with name: %s\n", name)
}
}
filteredTemplates = foundTemplates
}

headerSpacing := max(0, longestName-4)
fmt.Println("NAME" + strings.Repeat(" ", headerSpacing) + " AGE")
output := ""
if len(filteredTemplates) > 0 {
output += "NAME" + strings.Repeat(" ", headerSpacing) + " AGE\n"
}

fmt.Print(output)
for _, template := range filteredTemplates {
age := time.Since(template.CreationTimestamp.Time).Round(time.Second)
nameSpacing := max(0, longestName-len(template.Name))
fmt.Printf("%s"+strings.Repeat(" ", nameSpacing)+" %s\n", template.Name, age.String())
}
if len(notFoundTemplates) > 0 {
os.Exit(1)
}
}

var (
Expand Down

0 comments on commit f0d052e

Please sign in to comment.