Skip to content

Commit 022f9fd

Browse files
Feat/terraform dir (#121)
* feat: Improved Terraform directory * fixed terraform validate command * Update action.yml to use Dockerfile * feat: Improved test case and added feature of dir in terraform apply
1 parent 178f005 commit 022f9fd

25 files changed

+437
-360
lines changed

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ inputs:
1818

1919
runs:
2020
using: "docker"
21-
image: 'docker://ghcr.io/clouddrove/smurf:v1.0.0'
21+
image: 'docker://ghcr.io/clouddrove/smurf:vv1.0.3'
2222
args:
2323
- ${{ inputs.tool }}
2424
- ${{ inputs.command }}

cmd/stf/apply.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ var applyApprove bool
99
var applyVarNameValue []string
1010
var applyVarFile []string
1111
var applyLock bool
12+
var applyDir string
1213

1314
// applyCmd defines a subcommand that applies the changes required to reach the desired state of Terraform Infrastructure.
1415
var applyCmd = &cobra.Command{
1516
Use: "apply",
1617
Short: "Apply the changes required to reach the desired state of Terraform Infrastructure",
1718
RunE: func(cmd *cobra.Command, args []string) error {
18-
return terraform.Apply(applyApprove, applyVarNameValue, applyVarFile, applyLock)
19+
return terraform.Apply(applyApprove, applyVarNameValue, applyVarFile, applyLock, applyDir)
1920
},
2021
Example: `
2122
smurf stf apply
@@ -25,6 +26,9 @@ var applyCmd = &cobra.Command{
2526
2627
# Specify multiple variables
2728
smurf stf apply -var="region=us-west-2" -var="instance_type=t2.micro"
29+
30+
# Specify a custom directory
31+
smurf stf apply --dir=/path/to/terraform/files
2832
`,
2933
}
3034

@@ -33,5 +37,7 @@ func init() {
3337
applyCmd.Flags().StringArrayVar(&applyVarFile, "var-file", []string{}, "Specify a file containing variables")
3438
applyCmd.Flags().BoolVar(&applyApprove, "approve", false, "Skip interactive approval of plan before applying")
3539
applyCmd.Flags().BoolVar(&applyLock, "lock", true, "Don't hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.")
40+
applyCmd.Flags().StringVar(&applyDir, "dir", ".", "Specify the directory containing Terraform files")
41+
3642
stfCmd.AddCommand(applyCmd)
3743
}

cmd/stf/destroy.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ import (
77

88
var destroyApprove bool
99
var destroyLock bool
10+
var destroyDir string
1011

1112
// destroyCmd defines a subcommand that destroys the Terraform Infrastructure.
1213
var destroyCmd = &cobra.Command{
1314
Use: "destroy",
1415
Short: "Destroy the Terraform Infrastructure",
1516
RunE: func(cmd *cobra.Command, args []string) error {
16-
return terraform.Destroy(destroyApprove, destroyLock)
17+
return terraform.Destroy(destroyApprove, destroyLock, destroyDir)
1718
},
1819
Example: `
19-
smurf stf destroy
20+
smurf stf destroy
21+
smurf stf destroy --dir=/path/to/terraform
2022
`,
2123
}
2224

2325
func init() {
2426
destroyCmd.Flags().BoolVar(&destroyApprove, "approve", false, "Skip interactive approval of plan before applying")
2527
destroyCmd.Flags().BoolVar(&destroyLock, "lock", true, "Don't hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.")
28+
destroyCmd.Flags().StringVar(&destroyDir, "dir", ".", "Specify the directory containing Terraform configuration")
2629
stfCmd.AddCommand(destroyCmd)
27-
}
30+
}

cmd/stf/drift.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8+
var driftDir string
9+
810
// driftCmd defines a subcommand that detects drift between state and infrastructure for Terraform.
911
var driftCmd = &cobra.Command{
1012
Use: "drift",
11-
Short: "Detect drift between state and infrastructure for Terraform",
13+
Short: "Detect drift between state and infrastructure for Terraform",
1214
RunE: func(cmd *cobra.Command, args []string) error {
13-
14-
return terraform.DetectDrift()
15+
return terraform.DetectDrift(driftDir)
1516
},
1617
Example: `
1718
smurf stf drift
19+
smurf stf drift --dir=/path/to/terraform
1820
`,
1921
}
2022

2123
func init() {
24+
driftCmd.Flags().StringVar(&driftDir, "dir", ".", "Specify the directory containing Terraform configuration")
2225
stfCmd.AddCommand(driftCmd)
23-
}
26+
}

