-
Notifications
You must be signed in to change notification settings - Fork 57
/
utils.go
43 lines (39 loc) · 1004 Bytes
/
utils.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
package internal
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
)
func GetStringValueFromJSON(fpath string, key string) (string, error) {
byteValue, err := os.ReadFile(fpath)
if err != nil {
return "", err
}
var result map[string]interface{}
err = json.Unmarshal(byteValue, &result)
if err != nil {
return "", err
}
executable := result[key]
name, ok := executable.(string)
if !ok || name == "" {
return "", fmt.Errorf("plugin.json is missing: %s", key)
}
return name, nil
}
func GetExecutableFromPluginJSON(dir string) (string, error) {
exe, err := GetStringValueFromJSON(path.Join(dir, "plugin.json"), "executable")
if err != nil {
// In app plugins, the exe may be nested
exe, err2 := GetStringValueFromJSON(path.Join(dir, "datasource", "plugin.json"), "executable")
if err2 == nil {
if !strings.HasPrefix(exe, "../") {
return "", fmt.Errorf("datasource should reference executable in root folder")
}
return exe[3:], nil
}
}
return exe, err
}