Skip to content

Commit

Permalink
sketch out saved output
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed Dec 1, 2023
1 parent 87a6b20 commit 0ed4007
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
22 changes: 13 additions & 9 deletions internal/tui/components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/robinovitch61/wander/internal/tui/message"
"github.com/robinovitch61/wander/internal/tui/nomad"
"github.com/robinovitch61/wander/internal/tui/style"
"os"
"os/exec"
"strings"
"time"
Expand Down Expand Up @@ -266,17 +267,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case message.PageInputReceivedMsg:
if m.currentPage == nomad.ExecPage {
c := exec.Command("wander", "exec", m.alloc.ID, m.taskName, msg.Input)
save := &saveOutput{}
c.Stdout = save
m.getCurrentPageModel().SetDoesNeedNewInput()
return m, tea.ExecProcess(c, func(err error) tea.Msg {
output, err := c.Output()
if err != nil {
dev.Debug("LEOOOO")
dev.Debug(err.Error())
return nil
}
dev.Debug("LEO")
dev.Debug(string(output))
return TmpMsg{Output: string(output)}
return TmpMsg{Output: string(save.savedOutput)}
})
}
}
Expand All @@ -291,6 +286,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

type saveOutput struct {
savedOutput []byte
}

func (so *saveOutput) Write(p []byte) (n int, err error) {
so.savedOutput = append(so.savedOutput, p...)
return os.Stdout.Write(p)
}

func (m Model) View() string {
if m.err != nil {
return fmt.Sprintf("Error: %v", m.err) + "\n\nif this seems wrong, consider opening an issue here: https://github.com/robinovitch61/wander/issues/new/choose" + "\n\nq/ctrl+c to quit"
Expand Down
16 changes: 3 additions & 13 deletions internal/tui/nomad/execapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nomad
import (
"bufio"
"context"
"errors"
"fmt"
"github.com/hashicorp/nomad/api"
"github.com/moby/term"
Expand Down Expand Up @@ -90,10 +89,7 @@ func execImpl(client *api.Client, alloc *api.Allocation, task string,
// Ctrl+C and other commands to forward to remote process.
// It returns a cleanup function that restores terminal to original mode.
func setRawTerminal(stream interface{}) (cleanup func(), err error) {
fd, isTerminal := term.GetFdInfo(stream)
if !isTerminal {
return nil, errors.New("not a terminal!!")
}
fd, _ := term.GetFdInfo(stream)

state, err := term.SetRawTerminal(fd)
if err != nil {
Expand All @@ -109,10 +105,7 @@ func setRawTerminal(stream interface{}) (cleanup func(), err error) {
// so it disables LF -> CRLF translation.
// It's basically a no-op on unix.
func setRawTerminalOutput(stream interface{}) (cleanup func(), err error) {
fd, isTerminal := term.GetFdInfo(stream)
if !isTerminal {
return nil, errors.New("not a terminal!!!")
}
fd, _ := term.GetFdInfo(stream)

state, err := term.SetRawTerminalOutput(fd)
//_, err = term.SetRawTerminalOutput(fd)
Expand All @@ -127,10 +120,7 @@ func setRawTerminalOutput(stream interface{}) (cleanup func(), err error) {

// watchTerminalSize watches terminal size changes to propagate to remote tty.
func watchTerminalSize(out io.Writer, resize chan<- api.TerminalSize) (func(), error) {
fd, isTerminal := term.GetFdInfo(out)
if !isTerminal {
return nil, errors.New("not a terminal!")
}
fd, _ := term.GetFdInfo(out)

ctx, cancel := context.WithCancel(context.Background())

Expand Down

0 comments on commit 0ed4007

Please sign in to comment.