From 6abdb30f15cff6c911fa57b61def548844eaaf33 Mon Sep 17 00:00:00 2001 From: Steve Engledow Date: Wed, 23 Jul 2025 21:12:21 +0100 Subject: [PATCH] #694 Add support for reading from stdin with '-' as a template file name --- cft/parse/parse.go | 4 ++++ cft/parse/parse_test.go | 32 +++++++++++++++++++++++++ internal/cmd/deploy/deploy.go | 29 +++++++++++++++++++--- internal/cmd/deploy/util.go | 9 ++++++- internal/cmd/diff/diff.go | 2 +- internal/cmd/fmt/fmt.go | 40 ++++++++++++++++++------------- internal/cmd/forecast/forecast.go | 2 ++ internal/cmd/merge/merge.go | 2 +- internal/cmd/tree/tree.go | 2 +- 9 files changed, 98 insertions(+), 24 deletions(-) diff --git a/cft/parse/parse.go b/cft/parse/parse.go index 162ec5811..f1dd912cb 100644 --- a/cft/parse/parse.go +++ b/cft/parse/parse.go @@ -26,6 +26,10 @@ func Reader(r io.Reader) (*cft.Template, error) { // File returns a cft.Template parsed from a file specified by fileName func File(fileName string) (*cft.Template, error) { + if fileName == "-" { + return Reader(os.Stdin) + } + source, err := os.ReadFile(fileName) if err != nil { return &cft.Template{}, fmt.Errorf("unable to read file: %s", err) diff --git a/cft/parse/parse_test.go b/cft/parse/parse_test.go index 8bd2b8dbe..a2f9625e1 100644 --- a/cft/parse/parse_test.go +++ b/cft/parse/parse_test.go @@ -132,6 +132,38 @@ func TestReadFile(t *testing.T) { } } +func TestReadFile_stdin(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Error(err) + } + + _, err = w.WriteString(testTemplate) + if err != nil { + t.Error(err) + } + + err = w.Close() + if err != nil { + t.Error(err) + } + + oldStdin := os.Stdin + defer func() { + os.Stdin = oldStdin + }() + os.Stdin = r + + actual, err := parse.File("-") + if err != nil { + t.Error(err) + } + + if diff := cmp.Diff(actual.Map(), expected.Map()); diff != "" { + t.Errorf(diff) + } +} + func TestReadString(t *testing.T) { actual, err := parse.String(testTemplate) if err != nil { diff --git a/internal/cmd/deploy/deploy.go b/internal/cmd/deploy/deploy.go index d62b92bf7..895d74018 100644 --- a/internal/cmd/deploy/deploy.go +++ b/internal/cmd/deploy/deploy.go @@ -43,6 +43,7 @@ var Cmd = &cobra.Command{ Long: `Creates or updates a CloudFormation stack named from the template file