...
1 package kustomize
2
3 import (
4 "fmt"
5
6 "github.com/bazelbuild/bazel-gazelle/config"
7 "github.com/bazelbuild/bazel-gazelle/label"
8 "github.com/bazelbuild/bazel-gazelle/repo"
9 "github.com/bazelbuild/bazel-gazelle/resolve"
10 "github.com/bazelbuild/bazel-gazelle/rule"
11
12 container "edge-infra.dev/hack/build/rules/container/gazelle/language"
13 )
14
15
16
17
18
19
20
21
22
23
24
25 func (k *Kustomize) Imports(_ *config.Config, _ *rule.Rule, f *rule.File) []resolve.ImportSpec {
26 return []resolve.ImportSpec{{
27 Lang: kustomizationLangName,
28 Imp: f.Pkg,
29 }}
30 }
31
32 func (k *Kustomize) Resolve(c *config.Config, ix *resolve.RuleIndex, _ *repo.RemoteCache, r *rule.Rule, imports interface{}, _ label.Label) {
33 specs, ok := imports.([]resolve.ImportSpec)
34
35 if !ok {
36 fmt.Println("failed to assert type ImportSpec from imports")
37 return
38 } else if len(specs) == 0 {
39 return
40 }
41
42
43 images := make(map[string]string)
44
45
46
47
48
49 depStrings := []string{}
50 for _, spec := range specs {
51 matches := ix.FindRulesByImportWithConfig(c, spec, spec.Lang)
52 switch len(matches) {
53 case 0:
54 fmt.Printf("no Rules matching %s were found\n", spec.Imp)
55 fmt.Printf("consider running 'bazel run %s %s' to resolve\n", gazellePath, spec.Imp)
56 continue
57 case 1:
58 m := matches[0]
59 rel := label.New("", m.Label.Pkg, m.Label.Name)
60
61
62
63 switch spec.Lang {
64 case container.ContainerLangName:
65 images[rel.String()] = spec.Imp
66 continue
67 case kustomizationLangName:
68 depStrings = append(depStrings, rel.String())
69 continue
70 }
71 default:
72 fmt.Println("somehow more than 1")
73 }
74
75
76 if len(matches) > 1 {
77 fmt.Printf("multiple possible matches for %s, ignoring", spec.Imp)
78 continue
79 }
80 }
81
82 if len(images) > 0 {
83 r.SetAttr(imgsAttr, images)
84 }
85 r.DelAttr("deps")
86 r.SetAttr("deps", depStrings)
87 }
88
View as plain text