forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_name_builder.go
72 lines (58 loc) · 2.1 KB
/
plugin_name_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package goplugin
import (
"errors"
"path/filepath"
"runtime"
"strings"
)
// GetPluginFileNameToLoad check which file to load based on name, tyk version, os and architecture
// but it also takes care of returning the name of the file that exists
func GetPluginFileNameToLoad(pluginStorage storage, path string, version string) (string, error) {
prefixedGwVersion := getPrefixedVersion(version)
newNamingFormat := getPluginNameFromTykVersion(prefixedGwVersion, path)
// 1. attempt to load a plugin that follow the new standard
if pluginStorage.fileExist(newNamingFormat) {
return newNamingFormat, nil
}
// 2. attempt to load a plugin that follows the new standard but gw version is not prefixed
if !strings.HasPrefix(version, "v") {
newNamingFormat = getPluginNameFromTykVersion(version, path)
if pluginStorage.fileExist(newNamingFormat) {
return newNamingFormat, nil
}
}
// 3. attempt to load the exact name provided
if !pluginStorage.fileExist(path) {
return "", errors.New("plugin file not found")
}
return path, nil
}
// getPluginNameFromTykVersion builds a name of plugin based on tyk version
// os and architecture. The structure of the plugin name looks like:
// {plugin-dir}/{plugin-name}_{GW-version}_{OS}_{arch}.so
// it doesn't check if the file exist
func getPluginNameFromTykVersion(version string, path string) string {
if path == "" {
return ""
}
pluginDir := filepath.Dir(path)
// remove plugin extension to have the plugin's clean name
pluginName := strings.TrimSuffix(filepath.Base(path), ".so")
os := runtime.GOOS
architecture := runtime.GOARCH
// sanitize away `-rc15` suffixes (remove `-*`) from version
vs := strings.Split(version, "-")
if len(vs) > 0 {
version = vs[0]
}
newPluginName := strings.Join([]string{pluginName, version, os, architecture}, "_")
newPluginPath := pluginDir + "/" + newPluginName + ".so"
return newPluginPath
}
// getPrefixedVersion receives a version and check that it has the prefix 'v' otherwise, it adds it
func getPrefixedVersion(version string) string {
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
return version
}