From 75e2c46295b10f515757befb5bc02985df4c9498 Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Tue, 4 Apr 2023 18:20:19 +0300 Subject: [PATCH 1/3] build(cli): allow passing in-stream using command.Cli Use command.Cli::In() which is always initialized either to os.Stdin, or to user-specified stream: * https://github.com/docker/cli/blob/5be21394cbbafc5627bb3e8a54014c108a534b52/cli/command/cli.go#L494 * https://github.com/docker/cli/blob/master/cli/command/cli_options.go#L16C1-L26 Signed-off-by: Timofey Kirillov --- commands/build.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/build.go b/commands/build.go index 15355ce72..34ca483f0 100644 --- a/commands/build.go +++ b/commands/build.go @@ -312,7 +312,7 @@ func getImageID(resp map[string]string) string { } func runBasicBuild(ctx context.Context, dockerCli command.Cli, opts *controllerapi.BuildOptions, options buildOptions, printer *progress.Printer) (*client.SolveResponse, error) { - resp, res, err := cbuild.RunBuild(ctx, dockerCli, *opts, os.Stdin, printer, false) + resp, res, err := cbuild.RunBuild(ctx, dockerCli, *opts, dockerCli.In(), printer, false) if res != nil { res.Done() } @@ -346,7 +346,7 @@ func runControllerBuild(ctx context.Context, dockerCli command.Cli, opts *contro var retErr error var resp *client.SolveResponse f := ioset.NewSingleForwarder() - f.SetReader(os.Stdin) + f.SetReader(dockerCli.In()) if !options.noBuild { pr, pw := io.Pipe() f.SetWriter(pw, func() io.WriteCloser { From 14aebe713eb07613776f71ef0fec585a4e41032b Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Fri, 9 Jun 2023 10:50:35 +0100 Subject: [PATCH 2/3] debug-shell(cli): allow passing in-stream using command.Cli Signed-off-by: Justin Chadwell --- commands/debug-shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/debug-shell.go b/commands/debug-shell.go index 0b091572d..6b814094f 100644 --- a/commands/debug-shell.go +++ b/commands/debug-shell.go @@ -47,7 +47,7 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command { err = monitor.RunMonitor(ctx, "", nil, controllerapi.InvokeConfig{ Tty: true, - }, c, os.Stdin, os.Stdout, os.Stderr, printer) + }, c, dockerCli.In(), os.Stdout, os.Stderr, printer) con.Reset() return err }, From f5f00e68ef41b2305e902d7e2f405983376470e5 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Fri, 9 Jun 2023 10:50:51 +0100 Subject: [PATCH 3/3] bake(cli): allow passing in-stream using command.Cli ReadLocalFiles should allow passing the stdin file as an argument, which allows us to read from dockerCli.Stdin() to be consistent with other commands in the same package. Signed-off-by: Justin Chadwell --- bake/bake.go | 4 ++-- bake/bake_test.go | 2 +- commands/bake.go | 2 +- util/cobrautil/completion/completion.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 9a507cd6e..39cd927eb 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -55,7 +55,7 @@ func defaultFilenames() []string { return names } -func ReadLocalFiles(names []string) ([]File, error) { +func ReadLocalFiles(names []string, stdin io.Reader) ([]File, error) { isDefault := false if len(names) == 0 { isDefault = true @@ -67,7 +67,7 @@ func ReadLocalFiles(names []string) ([]File, error) { var dt []byte var err error if n == "-" { - dt, err = io.ReadAll(os.Stdin) + dt, err = io.ReadAll(stdin) if err != nil { return nil, err } diff --git a/bake/bake_test.go b/bake/bake_test.go index f3341c06d..efebea204 100644 --- a/bake/bake_test.go +++ b/bake/bake_test.go @@ -1398,7 +1398,7 @@ func TestReadLocalFilesDefault(t *testing.T) { for _, tf := range tt.filenames { require.NoError(t, os.WriteFile(tf, []byte(tf), 0644)) } - files, err := ReadLocalFiles(nil) + files, err := ReadLocalFiles(nil, nil) require.NoError(t, err) if len(files) == 0 { require.Equal(t, len(tt.expected), len(files)) diff --git a/commands/bake.go b/commands/bake.go index 8edd45e6f..574a68741 100644 --- a/commands/bake.go +++ b/commands/bake.go @@ -146,7 +146,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com if url != "" { files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer) } else { - files, err = bake.ReadLocalFiles(in.files) + files, err = bake.ReadLocalFiles(in.files, dockerCli.In()) } if err != nil { return err diff --git a/util/cobrautil/completion/completion.go b/util/cobrautil/completion/completion.go index 928be1740..607545378 100644 --- a/util/cobrautil/completion/completion.go +++ b/util/cobrautil/completion/completion.go @@ -19,7 +19,7 @@ func Disable(cmd *cobra.Command, args []string, toComplete string) ([]string, co func BakeTargets(files []string) ValidArgsFn { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - f, err := bake.ReadLocalFiles(files) + f, err := bake.ReadLocalFiles(files, nil) if err != nil { return nil, cobra.ShellCompDirectiveError }