cmd/stf/graph.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8-
// graphCmd represents the command to generate a visual representation of the Terraform resources
8+
var graphDir string
9+
10+
// graphCmd defines a subcommand that generates a visual representation of the Terraform resources.
911
var graphCmd = &cobra.Command{
1012
Use: "graph",
1113
Short: "Generate a visual graph of Terraform resources",
1214
RunE: func(cmd *cobra.Command, args []string) error {
13-
return terraform.Graph()
15+
return terraform.Graph(graphDir)
1416
},
1517
Example: `
16-
# Generate resource graph
17-
smurf stf graph
18-
`,
18+
smurf stf graph --dir <terraform-directory>
19+
smurf stf graph
20+
`,
1921
}
2022

2123
func init() {
24+
graphCmd.Flags().StringVar(&graphDir, "dir", ".", "Specify the directory containing Terraform configurations")
2225
stfCmd.AddCommand(graphCmd)
23-
}
26+
}

cmd/stf/init.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package stf
22

33
import (
4-
"github.com/clouddrove/smurf/internal/terraform"
5-
"github.com/spf13/cobra"
4+
"github.com/clouddrove/smurf/internal/terraform"
5+
"github.com/spf13/cobra"
66
)
77

8-
var initUpgrade bool
8+
var (
9+
initUpgrade bool
10+
initDir string // New variable for directory flag
11+
)
912

1013
// initCmd defines a subcommand that initializes Terraform.
1114
var initCmd = &cobra.Command{
12-
Use: "init",
13-
Short: "Initialize Terraform",
14-
RunE: func(cmd *cobra.Command, args []string) error {
15-
16-
return terraform.Init(initUpgrade)
17-
},
18-
Example: `
19-
smurf stf init
20-
`,
15+
Use: "init",
16+
Short: "Initialize Terraform",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
return terraform.Init(initDir, initUpgrade)
19+
},
20+
Example: `
21+
smurf stf init
22+
smurf stf init --dir=/path/to/terraform/files
23+
`,
2124
}
2225

2326
func init() {
24-
initCmd.Flags().BoolVar(&initUpgrade, "upgrade", true, "Upgrade the Terraform modules and plugins")
25-
stfCmd.AddCommand(initCmd)
27+
initCmd.Flags().BoolVar(&initUpgrade, "upgrade", true, "Upgrade the Terraform modules and plugins")
28+
initCmd.Flags().StringVar(&initDir, "dir", "", "Directory containing Terraform files (default is current directory)") // Add directory flag
29+
stfCmd.AddCommand(initCmd)
2630
}
31+

cmd/stf/output.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8+
var outputDir string
9+
810
// outputCmd defines a subcommand that generates output for the current state of Terraform Infrastructure.
911
var outputCmd = &cobra.Command{
1012
Use: "output",
1113
Short: "Generate output for the current state of Terraform Infrastructure",
1214
RunE: func(cmd *cobra.Command, args []string) error {
13-
14-
return terraform.Output()
15+
return terraform.Output(outputDir)
1516
},
1617
Example: `
1718
smurf stf output
19+
smurf stf output --dir <terraform-directory>
1820
`,
1921
}
2022

2123
func init() {
24+
outputCmd.Flags().StringVar(&outputDir, "dir", ".", "Specify the Terraform directory")
2225
stfCmd.AddCommand(outputCmd)
23-
}
26+
}

cmd/stf/plan.go

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
package stf
22

33
import (
4-
"github.com/clouddrove/smurf/internal/terraform"
5-
"github.com/spf13/cobra"
4+
"github.com/clouddrove/smurf/internal/terraform"
5+
"github.com/spf13/cobra"
66
)
77

88
var planVarNameValue []string
99
var planVarFile []string
10+
var planDir string
1011

1112
// planCmd defines a subcommand that generates and shows an execution plan for Terraform
1213
var planCmd = &cobra.Command{
13-
Use: "plan",
14-
Short: "Generate and show an execution plan for Terraform",
15-
RunE: func(cmd *cobra.Command, args []string) error {
16-
return terraform.Plan(planVarNameValue, planVarFile)
17-
},
18-
Example: `
19-
smurf stf plan
20-
21-
# Specify variables
22-
smurf stf plan -var="region=us-west-2"
23-
24-
# Specify multiple variables
25-
smurf stf plan -var="region=us-west-2" -var="instance_type=t2.micro"
26-
`,
14+
Use: "plan",
15+
Short: "Generate and show an execution plan for Terraform",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
return terraform.Plan(planVarNameValue, planVarFile, planDir)
18+
},
19+
Example: `
20+
smurf stf plan
21+
22+
# Specify variables
23+
smurf stf plan -var="region=us-west-2"
24+
25+
# Specify multiple variables
26+
smurf stf plan -var="region=us-west-2" -var="instance_type=t2.micro"
27+
28+
# Specify a custom directory
29+
smurf stf plan --dir=/path/to/terraform/files
30+
`,
2731
}
2832

2933
func init() {
30-
planCmd.Flags().StringArrayVar(&planVarNameValue, "var", []string{}, "Specify a variable in 'NAME=VALUE' format")
31-
planCmd.Flags().StringArrayVar(&planVarFile, "var-file", []string{}, "Specify a file containing variables")
34+
planCmd.Flags().StringArrayVar(&planVarNameValue, "var", []string{}, "Specify a variable in 'NAME=VALUE' format")
35+
planCmd.Flags().StringArrayVar(&planVarFile, "var-file", []string{}, "Specify a file containing variables")
36+
planCmd.Flags().StringVar(&planDir, "dir", ".", "Specify the directory containing Terraform files")
3237

33-
stfCmd.AddCommand(planCmd)
38+
stfCmd.AddCommand(planCmd)
3439
}

cmd/stf/provision.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@ var varNameValue []string
1010
var varFile []string
1111
var lock bool
1212
var upgrade bool
13+
var provisionDir string // Define provisionDir variable
1314

14-
// provisionCmd orchestrates multiple Terraform operations (init, plan, apply, output)
15-
// in a sequential flow, grouping them into one streamlined command. After successful
16-
// initialization, planning, and applying of changes, it retrieves the final outputs
17-
// asynchronously and handles any errors accordingly.
1815
var provisionCmd = &cobra.Command{
1916
Use: "provision",
2017
Short: "Its the combination of init, plan, apply, output for Terraform",
2118
RunE: func(cmd *cobra.Command, args []string) error {
22-
if err := terraform.Init(upgrade); err != nil {
19+
if err := terraform.Init(provisionDir, upgrade); err != nil {
2320
return err
2421
}
2522

26-
if err := terraform.Plan(varNameValue, varFile); err != nil {
23+
if err := terraform.Plan(varNameValue, varFile, provisionDir); err != nil {
2724
return err
2825
}
2926

30-
if err := terraform.Apply(provisionApprove, varNameValue, varFile, lock); err != nil {
27+
if err := terraform.Apply(provisionApprove, varNameValue, varFile, lock, provisionDir); err != nil {
3128
return err
3229
}
3330

34-
if err := terraform.Output(); err != nil {
31+
if err := terraform.Output(provisionDir); err != nil {
3532
return err
3633
}
3734

3835
return nil
3936
},
4037
Example: `
4138
smurf stf provision
39+
smurf stf provision --dir=/path/to/terraform/files
4240
`,
4341
}
4442

@@ -48,5 +46,6 @@ func init() {
4846
provisionCmd.Flags().BoolVar(&provisionApprove, "approve", true, "Skip interactive approval of plan before applying")
4947
provisionCmd.Flags().BoolVar(&lock, "lock", false, "Don't hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.")
5048
provisionCmd.Flags().BoolVar(&upgrade, "upgrade", false, "Upgrade the Terraform modules and plugins to the latest versions")
49+
provisionCmd.Flags().StringVar(&provisionDir, "dir", "", "Specify the directory for Terraform operations") // Added flag
5150
stfCmd.AddCommand(provisionCmd)
52-
}
51+
}

cmd/stf/refresh.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@ var (
99
refreshVars []string
1010
refreshVarFiles []string
1111
refreshLock bool
12+
refreshDir string
1213
)
1314

1415
// refreshCmd represents the command to refresh the state of Terraform resources
1516
var refreshCmd = &cobra.Command{
1617
Use: "refresh",
1718
Short: "Update the state file of your infrastructure",
1819
RunE: func(cmd *cobra.Command, args []string) error {
19-
return terraform.Refresh(refreshVars, refreshVarFiles, refreshLock)
20+
return terraform.Refresh(refreshVars, refreshVarFiles, refreshLock, refreshDir)
2021
},
2122
Example: `
2223
# Basic refresh
2324
smurf stf refresh
2425
26+
# Refresh with a specific directory
27+
smurf stf refresh --dir=path/to/terraform/code
28+
2529
# Refresh with variables
26-
smurf stf refresh -var="region=us-west-2"
30+
smurf stf refresh --var="region=us-west-2"
2731
2832
# Refresh with variable file
29-
smurf stf refresh -var-file="prod.tfvars"
33+
smurf stf refresh --var-file="prod.tfvars"
3034
3135
# Refresh without state locking
3236
smurf stf refresh --lock=false
@@ -37,6 +41,7 @@ func init() {
3741
refreshCmd.Flags().StringArrayVar(&refreshVars, "var", []string{}, "Set a variable in 'name=value' format")
3842
refreshCmd.Flags().StringArrayVar(&refreshVarFiles, "var-file", []string{}, "Path to a Terraform variable file")
3943
refreshCmd.Flags().BoolVar(&refreshLock, "lock", true, "Lock the state file when running operation (defaults to true)")
44+
refreshCmd.Flags().StringVar(&refreshDir, "dir", ".", "Specify the Terraform directory")
4045

4146
stfCmd.AddCommand(refreshCmd)
42-
}
47+
}

cmd/stf/state.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8+
var stateListDir string
9+
810
// stateListCmd represents the command to list resources in the Terraform state
911
var stateListCmd = &cobra.Command{
1012
Use: "state-list",
1113
Short: "List resources in the Terraform state",
1214
RunE: func(cmd *cobra.Command, args []string) error {
13-
err := terraform.StateList()
15+
err := terraform.StateList(stateListDir)
1416

1517
if err != nil {
1618
terraform.ErrorHandler(err)
@@ -22,9 +24,13 @@ var stateListCmd = &cobra.Command{
2224
Example: `
2325
# List all resources in state
2426
smurf stf state-list
27+
28+
# List resources in a specific directory
29+
smurf stf state-list --dir=path/to/terraform/code
2530
`,
2631
}
2732

2833
func init() {
34+
stateListCmd.Flags().StringVar(&stateListDir, "dir", ".", "Specify the Terraform directory")
2935
stfCmd.AddCommand(stateListCmd)
30-
}
36+
}

0 commit comments

Comments
 (0)