diff --git a/main.go b/main.go index aa37c62..d44add1 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "github.com/charmbracelet/bubbles/list" @@ -10,6 +11,15 @@ import ( "periodic/prddeploy/commands" ) +type sshExecCmd struct { + run func() +} + +func (c *sshExecCmd) Run() error { c.run(); return nil } +func (c *sshExecCmd) SetStdin(_ io.Reader) {} +func (c *sshExecCmd) SetStdout(_ io.Writer) {} +func (c *sshExecCmd) SetStderr(_ io.Writer) {} + type appState int const ( @@ -117,11 +127,33 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } if m.form.State == huh.StateCompleted { m.ReadForm() - return m, tea.Quit + var execCmd tea.ExecCommand + if m.choice == 0 { + args := commands.RunCommandArgs{ + RemoteCommand: m.RemoteCommand, + PrivateKeyFile: m.PrivateKeyFile, + Username: m.Username, + Host: m.Host, + } + execCmd = &sshExecCmd{run: func() { commands.RunCommand(args) }} + } else { + args := commands.UploadFileArgs{ + LocalFilepath: m.LocalFilepath, + DestinationFilepath: m.DestinationFilepath, + PrivateKeyFile: m.PrivateKeyFile, + Username: m.Username, + Host: m.Host, + } + execCmd = &sshExecCmd{run: func() { commands.UploadFile(args) }} + } + m.state = stateList + m.form = nil + return m, tea.Exec(execCmd, func(err error) tea.Msg { return nil }) } if m.form.State == huh.StateAborted { - m.ReadForm() - return m, tea.Quit + m.state = stateList + m.form = nil + return m, nil } return m, cmd } @@ -141,33 +173,8 @@ func (m model) View() string { func main() { p := tea.NewProgram(newModel(), tea.WithAltScreen()) - result, err := p.Run() - if err != nil { + if _, err := p.Run(); err != nil { fmt.Fprintf(os.Stderr, "Error running program: %v\n", err) os.Exit(1) } - - m, ok := result.(model) - if !ok || m.form == nil || m.form.State != huh.StateCompleted { - return - } - - m.DebugState() - - if m.choice == 0 { - commands.RunCommand(commands.RunCommandArgs{ - RemoteCommand: m.RemoteCommand, - PrivateKeyFile: m.PrivateKeyFile, - Username: m.Username, - Host: m.Host, - }) - } else { - commands.UploadFile(commands.UploadFileArgs{ - LocalFilepath: m.LocalFilepath, - DestinationFilepath: m.DestinationFilepath, - PrivateKeyFile: m.PrivateKeyFile, - Username: m.Username, - Host: m.Host, - }) - } }