...
1 package container
2
3 import (
4 "fmt"
5 "path/filepath"
6
7 "github.com/bazelbuild/bazel-gazelle/config"
8 "github.com/bazelbuild/bazel-gazelle/resolve"
9 "github.com/bazelbuild/bazel-gazelle/rule"
10 "github.com/bazelbuild/buildtools/build"
11 "github.com/bazelbuild/buildtools/file"
12
13 "edge-infra.dev/pkg/lib/build/bazel"
14 )
15
16 var (
17 containerBuildRelativePath = "hack/build/rules/container"
18
19 ociRepoLabels = map[string]string{}
20 )
21
22
23 func init() {
24 var wd string
25 var containerBuildPath string
26
27 if bazel.IsBazelTest() {
28 containerBuildPath = "oci_repos.txt"
29 } else {
30 wd = bazel.ResolveWdOrDie()
31 containerBuildPath = filepath.Join(wd, containerBuildRelativePath, "BUILD.bazel")
32 }
33
34 fPath := containerBuildPath
35 fileBytes, _, err := file.ReadFile(fPath)
36 if err != nil {
37 fmt.Printf("gazelle container: error getting the BUILD.bazel at %s: %s\n", wd, err)
38 fmt.Println("gazelle container: error container push generation will fail due to no oci_repo rules being available")
39 return
40 }
41
42 bzlFile, err := build.Parse(fPath, fileBytes)
43 if err != nil {
44 fmt.Println(err)
45 fmt.Println("gazelle container: error container push generation will fail due to no oci_repo rules being available")
46 return
47 }
48
49 repos := bzlFile.Rules("oci_repo")
50 for _, repo := range repos {
51 key := fmt.Sprintf("//%s:%s", containerBuildRelativePath, repo.AttrString("name"))
52 ociRepoLabels[key] = repo.AttrString("build_setting_default")
53 }
54 }
55
56 func toImportSpec(r *rule.Rule, f *rule.File) resolve.ImportSpec {
57 if repo, found := ociRepoLabels[r.AttrString("repository_file")]; found {
58 if r.AttrString("image_name") == "" {
59 fmt.Printf("gazelle: container push image name missing for target at %s\n", f.Path)
60 fmt.Println("gazelle: import of this container push will fail")
61 imageNameAttr := r.Attr("image_name")
62 fmt.Printf("%+v\n", imageNameAttr)
63 if call, ok := imageNameAttr.(*build.CallExpr); ok {
64 fmt.Printf("x? %+v\n", call.X)
65 for _, l := range call.List {
66 fmt.Printf("%+v\n", l)
67 }
68 }
69 return resolve.ImportSpec{}
70 }
71 impName := fmt.Sprintf(
72 "%s/%s:%s",
73 repo,
74 r.AttrString("image_name"),
75 pushTag,
76 )
77 spec := resolve.ImportSpec{
78 Lang: ContainerLangName,
79 Imp: impName,
80 }
81 return spec
82 }
83
84 return resolve.ImportSpec{}
85 }
86
87
88
89
90
91
92 func (c *Container) Imports(_ *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
93 importSpecs := []resolve.ImportSpec{}
94 if r.Kind() == ContainerLangName {
95 spec := toImportSpec(r, f)
96 importSpecs = append(importSpecs, spec)
97 }
98
99 return importSpecs
100 }
101
102
103
104
View as plain text