forked from craftamap/atlas-gonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.go
84 lines (68 loc) · 1.83 KB
/
addon.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
73
74
75
76
77
78
79
80
81
82
83
84
package gonnect
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"text/template"
"github.com/go-enjin/be/pkg/log"
"github.com/go-enjin/github-com-craftamap-atlas-gonnect/store"
)
type Addon struct {
Config *Profile
CurrentProfile string
Store *store.Store
AddonDescriptor map[string]interface{}
Key *string
Name *string
}
func readAddonDescriptor(descriptorReader io.Reader, baseUrl string) (map[string]interface{}, error) {
vals := map[string]string{
"BaseUrl": baseUrl,
}
temp, err := ioutil.ReadAll(descriptorReader)
if err != nil {
return nil, err
}
tmpl, err := template.New("descriptor").Parse(string(temp))
if err != nil {
return nil, err
}
var buffer bytes.Buffer
err = tmpl.ExecuteTemplate(&buffer, "descriptor", vals)
if err != nil {
return nil, err
}
descriptor := map[string]interface{}{}
err = json.Unmarshal(buffer.Bytes(), &descriptor)
if err != nil {
return nil, err
}
return descriptor, nil
}
func NewCustomAddon(config *Profile, currentProfile string, addonDescriptor map[string]interface{}, s *store.Store) (a *Addon, err error) {
log.InfoF("Initializing new Addon with profile: %v", currentProfile)
log.DebugF("Using Addon Profile: %v", config)
log.DebugF("Using Addon descriptor: %v", addonDescriptor)
var ok bool
var name, key string
if name, ok = addonDescriptor["name"].(string); !ok {
err = fmt.Errorf("name could not be read from AddonDescriptor")
return
}
if key, ok = addonDescriptor["key"].(string); !ok {
err = fmt.Errorf("key could not be read from AddonDescriptor")
return
}
a = &Addon{
Config: config,
Store: s,
CurrentProfile: currentProfile,
AddonDescriptor: addonDescriptor,
Name: &name,
Key: &key,
}
log.DebugF("addon successfully initialized")
return
}