@@ -10,10 +10,9 @@ import (
1010 "strings"
1111
1212 "github.com/hashicorp/terraform-exec/tfexec"
13- "github.com/pterm/pterm"
1413)
1514
16- // FormatError represents a single formatting error with its details
15+ // FormatError represents a single formatting error with details
1716type FormatError struct {
1817 ErrorType string
1918 Description string
@@ -32,13 +31,10 @@ type CustomFormatter struct {
3231
3332// NewCustomFormatter creates a new formatter instance
3433func NewCustomFormatter (tf * tfexec.Terraform , workDir string ) * CustomFormatter {
35- return & CustomFormatter {
36- tf : tf ,
37- workDir : workDir ,
38- }
34+ return & CustomFormatter {tf : tf , workDir : workDir }
3935}
4036
41- // findTerraformFiles finds all .tf files in the given directory
37+ // findTerraformFiles finds all ` .tf` files in the given directory
4238func (cf * CustomFormatter ) findTerraformFiles (root string , recursive bool ) ([]string , error ) {
4339 var files []string
4440
@@ -62,51 +58,48 @@ func (cf *CustomFormatter) findTerraformFiles(root string, recursive bool) ([]st
6258 return nil
6359 }
6460
65- err := filepath .Walk (root , walkFn )
66- if err != nil {
61+ if err := filepath .Walk (root , walkFn ); err != nil {
6762 return nil , err
6863 }
69-
7064 return files , nil
7165}
7266
7367// formatError formats a single formatting error in Terraform style
7468func (cf * CustomFormatter ) formatError (err FormatError ) string {
7569 var sb strings.Builder
7670
77- errorSymbol := pterm .Red ("│" )
78- errorPrefix := pterm .Red ("Error: " )
79- locationColor := pterm .White (err .Location )
80- lineNumColor := pterm .White (fmt .Sprintf ("line %d" , err .LineNumber ))
81-
82- sb .WriteString ("╷\n " )
83- sb .WriteString (fmt .Sprintf ("%s %s%s\n " , errorSymbol , errorPrefix , err .Description ))
84- sb .WriteString (fmt .Sprintf ("%s\n " , errorSymbol ))
85- sb .WriteString (fmt .Sprintf ("%s on %s %s:\n " , errorSymbol , locationColor , lineNumColor ))
86- sb .WriteString (fmt .Sprintf ("%s %d: %s\n " , errorSymbol , err .LineNumber , err .LineContent ))
71+ sb .WriteString ("\n ╷\n " )
72+ sb .WriteString (fmt .Sprintf ("│ %s%s\n " , RedText ("Error: " ), err .Description ))
73+ sb .WriteString ("│\n " )
74+ sb .WriteString (fmt .Sprintf ("│ on %s line %d:\n " , GreyText (err .Location ), err .LineNumber ))
75+ sb .WriteString (fmt .Sprintf ("│ %d: %s\n " , err .LineNumber , err .LineContent ))
8776 if err .NextLine != "" {
88- sb .WriteString (fmt .Sprintf ("%s %d: %s\n " , errorSymbol , err .LineNumber + 1 , err .NextLine ))
77+ sb .WriteString (fmt .Sprintf ("│ %d: %s\n " , err .LineNumber + 1 , err .NextLine ))
8978 }
9079 if err .HelpText != "" {
91- sb .WriteString (fmt . Sprintf ( "%s \n ", errorSymbol ) )
92- sb .WriteString (fmt .Sprintf ("%s %s\n " , errorSymbol , err .HelpText ))
80+ sb .WriteString ("│ \n " )
81+ sb .WriteString (fmt .Sprintf ("│ %s\n " , GreyText ( err .HelpText ) ))
9382 }
9483 sb .WriteString ("╵\n " )
95-
9684 return sb .String ()
9785}
9886
99- // FormatWithDetails performs formatting and returns detailed output
87+ // FormatWithDetails performs Terraform formatting with rich logging
10088func (cf * CustomFormatter ) FormatWithDetails (ctx context.Context , dir string , recursive bool ) error {
101- spinner , _ := pterm . DefaultSpinner . Start ( "Formatting Terraform configuration files ..." )
89+ Info ( "Starting Terraform formatting process ..." )
10290
10391 files , err := cf .findTerraformFiles (dir , recursive )
10492 if err != nil {
105- spinner . Fail ("Failed to find Terraform files" )
93+ Error ("Failed to find Terraform files: %v" , err )
10694 return fmt .Errorf ("error finding Terraform files: %w" , err )
10795 }
10896
109- formatted := make ([]string , 0 )
97+ if len (files ) == 0 {
98+ Warn ("No Terraform (.tf) files found in the directory." )
99+ return nil
100+ }
101+
102+ formatted := []string {}
110103 var formatErrors []FormatError
111104
112105 for _ , file := range files {
@@ -116,7 +109,6 @@ func (cf *CustomFormatter) FormatWithDetails(ctx context.Context, dir string, re
116109 ErrorType : "File read failed" ,
117110 Description : err .Error (),
118111 Location : file ,
119- LineNumber : 0 ,
120112 HelpText : "Failed to read file for formatting check" ,
121113 })
122114 continue
@@ -129,7 +121,6 @@ func (cf *CustomFormatter) FormatWithDetails(ctx context.Context, dir string, re
129121 ErrorType : "Terraform init failed" ,
130122 Description : err .Error (),
131123 Location : file ,
132- LineNumber : 0 ,
133124 HelpText : "Failed to initialize Terraform for formatting" ,
134125 })
135126 continue
@@ -143,7 +134,7 @@ func (cf *CustomFormatter) FormatWithDetails(ctx context.Context, dir string, re
143134 }
144135
145136 if bytes .Equal (content , outputBuffer .Bytes ()) {
146- continue
137+ continue // Already properly formatted
147138 }
148139
149140 err = os .WriteFile (file , outputBuffer .Bytes (), 0644 )
@@ -152,7 +143,6 @@ func (cf *CustomFormatter) FormatWithDetails(ctx context.Context, dir string, re
152143 ErrorType : "File write failed" ,
153144 Description : err .Error (),
154145 Location : file ,
155- LineNumber : 0 ,
156146 HelpText : "Failed to write formatted content to file" ,
157147 })
158148 continue
@@ -162,21 +152,21 @@ func (cf *CustomFormatter) FormatWithDetails(ctx context.Context, dir string, re
162152 }
163153
164154 if len (formatErrors ) > 0 {
165- spinner .Fail (fmt .Sprintf ("Formatting failed with %d errors" , len (formatErrors )))
166- for _ , err := range formatErrors {
167- fmt .Print (cf .formatError (err ))
155+ for _ , e := range formatErrors {
156+ fmt .Print (cf .formatError (e ))
168157 }
158+ Error ("Formatting failed with %d errors" , len (formatErrors ))
169159 return fmt .Errorf ("formatting failed with %d errors" , len (formatErrors ))
170160 }
171161
172162 if len (formatted ) > 0 {
173- spinner . Success ("Terraform files formatted successfully" )
174- pterm . Info . Println ( " \n Formatted files:" )
163+ Success ("Terraform files formatted successfully ✅ " )
164+ Info ( "Formatted files:" )
175165 for _ , file := range formatted {
176- pterm . Info . Printf ("- %s\n " , file )
166+ fmt . Printf (" %s\n " , CyanText ( file ) )
177167 }
178168 } else {
179- spinner . Success ("No files needed formatting " )
169+ Success ("No Terraform file changes detected " )
180170 }
181171
182172 return nil
@@ -189,42 +179,44 @@ func (cf *CustomFormatter) parseFormatError(err error, file string) FormatError
189179 Description : err .Error (),
190180 Location : file ,
191181 LineNumber : 1 ,
192- LineContent : "" ,
193- HelpText : "Please check the file syntax and try again" ,
182+ HelpText : "Please check file syntax and try again" ,
194183 }
195184}
196185
186+ // GetFmtTerraform initializes and returns a Terraform executor
197187func GetFmtTerraform () (* tfexec.Terraform , error ) {
198188 workDir , err := os .Getwd ()
199189 if err != nil {
190+ Error ("Failed to get working directory: %v" , err )
200191 return nil , fmt .Errorf ("failed to get working directory: %w" , err )
201192 }
202193
203194 terraformPath , err := exec .LookPath ("terraform" )
204195 if err != nil {
196+ Error ("Terraform executable not found: %v" , err )
205197 return nil , fmt .Errorf ("terraform executable not found: %w" , err )
206198 }
207199
208200 tf , err := tfexec .NewTerraform (workDir , terraformPath )
209201 if err != nil {
202+ Error ("Failed to create Terraform executor: %v" , err )
210203 return nil , fmt .Errorf ("failed to create Terraform executor: %w" , err )
211204 }
212205
213206 return tf , nil
214207}
215208
216- // Format applies a canonical format to Terraform configuration files.
217- // It runs `terraform fmt` in the current directory to ensure that all
218- // Terraform files adhere to the standard formatting conventions.
209+ // Format applies canonical formatting to all Terraform files.
219210func Format (recursive bool ) error {
220- workDir , err := os . Getwd ()
211+ tf , err := GetFmtTerraform ()
221212 if err != nil {
222- return fmt . Errorf ( "failed to get working directory: %w" , err )
213+ return err
223214 }
224215
225- tf , err := GetFmtTerraform ()
216+ workDir , err := os . Getwd ()
226217 if err != nil {
227- return err
218+ Error ("Failed to get working directory: %v" , err )
219+ return fmt .Errorf ("failed to get working directory: %w" , err )
228220 }
229221
230222 formatter := NewCustomFormatter (tf , workDir )
0 commit comments