1package internal
2
3import (
4	"os"
5	"path/filepath"
6	"regexp"
7	"runtime"
8	"strings"
9)
10
11var assetsPath string
12
13func init() {
14	_, thisFile, _, ok := runtime.Caller(0)
15	if !ok {
16		panic("Could not determine the path of the BinPathFinder")
17	}
18	assetsPath = filepath.Join(filepath.Dir(thisFile), "..", "assets", "bin")
19}
20
21// BinPathFinder checks the an environment variable, derived from the symbolic name,
22// and falls back to a default assets location when this variable is not set
23func BinPathFinder(symbolicName string) (binPath string) {
24	punctuationPattern := regexp.MustCompile("[^A-Z0-9]+")
25	sanitizedName := punctuationPattern.ReplaceAllString(strings.ToUpper(symbolicName), "_")
26	leadingNumberPattern := regexp.MustCompile("^[0-9]+")
27	sanitizedName = leadingNumberPattern.ReplaceAllString(sanitizedName, "")
28	envVar := "TEST_ASSET_" + sanitizedName
29
30	if val, ok := os.LookupEnv(envVar); ok {
31		return val
32	}
33
34	return filepath.Join(assetsPath, symbolicName)
35}
36