From 56ad8542d43aee77420adeda37c7b0a45d068561 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Wed, 30 Oct 2024 07:11:21 +0200 Subject: [PATCH 1/3] fix(loader): Add fix for stdin config read Signed-off-by: Suleiman Dibirov --- loader/loader.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/loader/loader.go b/loader/loader.go index 62030f23..8fb95088 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -318,6 +318,13 @@ func LoadConfigFiles(ctx context.Context, configFiles []string, workingDir strin opts.ResourceLoaders = append(opts.ResourceLoaders, localResourceLoader{}) for i, p := range configFiles { + if p == "-" { + config.ConfigFiles[i] = types.ConfigFile{ + Filename: p, + } + continue + } + for _, loader := range opts.ResourceLoaders { _, isLocalResourceLoader := loader.(localResourceLoader) if !loader.Accept(p) { From b377c302c180c731138b295ad407a4cfa2c98bf7 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Wed, 30 Oct 2024 07:22:20 +0200 Subject: [PATCH 2/3] Add IsStdin() to ConfigFile Signed-off-by: Suleiman Dibirov --- cli/options.go | 2 +- types/config.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/options.go b/cli/options.go index b2ab60e8..caa07666 100644 --- a/cli/options.go +++ b/cli/options.go @@ -414,7 +414,7 @@ func (o *ProjectOptions) ReadConfigFiles(ctx context.Context, workingDir string, for i, c := range config.ConfigFiles { var err error var b []byte - if c.Filename == "-" { + if c.IsStdin() { b, err = io.ReadAll(os.Stdin) if err != nil { return nil, err diff --git a/types/config.go b/types/config.go index a2382fb3..d73d2b9f 100644 --- a/types/config.go +++ b/types/config.go @@ -67,6 +67,10 @@ type ConfigFile struct { Config map[string]interface{} } +func (cf ConfigFile) IsStdin() bool { + return cf.Filename == "-" +} + func ToConfigFiles(path []string) (f []ConfigFile) { for _, p := range path { f = append(f, ConfigFile{Filename: p}) From 08fef259a3b0275abc6092e0913c2db87b3f1188 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Wed, 30 Oct 2024 07:30:23 +0200 Subject: [PATCH 3/3] Add test to options_test.go Signed-off-by: Suleiman Dibirov --- cli/options_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cli/options_test.go b/cli/options_test.go index 31edf9b9..5dbba5d6 100644 --- a/cli/options_test.go +++ b/cli/options_test.go @@ -242,6 +242,36 @@ func TestProjectComposefilesFromWorkingDir(t *testing.T) { }) } +func TestProjectComposefilesFromStdin(t *testing.T) { + composeData := ` +services: + simple: + image: nginx +` + originalStdin := os.Stdin + r, w, _ := os.Pipe() + defer func() { + os.Stdin = originalStdin + }() + + w.WriteString(composeData) + w.Close() + + os.Stdin = r + + opts, err := NewProjectOptions( + []string{ + "-", + }, WithName("my_project"), + ) + assert.NilError(t, err) + p, err := opts.LoadProject(context.TODO()) + assert.NilError(t, err) + service, err := p.GetService("simple") + assert.NilError(t, err) + assert.Equal(t, service.Image, "nginx") +} + func TestProjectWithDotEnv(t *testing.T) { wd, err := os.Getwd() assert.NilError(t, err)