|
|
|
|
@ -55,63 +55,7 @@ func (f *HCL2Formatter) formatFile(path string, diags hcl.Diagnostics, bytesModi
|
|
|
|
|
// If any error is encountered, zero bytes will be returned.
|
|
|
|
|
//
|
|
|
|
|
// Path can be a directory or a file.
|
|
|
|
|
func (f *HCL2Formatter) Format(path string) (int, hcl.Diagnostics) {
|
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
var bytesModified int
|
|
|
|
|
|
|
|
|
|
if path == "" {
|
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: "path is empty, cannot format",
|
|
|
|
|
Detail: "path is empty, cannot format",
|
|
|
|
|
})
|
|
|
|
|
return bytesModified, diags
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f.parser == nil {
|
|
|
|
|
f.parser = hclparse.NewParser()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s, err := os.Stat(path); err != nil || !s.IsDir() {
|
|
|
|
|
return f.formatFile(path, diags, bytesModified)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileInfos, err := os.ReadDir(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
diag := &hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: "Cannot read hcl directory",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
}
|
|
|
|
|
diags = append(diags, diag)
|
|
|
|
|
return bytesModified, diags
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
|
filename := filepath.Join(path, fileInfo.Name())
|
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
|
if f.Recursive {
|
|
|
|
|
var tempDiags hcl.Diagnostics
|
|
|
|
|
var tempBytesModified int
|
|
|
|
|
tempBytesModified, tempDiags = f.Format(filename)
|
|
|
|
|
bytesModified += tempBytesModified
|
|
|
|
|
diags = diags.Extend(tempDiags)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if isHcl2FileOrVarFile(filename) {
|
|
|
|
|
bytesModified, diags = f.formatFile(filename, diags, bytesModified)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bytesModified, diags
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FormatNew all HCL2 files in path and return the total bytes formatted.
|
|
|
|
|
// If any error is encountered, zero bytes will be returned.
|
|
|
|
|
//
|
|
|
|
|
// Path can be a directory or a file.
|
|
|
|
|
func (f *HCL2Formatter) FormatNew(paths []string) (int, hcl.Diagnostics) {
|
|
|
|
|
func (f *HCL2Formatter) Format(paths []string) (int, hcl.Diagnostics) {
|
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
var bytesModified int
|
|
|
|
|
|
|
|
|
|
@ -121,32 +65,9 @@ func (f *HCL2Formatter) FormatNew(paths []string) (int, hcl.Diagnostics) {
|
|
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
|
s, err := os.Stat(path)
|
|
|
|
|
/*if err != nil {
|
|
|
|
|
diag := &hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: fmt.Sprintf("No file or directory at %s", path),
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
}
|
|
|
|
|
diags = append(diags, diag)
|
|
|
|
|
return 0, diags
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
if err != nil || !s.IsDir() {
|
|
|
|
|
bytesModified, diags = f.formatFile(path, diags, bytesModified)
|
|
|
|
|
/*formatted := false
|
|
|
|
|
if isHcl2FileOrVarFile(path) {
|
|
|
|
|
bytesModified, diags = f.formatFile(path, diags, bytesModified)
|
|
|
|
|
|
|
|
|
|
formatted = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !formatted {
|
|
|
|
|
diag := &hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: "Only .pkr.hcl and .pkrvars.hcl files can be processed with packer fmt",
|
|
|
|
|
}
|
|
|
|
|
diags = append(diags, diag)
|
|
|
|
|
}*/
|
|
|
|
|
} else {
|
|
|
|
|
fileInfos, err := os.ReadDir(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -171,7 +92,7 @@ func (f *HCL2Formatter) FormatNew(paths []string) (int, hcl.Diagnostics) {
|
|
|
|
|
var tempBytesModified int
|
|
|
|
|
var newPaths []string
|
|
|
|
|
newPaths = append(newPaths, filename)
|
|
|
|
|
tempBytesModified, tempDiags = f.FormatNew(newPaths)
|
|
|
|
|
tempBytesModified, tempDiags = f.Format(newPaths)
|
|
|
|
|
bytesModified += tempBytesModified
|
|
|
|
|
diags = diags.Extend(tempDiags)
|
|
|
|
|
}
|
|
|
|
|
|