diff --git a/command/cli.go b/command/cli.go index ebe2a2b83..fa0797392 100644 --- a/command/cli.go +++ b/command/cli.go @@ -66,6 +66,7 @@ type MetaArgs struct { // TODO(azr): in the future, I want to allow passing multiple path to // merge HCL confs together; but this will probably need an RFC first. Path string + Paths []string Only, Except []string Vars map[string]string VarFiles []string diff --git a/command/fmt.go b/command/fmt.go index 3a70bcf18..359464518 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -36,12 +36,13 @@ func (c *FormatCommand) ParseArgs(args []string) (*FormatArgs, int) { } args = flags.Args() - if len(args) != 1 { + /*if len(args) != 1 { flags.Usage() return &cfg, 1 - } + }*/ - cfg.Path = args[0] + //cfg.Path = args[0] + cfg.Paths = args return &cfg, 0 } @@ -57,7 +58,8 @@ func (c *FormatCommand) RunContext(ctx context.Context, cla *FormatArgs) int { Recursive: cla.Recursive, } - bytesModified, diags := formatter.Format(cla.Path) + //bytesModified, diags := formatter.Format(cla.Path) + bytesModified, diags := formatter.FormatNew(cla.Paths) ret := writeDiags(c.Ui, nil, diags) if ret != 0 { return ret diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index 3835aea8b..0daf73fd8 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -107,6 +107,58 @@ func (f *HCL2Formatter) Format(path string) (int, hcl.Diagnostics) { 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) { + var diags hcl.Diagnostics + var bytesModified int + + if f.parser == nil { + f.parser = hclparse.NewParser() + } + + fmt.Println(fmt.Sprintf("1... Formatting paths %s", strings.Join(paths, " "))) + for _, path := range paths { + if s, err := os.Stat(path); err != nil || !s.IsDir() { + bytesModified, diags = f.formatFile(path, diags, bytesModified) + } else { + 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 + var newPaths []string + newPaths = append(newPaths, path) + tempBytesModified, tempDiags = f.FormatNew(newPaths) + bytesModified += tempBytesModified + diags = diags.Extend(tempDiags) + } + continue + } + if isHcl2FileOrVarFile(filename) { + bytesModified, diags = f.formatFile(filename, diags, bytesModified) + } + } + } + } + + return bytesModified, diags +} + // processFile formats the source contents of filename and return the formatted data. // overwriting the contents of the original when the f.Write is true; a diff of the changes // will be outputted if f.ShowDiff is true.