Skip to content

Commit

Permalink
tiltfile: add fuzzy matching suggestions to docker_build (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks authored Feb 19, 2019
1 parent 3c0bc2c commit 27eb3b6
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions internal/tiltfile/build_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package tiltfile

import (
"fmt"
"strings"

"github.com/docker/distribution/reference"
"github.com/schollz/closestmatch"
"github.com/windmilleng/tilt/internal/model"
)

Expand All @@ -15,13 +17,17 @@ type buildIndex struct {
taggedImages map[string]*dockerImage
nameOnlyImages map[string]*dockerImage
byTargetID map[model.TargetID]*dockerImage

consumedImageNames []string
consumedImageNameMap map[string]bool
}

func newBuildIndex() *buildIndex {
return &buildIndex{
taggedImages: make(map[string]*dockerImage),
nameOnlyImages: make(map[string]*dockerImage),
byTargetID: make(map[model.TargetID]*dockerImage),
taggedImages: make(map[string]*dockerImage),
nameOnlyImages: make(map[string]*dockerImage),
byTargetID: make(map[model.TargetID]*dockerImage),
consumedImageNameMap: make(map[string]bool),
}
}

Expand Down Expand Up @@ -63,6 +69,11 @@ func (idx *buildIndex) findBuilderByID(id model.TargetID) *dockerImage {
// Check to see if we have a build target for that image,
// and mark that build target as consumed by an larger target.
func (idx *buildIndex) findBuilderForConsumedImage(ref reference.Named) *dockerImage {
if !idx.consumedImageNameMap[ref.Name()] {
idx.consumedImageNameMap[ref.Name()] = true
idx.consumedImageNames = append(idx.consumedImageNames, ref.Name())
}

refTagged, hasTag := ref.(reference.NamedTagged)
if hasTag {
key := fmt.Sprintf("%s:%s", ref.Name(), refTagged.Tag())
Expand All @@ -85,7 +96,18 @@ func (idx *buildIndex) findBuilderForConsumedImage(ref reference.Named) *dockerI
func (idx *buildIndex) assertAllMatched() error {
for _, image := range idx.images {
if !image.matched {
return fmt.Errorf("image %v is not used in any resource", image.ref.String())
bagSizes := []int{2, 3, 4}
cm := closestmatch.New(idx.consumedImageNames, bagSizes)
matchLines := []string{}
for i, match := range cm.ClosestN(image.ref.Name(), 3) {
if i == 0 {
matchLines = append(matchLines, "Did you mean:")
}
matchLines = append(matchLines, fmt.Sprintf(" - %s", match))
}

return fmt.Errorf("image %v is not used in any resource. %s",
image.ref.String(), strings.Join(matchLines, "\n"))
}
}
return nil
Expand Down
13 changes: 13 additions & 0 deletions internal/tiltfile/tiltfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,19 @@ k8s_yaml('foo.yaml')
}, f.imageTargetNames(m))
}

func TestImageRefSuggestion(t *testing.T) {
f := newFixture(t)
defer f.TearDown()

f.setupFoo()
f.file("Tiltfile", `
docker_build('gcr.typo.io/foo', 'foo')
k8s_resource('foo', 'foo.yaml')
`)

f.loadErrString("Did you mean:\n - gcr.io/foo")
}

type fixture struct {
ctx context.Context
t *testing.T
Expand Down
4 changes: 4 additions & 0 deletions vendor/github.com/schollz/closestmatch/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/schollz/closestmatch/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/schollz/closestmatch/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions vendor/github.com/schollz/closestmatch/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 27eb3b6

Please sign in to comment.