Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
Condense commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jfortin-sap committed Nov 30, 2020
1 parent 3381a5d commit 7d9f973
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
73 changes: 30 additions & 43 deletions pkg/cmd/infra.go → pkg/cmd/orphan.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,61 +28,48 @@ import (
)

// NewInfraCmd returns a new infra command
func NewInfraCmd(targetReader TargetReader) *cobra.Command {
func NewOrphanCmd(targetReader TargetReader) *cobra.Command {
return &cobra.Command{
Use: "infra [(orphan)] [(list)]",
Short: "Manage shoot infra resources\n",
Use: "orphan",
Short: "List shoot resources that do not exist in the Gardener terraform state\n",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
target := targetReader.ReadTarget(pathTarget)
if !CheckShootIsTargeted(target) {
return errors.New("no shoot targeted")
}
if len(args) < 2 || len(args) > 2 {
return errors.New("command must be in the format: infra [orphan] [list]")
var rs []string
pathTerraformState := filepath.Join(downloadTerraformFiles("infra", targetReader), "terraform.tfstate")
buf, err := ioutil.ReadFile(pathTerraformState)
if err != nil || len(buf) < 64 {
fmt.Println("Could not read terraform.tfstate: " + pathTerraformState)
os.Exit(2)
}
switch args[0] {
case "orphan":
switch args[1] {
case "list":
var rs []string
pathTerraformState := filepath.Join(downloadTerraformFiles("infra", targetReader), "terraform.tfstate")
buf, err := ioutil.ReadFile(pathTerraformState)
if err != nil || len(buf) < 64 {
fmt.Println("Could not read terraform.tfstate: " + pathTerraformState)
os.Exit(2)
}
terraformstate := string(buf)
terraformstate := string(buf)

shoot, err := FetchShootFromTarget(target)
checkError(err)
infraType := shoot.Spec.Provider.Type

switch infraType {
case "aws":
rs = getAWSInfraResources(targetReader)
case "azure":
rs = getAzureInfraResources(targetReader)
case "gcp":
rs = getGCPInfraResources(targetReader)
case "openstack":
rs = getOstackInfraResources(targetReader)
case "alicloud":
rs = getAliCloudInfraResources(targetReader)
default:
return errors.New("infra type not found")
}

err = GetOrphanInfraResources(rs, terraformstate)
checkError(err)
fmt.Printf("\n\nsearched %s\n", pathTerraformState)
default:
fmt.Println("command must be in the format: infra [orphan] [list]")
}
shoot, err := FetchShootFromTarget(target)
checkError(err)
infraType := shoot.Spec.Provider.Type

switch infraType {
case "aws":
rs = getAWSInfraResources(targetReader)
case "azure":
rs = getAzureInfraResources(targetReader)
case "gcp":
rs = getGCPInfraResources(targetReader)
case "openstack":
rs = getOstackInfraResources(targetReader)
case "alicloud":
rs = getAliCloudInfraResources(targetReader)
default:
fmt.Println("command must be in the format: infra [orphan] [list]")
return errors.New("infra type not found")
}

err = GetOrphanInfraResources(rs, terraformstate)
checkError(err)
fmt.Printf("\n\nsearched %s\n", pathTerraformState)

return nil
},
ValidArgs: []string{"orphan"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/infra_test.go → pkg/cmd/orphan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
. "github.com/onsi/gomega"
)

var _ = Describe("Infra", func() {
var _ = Describe("Orphan", func() {
var rs = []string{"vpc-03cb057da4ded427f"}
terraformstate := `
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func init() {
RootCmd.AddCommand(NewKubectlCmd(), NewKaCmd(), NewKsCmd(), NewKgCmd(), NewKnCmd())
RootCmd.AddCommand(NewKubectxCmd())
RootCmd.AddCommand(NewTerraformCmd(targetReader))
RootCmd.AddCommand(NewInfraCmd(targetReader))
RootCmd.AddCommand(NewOrphanCmd(targetReader))
RootCmd.AddCommand(NewAliyunCmd(targetReader), NewAwsCmd(targetReader), NewAzCmd(targetReader), NewGcloudCmd(targetReader), NewOpenstackCmd(targetReader))
RootCmd.AddCommand(NewInfoCmd(targetReader, ioStreams))
RootCmd.AddCommand(NewVersionCmd(), NewUpdateCheckCmd())
Expand Down

0 comments on commit 7d9f973

Please sign in to comment.