Skip to content

Commit

Permalink
k8s: get tilt executable path (for 'tilt kubectl' calls) in a way tha…
Browse files Browse the repository at this point in the history
…t respects ~ in $PATH (#3838)
  • Loading branch information
Maia McCormick committed Oct 7, 2020
1 parent 050219f commit d212cba
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions internal/k8s/kubectl_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"os/exec"
"strings"

"github.com/pkg/errors"
)

type kubectlRunner interface {
Expand All @@ -19,12 +21,13 @@ type realKubectlRunner struct {

var _ kubectlRunner = realKubectlRunner{}

func (k realKubectlRunner) tiltPath() string {
func (k realKubectlRunner) tiltPath() (string, error) {
// TODO(nick): It might be better to dependency inject this. Right now, this
// only works if os.Args[0] is the Tilt binary. It won't work right if this
// only works if the executable is the Tilt binary. It won't work right if this
// is linked into separately compiled binaries that don't have a kubectl
// sub-command.
return os.Args[0]
path, err := os.Executable()
return path, errors.Wrap(err, "finding path of Tilt executable (for `tilt kubectl` call)")
}

func (k realKubectlRunner) prependGlobalArgs(args []string) []string {
Expand All @@ -33,7 +36,11 @@ func (k realKubectlRunner) prependGlobalArgs(args []string) []string {

func (k realKubectlRunner) exec(ctx context.Context, args []string) (stdout string, stderr string, err error) {
args = k.prependGlobalArgs(args)
c := exec.CommandContext(ctx, k.tiltPath(), args...)
tiltPath, err := k.tiltPath()
if err != nil {
return "", "", err
}
c := exec.CommandContext(ctx, tiltPath, args...)

stdoutBuf := &bytes.Buffer{}
stderrBuf := &bytes.Buffer{}
Expand All @@ -46,7 +53,11 @@ func (k realKubectlRunner) exec(ctx context.Context, args []string) (stdout stri

func (k realKubectlRunner) execWithStdin(ctx context.Context, args []string, stdin string) (stdout string, stderr string, err error) {
args = k.prependGlobalArgs(args)
c := exec.CommandContext(ctx, k.tiltPath(), args...)
tiltPath, err := k.tiltPath()
if err != nil {
return "", "", err
}
c := exec.CommandContext(ctx, tiltPath, args...)
c.Stdin = strings.NewReader(stdin)

stdoutBuf := &bytes.Buffer{}
Expand Down

0 comments on commit d212cba

Please sign in to comment.