github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/commands/down_infra.go (about) 1 package commands 2 3 import ( 4 "context" 5 "fmt" 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/hazelops/ize/internal/config" 8 "github.com/hazelops/ize/internal/manager" 9 "github.com/hazelops/ize/internal/requirements" 10 "github.com/hazelops/ize/pkg/terminal" 11 "github.com/spf13/cobra" 12 ) 13 14 type DownInfraOptions struct { 15 Config *config.Project 16 ui terminal.UI 17 Version string 18 AwsProfile string 19 AwsRegion string 20 SkipGen bool 21 OnlyInfra bool 22 } 23 24 func NewDownInfraFlags(project *config.Project) *DownInfraOptions { 25 return &DownInfraOptions{ 26 Config: project, 27 } 28 } 29 30 func NewCmdDownInfra(project *config.Project) *cobra.Command { 31 o := NewDownInfraFlags(project) 32 33 cmd := &cobra.Command{ 34 Use: "infra", 35 Short: "Destroy infrastructure", 36 RunE: func(cmd *cobra.Command, args []string) error { 37 cmd.SilenceUsage = true 38 err := o.Complete() 39 if err != nil { 40 return err 41 } 42 43 err = o.Validate() 44 if err != nil { 45 return err 46 } 47 48 err = o.Run() 49 if err != nil { 50 return err 51 } 52 53 return nil 54 }, 55 } 56 57 cmd.Flags().StringVar(&o.Version, "infra.terraform.version", "", "set terraform version") 58 cmd.Flags().StringVar(&o.AwsProfile, "infra.terraform.aws-profile", "", "set aws profile") 59 cmd.Flags().StringVar(&o.AwsRegion, "infra.terraform.aws-region", "", "set aws region") 60 cmd.Flags().BoolVar(&o.SkipGen, "skip-gen", false, "skip generating terraform files") 61 cmd.Flags().BoolVar(&o.OnlyInfra, "only-infra", false, "down only infra state") 62 63 return cmd 64 } 65 66 func (o *DownInfraOptions) Complete() error { 67 if err := requirements.CheckRequirements(requirements.WithIzeStructure(), requirements.WithConfigFile()); err != nil { 68 return err 69 } 70 71 if o.Config.Terraform == nil { 72 return fmt.Errorf("you must specify at least one terraform stack in ize.toml") 73 } 74 75 if _, ok := o.Config.Terraform["infra"]; ok { 76 if len(o.AwsProfile) != 0 { 77 o.Config.Terraform["infra"].AwsProfile = o.AwsProfile 78 } 79 80 if len(o.Config.Terraform["infra"].AwsProfile) == 0 { 81 o.Config.Terraform["infra"].AwsProfile = o.Config.AwsProfile 82 } 83 84 if len(o.AwsProfile) != 0 { 85 o.Config.Terraform["infra"].AwsRegion = o.AwsRegion 86 } 87 88 if len(o.Config.Terraform["infra"].AwsRegion) == 0 { 89 o.Config.Terraform["infra"].AwsRegion = o.Config.AwsRegion 90 } 91 92 if len(o.Version) != 0 { 93 o.Config.Terraform["infra"].Version = o.Version 94 } 95 96 if len(o.Config.Terraform["infra"].Version) == 0 { 97 o.Config.Terraform["infra"].Version = o.Config.TerraformVersion 98 } 99 } 100 101 o.ui = terminal.ConsoleUI(context.Background(), o.Config.PlainText) 102 103 return nil 104 } 105 106 func (o *DownInfraOptions) Validate() error { 107 if len(o.Config.Env) == 0 { 108 return fmt.Errorf("env must be specified") 109 } 110 111 return nil 112 } 113 114 func (o *DownInfraOptions) Run() error { 115 ui := o.ui 116 117 if _, ok := o.Config.Terraform["infra"]; ok { 118 err := destroyInfra("infra", o.Config, o.SkipGen, ui) 119 if err != nil { 120 return err 121 } 122 } 123 124 err := manager.InReversDependencyOrder(aws.BackgroundContext(), o.Config.GetStates(), func(c context.Context, name string) error { 125 return destroyInfra(name, o.Config, o.SkipGen, ui) 126 }) 127 if err != nil { 128 129 } 130 131 return nil 132 }