1
0
mirror of https://github.com/ko-build/ko.git synced 2025-01-20 18:28:32 +02:00

Add entrypoint to PATH (#114)

This makes it easier to invoke the binary when using a debug container.
E.g. you could invoke `ko` instead of `/ko-app/ko`.
This commit is contained in:
jonjohnsonjr 2019-12-11 11:08:26 -08:00 committed by GitHub
parent 28f239ab78
commit 4ff72e36de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
gb "go/build"
"io"
"io/ioutil"
@ -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"
@ -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)
}

View File

@ -21,6 +21,7 @@ import (
"io/ioutil"
"path"
"path/filepath"
"strings"
"testing"
"time"
@ -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.")
}
})