treewide: replace datafile pkg with rules_go/runfiles pkg

rules_go/runfiles provides the same functionality as our datafile
package. This change also contains some specifics for the now active
bzlmod, which replaces all WORKSPACE related behaviour. As example the
WORKSPACE name is now always set to _main.


Change-Id: I1a69c72b479330a627b402135670f218c297906f
Reviewed-on: https://review.monogon.dev/c/monogon/+/2745
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/cli/metroctl/BUILD.bazel b/metropolis/cli/metroctl/BUILD.bazel
index 9100031..af520e1 100644
--- a/metropolis/cli/metroctl/BUILD.bazel
+++ b/metropolis/cli/metroctl/BUILD.bazel
@@ -43,7 +43,6 @@
         "//go/clitable",
         "//metropolis/cli/metroctl/core",
         "//metropolis/cli/pkg/context",
-        "//metropolis/cli/pkg/datafile",
         "//metropolis/node/core/identity",
         "//metropolis/node/core/rpc",
         "//metropolis/node/core/rpc/resolver",
@@ -55,6 +54,7 @@
         "//version",
         "@com_github_adrg_xdg//:xdg",
         "@com_github_spf13_cobra//:cobra",
+        "@io_bazel_rules_go//go/runfiles:go_default_library",
         "@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
         "@io_k8s_client_go//pkg/apis/clientauthentication/v1:clientauthentication",
         "@org_golang_google_grpc//:go_default_library",
diff --git a/metropolis/cli/metroctl/cmd_install.go b/metropolis/cli/metroctl/cmd_install.go
index 39790f1..64cdb26 100644
--- a/metropolis/cli/metroctl/cmd_install.go
+++ b/metropolis/cli/metroctl/cmd_install.go
@@ -10,15 +10,16 @@
 	"os"
 	"strings"
 
+	"github.com/bazelbuild/rules_go/go/runfiles"
 	"github.com/spf13/cobra"
 
+	"source.monogon.dev/metropolis/proto/api"
+	cpb "source.monogon.dev/metropolis/proto/common"
+
 	"source.monogon.dev/metropolis/cli/metroctl/core"
 	clicontext "source.monogon.dev/metropolis/cli/pkg/context"
-	"source.monogon.dev/metropolis/cli/pkg/datafile"
 	"source.monogon.dev/metropolis/pkg/blkio"
 	"source.monogon.dev/metropolis/pkg/fat32"
-	"source.monogon.dev/metropolis/proto/api"
-	cpb "source.monogon.dev/metropolis/proto/common"
 )
 
 var installCmd = &cobra.Command{
@@ -53,10 +54,14 @@
 
 func external(name, datafilePath string, flag *string) fat32.SizedReader {
 	if flag == nil || *flag == "" {
-		df, err := datafile.Get(datafilePath)
+		rPath, err := runfiles.Rlocation(datafilePath)
 		if err != nil {
 			log.Fatalf("No %s specified", name)
 		}
+		df, err := os.ReadFile(rPath)
+		if err != nil {
+			log.Fatalf("Cant read file: %v", err)
+		}
 		return bytes.NewReader(df)
 	}
 
diff --git a/metropolis/cli/metroctl/test/BUILD.bazel b/metropolis/cli/metroctl/test/BUILD.bazel
index 36f5a2d..dc3f456 100644
--- a/metropolis/cli/metroctl/test/BUILD.bazel
+++ b/metropolis/cli/metroctl/test/BUILD.bazel
@@ -13,13 +13,13 @@
     ],
     rundir = ".",
     deps = [
-        "//metropolis/cli/pkg/datafile",
         "//metropolis/node",
         "//metropolis/pkg/cmd",
         "//metropolis/test/launch/cluster",
         "//metropolis/test/util",
         "//metropolis/version",
         "//version",
+        "@io_bazel_rules_go//go/runfiles:go_default_library",
     ],
 )
 
@@ -29,11 +29,11 @@
     importpath = "source.monogon.dev/metropolis/cli/metroctl/test",
     visibility = ["//visibility:private"],
     deps = [
-        "//metropolis/cli/pkg/datafile",
         "//metropolis/pkg/cmd",
         "//metropolis/test/launch/cluster",
         "//metropolis/test/util",
         "//metropolis/version",
         "//version",
+        "@io_bazel_rules_go//go/runfiles:go_default_library",
     ],
 )
diff --git a/metropolis/cli/metroctl/test/test.go b/metropolis/cli/metroctl/test/test.go
index b031271..85bbaa2 100644
--- a/metropolis/cli/metroctl/test/test.go
+++ b/metropolis/cli/metroctl/test/test.go
@@ -11,18 +11,20 @@
 	"testing"
 	"time"
 
-	"source.monogon.dev/metropolis/cli/pkg/datafile"
+	"github.com/bazelbuild/rules_go/go/runfiles"
+
+	mversion "source.monogon.dev/metropolis/version"
+
 	"source.monogon.dev/metropolis/pkg/cmd"
 	"source.monogon.dev/metropolis/test/launch/cluster"
 	"source.monogon.dev/metropolis/test/util"
