Merge pull request #10500 from hashicorp/format_stdin

enable Packer fmt to read from stdin
pull/10504/head
Megan Marsh 5 years ago committed by GitHub
commit 93008045cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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
allHclFiles := append(hclFiles, hclVarFiles...)
if path == "-" {
allHclFiles = []string{"-"}
} else {
hclFiles, _, diags := GetHCL2Files(path, hcl2FileExt, hcl2JsonFileExt)
if diags.HasErrors() {
return 0, diags
}
if len(allHclFiles) == 0 {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Cannot tell whether %s contains HCL2 configuration data", path),
})
hclVarFiles, _, diags := GetHCL2Files(path, hcl2VarFileExt, hcl2VarJsonFileExt)
if diags.HasErrors() {
return 0, diags
}
allHclFiles = append(hclFiles, hclVarFiles...)
return 0, diags
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
}
}
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)
@ -101,12 +117,18 @@ 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 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
}
}
}

Loading…
Cancel
Save