From 9e2a9ec1dfcac9130864d58c1bbf643877ee04af Mon Sep 17 00:00:00 2001 From: anshul sharma Date: Fri, 25 Apr 2025 09:55:58 +0530 Subject: [PATCH] removed not required code --- command/fmt.go | 6 +-- hcl2template/formatter.go | 83 +-------------------------------------- 2 files changed, 4 insertions(+), 85 deletions(-) diff --git a/command/fmt.go b/command/fmt.go index b04310ad2..001eee0bd 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -40,8 +40,7 @@ func (c *FormatCommand) ParseArgs(args []string) (*FormatArgs, int) { flags.Usage() return &cfg, 1 } - - //cfg.Path = args[0] + cfg.Paths = args return &cfg, 0 } @@ -58,8 +57,7 @@ func (c *FormatCommand) RunContext(ctx context.Context, cla *FormatArgs) int { Recursive: cla.Recursive, } - //bytesModified, diags := formatter.Format(cla.Path) - bytesModified, diags := formatter.FormatNew(cla.Paths) + bytesModified, diags := formatter.Format(cla.Paths) ret := writeDiags(c.Ui, nil, diags) if ret != 0 { return ret diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index 7680a2817..a4d34e26e 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -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) }