Write command output to Meta.Streams, more plugin err handling

pull/33602/head
Brandon Croft 3 years ago
parent 8f4a430e41
commit 3d1e07dcf3
No known key found for this signature in database
GPG Key ID: B01E32423322EB9D

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

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

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

Loading…
Cancel
Save