@@ -68,12 +68,20 @@ func (terraform Terraform) Apply() (string, string, error) {
6868 println ("terraform init failed." )
6969 return stdout .GetString (), "" , fmt .Errorf ("terraform init failed. %s" , err )
7070 }
71-
72- err = tf .WorkspaceSelect (context .Background (), terraform .Workspace )
71+ currentWorkspace , err := tf .WorkspaceShow (context .Background ())
7372
7473 if err != nil {
75- log .Printf ("terraform workspace select failed. workspace: %v. dir: %v" , terraform .Workspace , terraform .WorkingDir )
76- return stdout .GetString (), "" , fmt .Errorf ("terraform select failed. %s" , err )
74+ log .Printf ("terraform workspace show failed. workspace: %v . dir: %v" , terraform .Workspace , terraform .WorkingDir )
75+ return stdout .GetString (), "" , fmt .Errorf ("terraform show failed. %s" , err )
76+ }
77+
78+ if currentWorkspace != terraform .Workspace {
79+ err = tf .WorkspaceNew (context .Background (), terraform .Workspace )
80+
81+ if err != nil {
82+ log .Printf ("terraform workspace new failed. workspace: %v . dir: %v" , terraform .Workspace , terraform .WorkingDir )
83+ return stdout .GetString (), "" , fmt .Errorf ("terraform select failed. %s" , err )
84+ }
7785 }
7886
7987 err = tf .Apply (context .Background ())
@@ -123,12 +131,22 @@ func (terraform Terraform) Plan() (bool, string, string, error) {
123131 println ("terraform init failed." )
124132 return false , stdout .GetString (), stderr .GetString (), fmt .Errorf ("terraform init failed. %s" , err )
125133 }
126- err = tf .WorkspaceSelect (context .Background (), terraform . Workspace )
134+ currentWorkspace , err : = tf .WorkspaceShow (context .Background ())
127135
128136 if err != nil {
129- log .Printf ("terraform workspace select failed. workspace: %v. dir: %v" , terraform .Workspace , terraform .WorkingDir )
130- return false , stdout .GetString (), stderr . GetString () , fmt .Errorf ("terraform select failed. %s" , err )
137+ log .Printf ("terraform workspace show failed. workspace: %v . dir: %v" , terraform .Workspace , terraform .WorkingDir )
138+ return false , stdout .GetString (), "" , fmt .Errorf ("terraform show failed. %s" , err )
131139 }
140+
141+ if currentWorkspace != terraform .Workspace {
142+ err = tf .WorkspaceNew (context .Background (), terraform .Workspace )
143+
144+ if err != nil {
145+ log .Printf ("terraform workspace new failed. workspace: %v . dir: %v" , terraform .Workspace , terraform .WorkingDir )
146+ return false , stdout .GetString (), "" , fmt .Errorf ("terraform select failed. %s" , err )
147+ }
148+ }
149+
132150 isNonEmptyPlan , err := tf .Plan (context .Background ())
133151 if err != nil {
134152 println ("terraform plan failed. dir: " + terraform .WorkingDir )
0 commit comments