func Version()

in pkg/export/export.go [644:694]


func Version() (string, error) {
	if testing.Testing() {
		// TODO(TheSpiritXIII): After https://github.com/golang/go/issues/50603 just return an empty
		// string here. For now, use the opportunity to confirm that the static version is correct.
		// We manually get the closest git tag if the user is running the unit test locally, but
		// fallback to the GIT_TAG environment variable in case the user is running the test via
		// Docker (like `make test` does by default).
		if testTag, found := os.LookupEnv("TEST_TAG"); !found || testTag == "false" {
			return mainModuleVersion, nil
		}
		cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
		var stdout bytes.Buffer
		cmd.Stdout = &stdout

		version := ""
		if err := cmd.Run(); err != nil {
			version = strings.TrimSpace(os.Getenv("GIT_TAG"))
			if version == "" {
				return "", errors.New("unable to detect git tag, please set GIT_TAG env variable")
			}
		} else {
			version = strings.TrimSpace(stdout.String())
		}

		return version, nil
	}

	// TODO(TheSpiritXIII): Due to https://github.com/golang/go/issues/50603 we must use a static
	// string for the main module (when we import this function locally for binaries).

	bi, ok := debug.ReadBuildInfo()
	if !ok {
		return "", errors.New("unable to retrieve build info")
	}

	if bi.Main.Path == mainModuleName {
		return mainModuleVersion, nil
	}

	var exportDep *debug.Module
	for _, dep := range bi.Deps {
		if dep.Path == mainModuleName {
			exportDep = dep
			break
		}
	}
	if exportDep == nil {
		return "", fmt.Errorf("unable to find module %q %v", mainModuleName, bi.Deps)
	}
	return exportDep.Version, nil
}