Skip to content

Commit

Permalink
[fix] Same executable, cli opts, and env to exec (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 authored Dec 6, 2023
1 parent e590a91 commit 4172951
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ var (

func execEntrypoint(cmd *cobra.Command, args []string) {
task := cmd.Flags().Lookup("task").Value.String()
client, err := getConfig(cmd, "").Client()
// can ignore storing rootOpts here as exec just needs a client
config := getConfig(cmd, []string{}, "")
client, err := config.Client()
if err != nil {
fmt.Println(fmt.Errorf("could not get client: %v", err))
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,11 @@ func bindFlags(cmd *cobra.Command, nameToArg map[string]arg) {
}

func mainEntrypoint(cmd *cobra.Command, args []string) {
initialModel, options := setup(cmd, "")
dev.Debug("~STARTING UP~")
rootOpts := getRootOpts(cmd)
initialModel, options := setup(cmd, rootOpts, "")
program := tea.NewProgram(initialModel, options...)

dev.Debug("~STARTING UP~")
if _, err := program.Run(); err != nil {
fmt.Printf("Error on wander startup: %v", err)
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ func serveEntrypoint(cmd *cobra.Command, args []string) {
}

func generateTeaHandler(cmd *cobra.Command) func(ssh.Session) (tea.Model, []tea.ProgramOption) {
changedOpts := getRootOpts(cmd.Parent())
return func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
// optionally override token - MUST run with `-t` flag to force pty, e.g. ssh -p 20000 localhost -t <token>
var overrideToken string
if sshCommands := s.Command(); len(sshCommands) == 1 {
overrideToken = strings.TrimSpace(sshCommands[0])
}
return setup(cmd, overrideToken)
return setup(cmd, changedOpts, overrideToken)
}
}
25 changes: 22 additions & 3 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/robinovitch61/wander/internal/tui/components/app"
"github.com/robinovitch61/wander/internal/tui/nomad"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"os"
Expand Down Expand Up @@ -286,7 +287,7 @@ func customLoggingMiddleware() wish.Middleware {
}
}

func getConfig(cmd *cobra.Command, overrideToken string) app.Config {
func getConfig(cmd *cobra.Command, rootOpts []string, overrideToken string) app.Config {
nomadAddr := retrieveAddress(cmd)
nomadToken := retrieveToken(cmd)
if overrideToken != "" {
Expand Down Expand Up @@ -323,6 +324,7 @@ func getConfig(cmd *cobra.Command, overrideToken string) app.Config {
filterWithContext := retrieveFilterWithContext(cmd)

return app.Config{
RootOpts: rootOpts,
Version: getVersion(),
URL: nomadAddr,
Token: nomadToken,
Expand Down Expand Up @@ -361,7 +363,24 @@ func getConfig(cmd *cobra.Command, overrideToken string) app.Config {
}
}

func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOption) {
initialModel := app.InitialModel(getConfig(cmd, overrideToken))
func getRootOpts(cmd *cobra.Command) []string {
if cmd.Name() != "wander" {
panic("getRootOpts should only be called on the root wander command, for which both serve and exec are subcommands")
}
var opts []string
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
if f.Name != "" {
opts = append(opts, fmt.Sprintf("--%s=%s", f.Name, f.Value))
} else if f.Shorthand != "" {
opts = append(opts, fmt.Sprintf("-%s=%s", f.Shorthand, f.Value))
}
}
})
return opts
}

func setup(cmd *cobra.Command, rootOpts []string, overrideToken string) (app.Model, []tea.ProgramOption) {
initialModel := app.InitialModel(getConfig(cmd, rootOpts, overrideToken))
return initialModel, []tea.ProgramOption{tea.WithAltScreen()}
}
24 changes: 23 additions & 1 deletion internal/tui/components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ 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"
"path"
"strings"
"time"
)
Expand All @@ -38,6 +40,7 @@ type LogConfig struct {
}

type Config struct {
RootOpts []string
Version string
URL, Token, Region, Namespace string
HTTPAuth string
Expand Down Expand Up @@ -266,7 +269,26 @@ 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, "--task", m.taskName, msg.Input)
// run the same wander executable even if there is a different one in the path
ex, err := os.Executable()
if err != nil {
m.err = err
return m, nil
}
dir := path.Dir(ex)

args := []string{"exec"}
// pass the same cli opts to wander exec as passed into the current wander root command
args = append(args, m.config.RootOpts...)
args = append(args, []string{
m.alloc.ID,
"--task",
m.taskName,
msg.Input,
}...)
c := exec.Command(fmt.Sprintf("%s/wander", dir), args...)
c.Env = os.Environ()

stdoutProxy := &nomad.StdoutProxy{}
c.Stdout = stdoutProxy
m.getCurrentPageModel().SetDoesNeedNewInput()
Expand Down
3 changes: 3 additions & 0 deletions internal/tui/nomad/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"os/signal"
"syscall"
"time"
)

type ExecCompleteMsg struct {
Expand Down Expand Up @@ -111,7 +112,9 @@ func execImpl(client *api.Client, alloc *api.Allocation, task string,
command []string, escapeChar string, stdin io.Reader, stdout, stderr io.WriteCloser) (int, error) {

// attempt to clear screen
time.Sleep(10 * time.Millisecond)
os.Stdout.Write([]byte("\033c"))

fmt.Println(fmt.Sprintf("Exec session for %s (%s), task %s", alloc.Name, formatter.ShortAllocID(alloc.ID), task))

sizeCh := make(chan api.TerminalSize, 1)
Expand Down

0 comments on commit 4172951

Please sign in to comment.