package container import ( "fmt" "path/filepath" "github.com/bazelbuild/bazel-gazelle/config" "github.com/bazelbuild/bazel-gazelle/resolve" "github.com/bazelbuild/bazel-gazelle/rule" "github.com/bazelbuild/buildtools/build" "github.com/bazelbuild/buildtools/file" "edge-infra.dev/pkg/lib/build/bazel" ) var ( containerBuildRelativePath = "hack/build/rules/container" ociRepoLabels = map[string]string{} ) // resolve registry targets once func init() { var wd string var containerBuildPath string if bazel.IsBazelTest() { containerBuildPath = "oci_repos.txt" } else { wd = bazel.ResolveWdOrDie() containerBuildPath = filepath.Join(wd, containerBuildRelativePath, "BUILD.bazel") } fPath := containerBuildPath fileBytes, _, err := file.ReadFile(fPath) if err != nil { fmt.Printf("gazelle container: error getting the BUILD.bazel at %s: %s\n", wd, err) fmt.Println("gazelle container: error container push generation will fail due to no oci_repo rules being available") return } bzlFile, err := build.Parse(fPath, fileBytes) if err != nil { fmt.Println(err) fmt.Println("gazelle container: error container push generation will fail due to no oci_repo rules being available") return } repos := bzlFile.Rules("oci_repo") for _, repo := range repos { key := fmt.Sprintf("//%s:%s", containerBuildRelativePath, repo.AttrString("name")) ociRepoLabels[key] = repo.AttrString("build_setting_default") } } func toImportSpec(r *rule.Rule, f *rule.File) resolve.ImportSpec { if repo, found := ociRepoLabels[r.AttrString("repository_file")]; found { if r.AttrString("image_name") == "" { fmt.Printf("gazelle: container push image name missing for target at %s\n", f.Path) fmt.Println("gazelle: import of this container push will fail") imageNameAttr := r.Attr("image_name") fmt.Printf("%+v\n", imageNameAttr) if call, ok := imageNameAttr.(*build.CallExpr); ok { fmt.Printf("x? %+v\n", call.X) for _, l := range call.List { fmt.Printf("%+v\n", l) } } return resolve.ImportSpec{} } impName := fmt.Sprintf( "%s/%s:%s", repo, r.AttrString("image_name"), pushTag, ) spec := resolve.ImportSpec{ Lang: ContainerLangName, Imp: impName, } return spec } return resolve.ImportSpec{} } // Imports returns a list of ImportSpecs that can be used to import the rule // r. This is used to populate RuleIndex. // // If nil is returned, the rule will not be indexed. If any non-nil slice is // returned, including an empty slice, the rule will be indexed. func (c *Container) Imports(_ *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec { importSpecs := []resolve.ImportSpec{} if r.Kind() == ContainerLangName { spec := toImportSpec(r, f) importSpecs = append(importSpecs, spec) } return importSpecs } // doesn't need to resolve? // func (c *Container) Resolve(conf *config.Config, ix *resolve.RuleIndex, _ *repo.RemoteCache, r *rule.Rule, imports interface{}, l label.Label) { }