-	mversion "source.monogon.dev/metropolis/version"
 	"source.monogon.dev/version"
 )
 
 // resolveMetroctl resolves metroctl filesystem path. It will return a correct
 // path, or terminate test execution.
 func resolveMetroctl() string {
-	path, err := datafile.ResolveRunfile("metropolis/cli/metroctl/metroctl_/metroctl")
+	path, err := runfiles.Rlocation("_main/metropolis/cli/metroctl/metroctl_/metroctl")
 	if err != nil {
 		log.Fatalf("Couldn't resolve metroctl binary: %v", err)
 	}
diff --git a/metropolis/cli/pkg/datafile/BUILD.bazel b/metropolis/cli/pkg/datafile/BUILD.bazel
deleted file mode 100644
index 8c47aeb..0000000
--- a/metropolis/cli/pkg/datafile/BUILD.bazel
+++ /dev/null
@@ -1,9 +0,0 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_library")
-
-go_library(
-    name = "datafile",
-    srcs = ["datafile.go"],
-    importpath = "source.monogon.dev/metropolis/cli/pkg/datafile",
-    visibility = ["//visibility:public"],
-    deps = ["@io_bazel_rules_go//go/tools/bazel:go_default_library"],
-)
diff --git a/metropolis/cli/pkg/datafile/datafile.go b/metropolis/cli/pkg/datafile/datafile.go
deleted file mode 100644
index a78416e..0000000
--- a/metropolis/cli/pkg/datafile/datafile.go
+++ /dev/null
@@ -1,99 +0,0 @@
-// datafile provides an abstraction for accessing files passed through the data
-// attribute in a Bazel build rule.
-//
-// It thinly wraps around the Bazel/Go runfile library (to allow running from
-// outside `bazel run`).
-package datafile
-
-import (
-	"bufio"
-	"fmt"
-	"log"
-	"os"
-	"path/filepath"
-	"strings"
-
-	"github.com/bazelbuild/rules_go/go/tools/bazel"
-)
-
-// parseManifest takes a bazel runfile MANIFEST and parses it into a map from
-// workspace-relative path to absolute path, flattening all workspaces into a
-// single tree.
-func parseManifest(path string) (map[string]string, error) {
-	f, err := os.Open(path)
-	if err != nil {
-		return nil, fmt.Errorf("could not open MANIFEST: %v", err)
-	}
-	defer f.Close()
-
-	manifest := make(map[string]string)
-	scanner := bufio.NewScanner(f)
-	for scanner.Scan() {
-		parts := strings.Split(scanner.Text(), " ")
-		if len(parts) != 2 {
-			continue
-		}
-		fpathParts := strings.Split(parts[0], string(os.PathSeparator))
-		fpath := strings.Join(fpathParts[1:], string(os.PathSeparator))
-		manifest[fpath] = parts[1]
-	}
-	return manifest, nil
-}
-
-// ResolveRunfile tries to resolve a workspace-relative file path into an
-// absolute path with the use of bazel runfiles, through either the original
-// Bazel/Go runfile integration or a wrapper that also supports running from
-// outside `bazel run`.
-func ResolveRunfile(path string) (string, error) {
-	var errEx error
-	ep, err := os.Executable()
-	if err == nil {
-		rfdir := ep + ".runfiles"
-		mfpath := filepath.Join(rfdir, "MANIFEST")
-		if stat, err := os.Stat(rfdir); err == nil && stat.IsDir() {
-			// We have a runfiles directory, parse MANIFEST and resolve files this way.
-			manifest, err := parseManifest(mfpath)
-			if err == nil {
-				tpath := manifest[path]
-				if tpath == "" {
-					errEx = fmt.Errorf("not in MANIFEST")
-				} else {
-					return tpath, err
-				}
-			} else {
-				errEx = err
-			}
-		} else {
-			errEx = err
-		}
-	}
-
-	// Try runfiles just in case.
-	rf, errRF := bazel.Runfile(path)
-	if errRF == nil {
-		return rf, nil
-	}
-	return "", fmt.Errorf("could not resolve via executable location (%v) and runfile resolution failed: %v", errEx, errRF)
-}
-
-// Get tries to read a workspace-relative file path through the use of Bazel
-// runfiles, including for cases when executables are running outside `bazel
-// run`.
-func Get(path string) ([]byte, error) {
-	rfpath, err := ResolveRunfile(path)
-	if err != nil {
-		return nil, err
-	}
-	return os.ReadFile(rfpath)
-}
-
-// MustGet either successfully resolves a file through Get() or logs an error
-// (through the stdlib log library) and stops execution. This should thus only
-// be used in binaries which use the log library.
-func MustGet(path string) []byte {
-	res, err := Get(path)
-	if err != nil {
-		log.Fatalf("Could not get datafile %s: %v", path, err)
-	}
-	return res
-}