StartTerminal opens a new terminal
( ctx devspacecontext.Context, devContainer *latest.DevContainer, selector targetselector.TargetSelector, stdout io.Writer, stderr io.Writer, stdin io.Reader, parent *tomb.Tomb, )
| 82 | |
| 83 | // StartTerminal opens a new terminal |
| 84 | func StartTerminal( |
| 85 | ctx devspacecontext.Context, |
| 86 | devContainer *latest.DevContainer, |
| 87 | selector targetselector.TargetSelector, |
| 88 | stdout io.Writer, |
| 89 | stderr io.Writer, |
| 90 | stdin io.Reader, |
| 91 | parent *tomb.Tomb, |
| 92 | ) (err error) { |
| 93 | // restart on error |
| 94 | defer func() { |
| 95 | if err != nil { |
| 96 | if ctx.IsDone() { |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | ctx.Log().Infof("Restarting because: %s", err) |
| 101 | select { |
| 102 | case <-ctx.Context().Done(): |
| 103 | return |
| 104 | case <-time.After(time.Second * 3): |
| 105 | } |
| 106 | err = StartTerminal(ctx, devContainer, selector, stdout, stderr, stdin, parent) |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | ctx.Log().Debugf("Stopped terminal") |
| 111 | }() |
| 112 | |
| 113 | command := getCommand(devContainer) |
| 114 | container, err := selector.WithContainer(devContainer.Container).SelectSingleContainer(ctx.Context(), ctx.KubeClient(), ctx.Log()) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | ctx.Log().Infof("Opening shell to %s:%s (pod:container)", ansi.Color(container.Container.Name, "white+b"), ansi.Color(container.Pod.Name, "white+b")) |
| 120 | errChan := make(chan error) |
| 121 | parent.Go(func() error { |
| 122 | errChan <- startTerminal(ctx, command, !devContainer.Terminal.DisableTTY, devContainer.Terminal.DisableScreen, "dev", stdout, stderr, stdin, container) |
| 123 | return nil |
| 124 | }) |
| 125 | |
| 126 | select { |
| 127 | case <-ctx.Context().Done(): |
| 128 | <-errChan |
| 129 | return nil |
| 130 | case err = <-errChan: |
| 131 | if ctx.IsDone() { |
| 132 | return nil |
| 133 | } |
| 134 | |
| 135 | if err != nil { |
| 136 | // check if context is done |
| 137 | if exitError, ok := err.(kubectlExec.CodeExitError); ok { |
| 138 | // Expected exit codes are (https://shapeshed.com/unix-exit-codes/): |
| 139 | // 1 - Catchall for general errors |
| 140 | // 2 - Misuse of shell builtins (according to Bash documentation) |
| 141 | // 126 - Command invoked cannot execute |
no test coverage detected