From 3d1e07dcf3d7fb4555e2a21af4e69901bdb543d9 Mon Sep 17 00:00:00 2001 From: Brandon Croft Date: Fri, 18 Aug 2023 06:53:25 -0600 Subject: [PATCH] Write command output to Meta.Streams, more plugin err handling --- .../cloudplugin/cloudplugin1/grpc_client.go | 10 ++++++-- internal/command/cli_ui.go | 2 +- internal/command/cloud.go | 24 +------------------ 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/internal/cloudplugin/cloudplugin1/grpc_client.go b/internal/cloudplugin/cloudplugin1/grpc_client.go index 5315b6e16d..89d50e4231 100644 --- a/internal/cloudplugin/cloudplugin1/grpc_client.go +++ b/internal/cloudplugin/cloudplugin1/grpc_client.go @@ -44,17 +44,23 @@ func (c GRPCCloudClient) Execute(args []string, stdout, stderr io.Writer) int { } if bytes := response.GetStdout(); len(bytes) > 0 { - _, err := fmt.Fprint(stdout, string(bytes)) + written, err := fmt.Fprint(stdout, string(bytes)) if err != nil { log.Printf("[ERROR] Failed to write cloudplugin output to stdout: %s", err) return 1 } + if written != len(bytes) { + log.Printf("[ERROR] Wrote %d bytes to stdout but expected to write %d", written, len(bytes)) + } } else if bytes := response.GetStderr(); len(bytes) > 0 { - fmt.Fprint(stderr, string(bytes)) + written, err := fmt.Fprint(stderr, string(bytes)) if err != nil { log.Printf("[ERROR] Failed to write cloudplugin output to stderr: %s", err) return 1 } + if written != len(bytes) { + log.Printf("[ERROR] Wrote %d bytes to stdout but expected to write %d", written, len(bytes)) + } } else { exitCode := response.GetExitCode() log.Printf("[TRACE] received exit code: %d", exitCode) diff --git a/internal/command/cli_ui.go b/internal/command/cli_ui.go index 1234bcd51f..1021fff094 100644 --- a/internal/command/cli_ui.go +++ b/internal/command/cli_ui.go @@ -10,7 +10,7 @@ import ( "github.com/mitchellh/colorstring" ) -// ColoredUi is a Ui implementation that colors its output according +// ColorizeUi is a Ui implementation that colors its output according // to the given color schemes for the given type of output. type ColorizeUi struct { Colorize *colorstring.Colorize diff --git a/internal/command/cloud.go b/internal/command/cloud.go index 94bdba5aca..ad4a1c397b 100644 --- a/internal/command/cloud.go +++ b/internal/command/cloud.go @@ -11,7 +11,6 @@ import ( "os/exec" "path" "runtime" - "strings" "github.com/hashicorp/go-plugin" svchost "github.com/hashicorp/terraform-svchost" @@ -21,7 +20,6 @@ import ( "github.com/hashicorp/terraform/internal/cloudplugin/cloudplugin1" "github.com/hashicorp/terraform/internal/logging" "github.com/hashicorp/terraform/internal/tfdiags" - "github.com/mitchellh/cli" ) // CloudCommand is a Command implementation that interacts with Terraform @@ -225,30 +223,10 @@ func (c *CloudCommand) initPackagesCache() (string, error) { return packagesPath, nil } -type proxyOutput struct { - io.Writer - upstream cli.Ui -} - -func (p proxyOutput) Write(data []byte) (int, error) { - p.upstream.Output(strings.TrimSuffix(string(data), "\n")) - return len(data), nil -} - -type proxyError struct { - io.Writer - upstream cli.Ui -} - -func (p proxyError) Write(data []byte) (int, error) { - p.upstream.Error(strings.TrimSuffix(string(data), "\n")) - return len(data), nil -} - // Run runs the cloud command with the given arguments. func (c *CloudCommand) Run(args []string) int { args = c.Meta.process(args) - return c.realRun(args, proxyOutput{upstream: c.Meta.Ui}, proxyError{upstream: c.Meta.Ui}) + return c.realRun(args, c.Meta.Streams.Stdout.File, c.Meta.Streams.Stderr.File) } // Help returns help text for the cloud command.