From 4323b491304540e721d026dbb5627154d4865cf9 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 19 Jan 2021 15:30:34 -0800 Subject: [PATCH 1/3] enable Packer fmt to read from stdin --- command/fmt.go | 7 +++-- hcl2template/formatter.go | 60 ++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/command/fmt.go b/command/fmt.go index 822d5b3a1..5b15ed772 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -75,8 +75,11 @@ Usage: packer fmt [options] [TEMPLATE] configuration files (.pkr.hcl) and variable files (.pkrvars.hcl) are updated. JSON files (.json) are not modified. - If TEMPATE is "." the current directory will be used. The given content must - be in Packer's HCL2 configuration language; JSON is not supported. + If TEMPATE is "." the current directory will be used. + If TEMPATE is "-" then content will be read from STDIN. + + The given content must be in Packer's HCL2 configuration language; JSON is + not supported. Options: -check Check if the input is formatted. Exit status will be 0 if all diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index e6b1c6634..e83974a15 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -31,25 +31,33 @@ func NewHCL2Formatter() *HCL2Formatter { // // Path can be a directory or a file. func (f *HCL2Formatter) Format(path string) (int, hcl.Diagnostics) { - hclFiles, _, diags := GetHCL2Files(path, hcl2FileExt, hcl2JsonFileExt) - if diags.HasErrors() { - return 0, diags - } - hclVarFiles, _, diags := GetHCL2Files(path, hcl2VarFileExt, hcl2VarJsonFileExt) - if diags.HasErrors() { - return 0, diags - } + var allHclFiles []string + var diags []*hcl.Diagnostic + + if path == "-" { + allHclFiles = []string{"-"} + } else { + hclFiles, _, diags := GetHCL2Files(path, hcl2FileExt, hcl2JsonFileExt) + if diags.HasErrors() { + return 0, diags + } + + hclVarFiles, _, diags := GetHCL2Files(path, hcl2VarFileExt, hcl2VarJsonFileExt) + if diags.HasErrors() { + return 0, diags + } - allHclFiles := append(hclFiles, hclVarFiles...) + allHclFiles = append(hclFiles, hclVarFiles...) - if len(allHclFiles) == 0 { - diags = append(diags, &hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: fmt.Sprintf("Cannot tell whether %s contains HCL2 configuration data", path), - }) + if len(allHclFiles) == 0 { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: fmt.Sprintf("Cannot tell whether %s contains HCL2 configuration data", path), + }) - return 0, diags + return 0, diags + } } if f.parser == nil { @@ -80,9 +88,17 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) { f.Output = os.Stdout } - in, err := os.Open(filename) - if err != nil { - return nil, fmt.Errorf("failed to open %s: %s", filename, err) + var in io.Reader + var err error + + if filename == "-" { + in = os.Stdin + f.ShowDiff = false + } else { + in, err = os.Open(filename) + if err != nil { + return nil, fmt.Errorf("failed to open %s: %s", filename, err) + } } inSrc, err := ioutil.ReadAll(in) @@ -105,8 +121,12 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) { _, _ = f.Output.Write(s) if f.Write { - if err := ioutil.WriteFile(filename, outSrc, 0644); err != nil { - return nil, err + if filename == "-" { + f.Output.Write(outSrc) + } else { + if err := ioutil.WriteFile(filename, outSrc, 0644); err != nil { + return nil, err + } } } From 40df74e95afca30ba0aa8dbfa50cfd9fdad05d88 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 19 Jan 2021 15:34:18 -0800 Subject: [PATCH 2/3] linting --- hcl2template/formatter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index e83974a15..6a3db089b 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -122,7 +122,7 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) { if f.Write { if filename == "-" { - f.Output.Write(outSrc) + _, _ = f.Output.Write(outSrc) } else { if err := ioutil.WriteFile(filename, outSrc, 0644); err != nil { return nil, err From 2b0996daa6acff5e6416857bd934a2f6fbaebb01 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 20 Jan 2021 11:30:50 -0800 Subject: [PATCH 3/3] dont print filename if its reading from stdin --- hcl2template/formatter.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index 6a3db089b..d02d421a4 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -117,8 +117,10 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) { return nil, nil } - s := []byte(fmt.Sprintf("%s\n", filename)) - _, _ = f.Output.Write(s) + if filename != "-" { + s := []byte(fmt.Sprintf("%s\n", filename)) + _, _ = f.Output.Write(s) + } if f.Write { if filename == "-" {