Skip to content

Commit

Permalink
Add entrypoint to PATH (ko-build#114)
Browse files Browse the repository at this point in the history
This makes it easier to invoke the binary when using a debug container.
E.g. you could invoke `ko` instead of `/ko-app/ko`.
  • Loading branch information
jonjohnsonjr authored Dec 11, 2019
1 parent 28f239a commit 4ff72e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
23 changes: 23 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
gb "go/build"
"io"
"io/ioutil"
Expand Down Expand Up @@ -450,6 +451,7 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {

cfg = cfg.DeepCopy()
cfg.Config.Entrypoint = []string{appPath}
updatePath(cfg, appPath)
cfg.Config.Env = append(cfg.Config.Env, "KO_DATA_PATH="+kodataRoot)
cfg.Author = "github.com/google/ko"

Expand All @@ -464,3 +466,24 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {
}
return image, nil
}

// Append appPath to the PATH environment variable, if it exists. Otherwise,
// set the PATH environment variable to appPath.
func updatePath(cf *v1.ConfigFile, appPath string) {
for i, env := range cf.Config.Env {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
// Expect environment variables to be in the form KEY=VALUE, so this is unexpected.
continue
}
key, value := parts[0], parts[1]
if key == "PATH" {
value = fmt.Sprintf("%s:%s", value, appPath)
cf.Config.Env[i] = "PATH=" + value
return
}
}

// If we get here, we never saw PATH.
cf.Config.Env = append(cf.Config.Env, "PATH="+appPath)
}
20 changes: 19 additions & 1 deletion pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"path"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -336,7 +337,24 @@ func TestGoBuild(t *testing.T) {
}
}
if !found {
t.Error("Didn't find expected file in tarball.")
t.Error("Didn't find KO_DATA_PATH.")
}
})

// Check that PATH contains the produced binary.
t.Run("check PATH env var", func(t *testing.T) {
cfg, err := img.ConfigFile()
if err != nil {
t.Errorf("ConfigFile() = %v", err)
}
found := false
for _, entry := range cfg.Config.Env {
if strings.HasPrefix(entry, "PATH=") && strings.Contains(entry, "/ko-app/test") {
found = true
}
}
if !found {
t.Error("Didn't find entrypoint in PATH.")
}
})

Expand Down

0 comments on commit 4ff72e3

Please sign in to comment.