Skip to content

Commit

Permalink
Adding logic to clear terminal screen when running new plan (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayyush authored and msarvar committed Sep 27, 2021
1 parent 3e672ad commit 1f247f9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions server/controllers/logstreaming_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gorilla/websocket"
"github.com/runatlantis/atlantis/server/controllers/templates"
"github.com/runatlantis/atlantis/server/core/db"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/handlers"
"github.com/runatlantis/atlantis/server/logging"
)
Expand Down Expand Up @@ -100,6 +101,7 @@ func (j *JobsController) GetProjectJobs(w http.ResponseWriter, r *http.Request)
AtlantisVersion: j.AtlantisVersion,
ProjectPath: projectInfo.String(),
CleanedBasePath: j.AtlantisURL.Path,
ClearMsg: models.LogStreamingClearMsg,
}

err = j.ProjectJobsTemplate.Execute(w, viewData)
Expand Down
8 changes: 8 additions & 0 deletions server/controllers/templates/web_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ type ProjectJobData struct {
AtlantisVersion string
ProjectPath string
CleanedBasePath string
ClearMsg string
}

var ProjectJobsTemplate = template.Must(template.New("blank.html.tmpl").Parse(`
Expand Down Expand Up @@ -413,6 +414,13 @@ var ProjectJobsTemplate = template.Must(template.New("blank.html.tmpl").Parse(`
document.location.host +
document.location.pathname +
"/ws");
socket.onmessage = function(event) {
var msg = String.fromCharCode.apply(null, new Uint8Array(event.data))
if (msg.trim() === "-----Starting New Process-----") {
term.clear()
return
}
}
var attachAddon = new AttachAddon.AttachAddon(socket);
var fitAddon = new FitAddon.FitAddon();
term.loadAddon(attachAddon);
Expand Down
1 change: 1 addition & 0 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

const (
planfileSlashReplace = "::"
LogStreamingClearMsg = "\t\n-----Starting New Process-----\n"
)

type PullReqStatus struct {
Expand Down
8 changes: 6 additions & 2 deletions server/handlers/project_command_output_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (p *AsyncProjectCommandOutputHandler) Handle() {
for msg := range p.projectCmdOutput {
if msg.ClearBuffBefore {
p.clearLogLines(msg.ProjectInfo)
continue
}
p.writeLogLine(msg.ProjectInfo, msg.Line)
}
Expand All @@ -119,8 +118,8 @@ func (p *AsyncProjectCommandOutputHandler) Handle() {
func (p *AsyncProjectCommandOutputHandler) Clear(ctx models.ProjectCommandContext) {
p.projectCmdOutput <- &models.ProjectCmdOutputLine{
ProjectInfo: ctx.PullInfo(),
Line: models.LogStreamingClearMsg,
ClearBuffBefore: true,
Line: "",
}
}

Expand Down Expand Up @@ -168,6 +167,11 @@ func (p *AsyncProjectCommandOutputHandler) writeLogLine(pull string, line string
}
p.receiverBuffersLock.Unlock()

// No need to write to projectOutputBuffers if clear msg.
if line == models.LogStreamingClearMsg {
return
}

p.projectOutputBuffersLock.Lock()
if p.projectOutputBuffers[pull] == nil {
p.projectOutputBuffers[pull] = []string{}
Expand Down
13 changes: 10 additions & 3 deletions server/handlers/project_command_output_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestProjectCommandOutputHandler(t *testing.T) {
}()

projectOutputHandler.Send(ctx, Msg)
close(ch)

// Wait for the msg to be read.
wg.Wait()
Expand Down Expand Up @@ -114,6 +115,7 @@ func TestProjectCommandOutputHandler(t *testing.T) {

// Send a clear msg
projectOutputHandler.Clear(ctx)
close(ch)

dfProjectOutputHandler, ok := projectOutputHandler.(*handlers.AsyncProjectCommandOutputHandler)
assert.True(t, ok)
Expand Down Expand Up @@ -183,10 +185,10 @@ func TestProjectCommandOutputHandler(t *testing.T) {
// Expecting two calls to callback.
wg.Add(2)

expectedMsg := []string{}
receivedMsgs := []string{}
go func() {
err := projectOutputHandler.Receive(ctx.PullInfo(), ch, func(msg string) error {
expectedMsg = append(expectedMsg, msg)
receivedMsgs = append(receivedMsgs, msg)
wg.Done()
return nil
})
Expand All @@ -201,7 +203,12 @@ func TestProjectCommandOutputHandler(t *testing.T) {
// Wait for the message to be read.
wg.Wait()
close(ch)
assert.Equal(t, []string{Msg, Msg}, expectedMsg)

expectedMsgs := []string{Msg, Msg}
assert.Equal(t, len(expectedMsgs), len(receivedMsgs))
for i := range expectedMsgs {
assert.Equal(t, expectedMsgs[i], receivedMsgs[i])
}
})

t.Run("update project status with project jobs url", func(t *testing.T) {
Expand Down

0 comments on commit 1f247f9

Please sign in to comment.