Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
loaynaser3 committed Sep 5, 2023
1 parent f52fdbd commit 939fb32
Show file tree
Hide file tree
Showing 24 changed files with 146 additions and 270 deletions.
12 changes: 3 additions & 9 deletions cmd/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@ var allCmd = &cobra.Command{
Short: "Gets unused resources",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedAllJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedAllStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedAllYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedAll(includeExcludeLists, kubeconfig)
Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var configmapCmd = &cobra.Command{
Short: "Gets unused configmaps",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedConfigmapsJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedConfigmapsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedConfigmapsYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedConfigmaps(includeExcludeLists, kubeconfig)
Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var deployCmd = &cobra.Command{
Short: "Gets unused deployments",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedDeploymentsJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedDeploymentsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedDeploymentsYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedDeployments(includeExcludeLists, kubeconfig)
Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@ var hpaCmd = &cobra.Command{
Short: "Gets unused hpas",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedHpasJson(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedHpasStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedHpasYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedHpas(includeExcludeLists, kubeconfig)
Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var ingressCmd = &cobra.Command{
Short: "Gets unused ingresses",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedIngressesJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedIngressesStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedIngressesYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedIngresses(includeExcludeLists, kubeconfig)
Expand Down
13 changes: 4 additions & 9 deletions cmd/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ var pdbCmd = &cobra.Command{
Short: "Gets unused pdbs",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedPdbsJson(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedPdbsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedPdbsYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedPdbs(includeExcludeLists, kubeconfig)
}

},
}

Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@ var pvcCmd = &cobra.Command{
Short: "Gets unused pvcs",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedPvcsJson(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedPvcsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedPvcsYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedPvcs(includeExcludeLists, kubeconfig)
Expand Down
13 changes: 4 additions & 9 deletions cmd/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ var roleCmd = &cobra.Command{
Short: "Gets unused roles",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedRolesJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedRolesStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedRolesYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedRoles(includeExcludeLists, kubeconfig)
}

},
}

Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var secretCmd = &cobra.Command{
Short: "Gets unused secrets",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedSecretsJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedSecretsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedSecretsYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedSecrets(includeExcludeLists, kubeconfig)
Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/serviceaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var serviceAccountCmd = &cobra.Command{
Short: "Gets unused service accounts",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedServiceAccountsJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedServiceAccountsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedServiceAccountsYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedServiceAccounts(includeExcludeLists, kubeconfig)
Expand Down
12 changes: 3 additions & 9 deletions cmd/kor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ var serviceCmd = &cobra.Command{
Short: "Gets unused services",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
if jsonResponse, err := kor.GetUnusedServicesJSON(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedServicesStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(jsonResponse)
}
} else if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedServicesYAML(includeExcludeLists, kubeconfig); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedServices(includeExcludeLists, kubeconfig)
Expand Down
6 changes: 3 additions & 3 deletions cmd/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ var stsCmd = &cobra.Command{
Short: "Gets unused statefulsets",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "yaml" {
if yamlResponse, err := kor.GetUnusedStatefulsetsYAML(includeExcludeLists, kubeconfig); err != nil {
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedStatefulsetsStructured(includeExcludeLists, kubeconfig, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(yamlResponse)
fmt.Println(response)
}
} else {
kor.GetUnusedStatefulsets(includeExcludeLists, kubeconfig)
Expand Down
24 changes: 9 additions & 15 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func GetUnusedAll(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
}
}

func GetUnusedAllJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
func GetUnusedAllStructured(includeExcludeLists IncludeExcludeLists, kubeconfig string, outputFormat string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

Expand Down Expand Up @@ -216,19 +216,13 @@ func GetUnusedAllJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string
return "", err
}

return string(jsonResponse), nil
}

func GetUnusedAllYAML(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {

jsonResponse, err := GetUnusedAllJSON(includeExcludeLists, kubeconfig)
if err != nil {
fmt.Println(err)
}

yamlResponse, err := yaml.JSONToYAML([]byte(jsonResponse))
if err != nil {
fmt.Printf("err: %v\n", err)
if outputFormat == "yaml" {
yamlResponse, err := yaml.JSONToYAML(jsonResponse)
if err != nil {
fmt.Printf("err: %v\n", err)
}
return string(yamlResponse), nil
} else {
return string(jsonResponse), nil
}
return (string(yamlResponse)), nil
}
22 changes: 9 additions & 13 deletions pkg/kor/confimgmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func GetUnusedConfigmaps(includeExcludeLists IncludeExcludeLists, kubeconfig str
}
}

func GetUnusedConfigmapsJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
func GetUnusedConfigmapsStructured(includeExcludeLists IncludeExcludeLists, kubeconfig string, outputFormat string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

Expand All @@ -151,18 +151,14 @@ func GetUnusedConfigmapsJSON(includeExcludeLists IncludeExcludeLists, kubeconfig
return "", err
}

return string(jsonResponse), nil
}

func GetUnusedConfigmapsYAML(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
jsonResponse, err := GetUnusedConfigmapsJSON(includeExcludeLists, kubeconfig)
if err != nil {
fmt.Println(err)
if outputFormat == "yaml" {
yamlResponse, err := yaml.JSONToYAML(jsonResponse)
if err != nil {
fmt.Printf("err: %v\n", err)
}
return string(yamlResponse), nil
} else {
return string(jsonResponse), nil
}

yamlResponse, err := yaml.JSONToYAML([]byte(jsonResponse))
if err != nil {
fmt.Printf("err: %v\n", err)
}
return (string(yamlResponse)), nil
}
23 changes: 9 additions & 14 deletions pkg/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetUnusedDeployments(includeExcludeLists IncludeExcludeLists, kubeconfig st
}
}

func GetUnusedDeploymentsJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
func GetUnusedDeploymentsStructured(includeExcludeLists IncludeExcludeLists, kubeconfig string, outputFormat string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

Expand All @@ -82,18 +82,13 @@ func GetUnusedDeploymentsJSON(includeExcludeLists IncludeExcludeLists, kubeconfi
return "", err
}

return string(jsonResponse), nil
}

func GetUnusedDeploymentsYAML(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
jsonResponse, err := GetUnusedDeploymentsJSON(includeExcludeLists, kubeconfig)
if err != nil {
fmt.Println(err)
}

yamlResponse, err := yaml.JSONToYAML([]byte(jsonResponse))
if err != nil {
fmt.Printf("err: %v\n", err)
if outputFormat == "yaml" {
yamlResponse, err := yaml.JSONToYAML(jsonResponse)
if err != nil {
fmt.Printf("err: %v\n", err)
}
return string(yamlResponse), nil
} else {
return string(jsonResponse), nil
}
return (string(yamlResponse)), nil
}
Loading

0 comments on commit 939fb32

Please sign in to comment.