Skip to content

Commit

Permalink
Removing tf show output when streaming logs (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayyush authored Sep 16, 2021
1 parent c7cd0ad commit e29d7ef
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions server/events/terraform/terraform_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e29d7ef

Please sign in to comment.