From e29d7efb91ae084e583915e0dec7f5ceb72b73df Mon Sep 17 00:00:00 2001 From: Aayush Gupta <43479002+Aayyush@users.noreply.github.com> Date: Thu, 16 Sep 2021 10:07:27 -0700 Subject: [PATCH] Removing tf show output when streaming logs (#104) --- server/events/terraform/terraform_client.go | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/server/events/terraform/terraform_client.go b/server/events/terraform/terraform_client.go index d453d0349..9111ac8eb 100644 --- a/server/events/terraform/terraform_client.go +++ b/server/events/terraform/terraform_client.go @@ -38,6 +38,8 @@ import ( "github.com/runatlantis/atlantis/server/logging" ) +var LogStreamingValidCmds = [...]string{"init", "plan", "apply"} + //go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_terraform_client.go Client type Client interface { @@ -440,21 +442,28 @@ func (c *DefaultClient) RunCommandAsync(ctx models.ProjectCommandContext, path s // Asynchronously copy from stdout/err to outCh. go func() { + // Don't stream terraform show output to outCh + cmds := strings.Split(tfCmd, " ") c.projectCmdOutputHandler.Send(ctx, fmt.Sprintf("\n----- running terraform %s -----\n", args[0])) s := bufio.NewScanner(stdout) for s.Scan() { message := s.Text() outCh <- Line{Line: message} - c.projectCmdOutputHandler.Send(ctx, message) + if isValidCommand(cmds[1]) { + c.projectCmdOutputHandler.Send(ctx, message) + } } wg.Done() }() go func() { + cmds := strings.Split(tfCmd, " ") s := bufio.NewScanner(stderr) for s.Scan() { message := s.Text() outCh <- Line{Line: message} - c.projectCmdOutputHandler.Send(ctx, message) + if isValidCommand(cmds[1]) { + c.projectCmdOutputHandler.Send(ctx, message) + } } wg.Done() }() @@ -557,6 +566,15 @@ func generateRCFile(tfeToken string, tfeHostname string, home string) error { return nil } +func isValidCommand(cmd string) bool { + for _, validCmd := range LogStreamingValidCmds { + if validCmd == cmd { + return true + } + } + return false +} + func getVersion(tfBinary string) (*version.Version, error) { versionOutBytes, err := exec.Command(tfBinary, "version").Output() // #nosec versionOutput := string(versionOutBytes)