forked from pivotal-pez/admin-portal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfenv.go
43 lines (38 loc) · 1.13 KB
/
cfenv.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 cfenv provides information about the current app deployed on Cloud Foundry, including any bound service(s).
package cfenv
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
)
// New creates a new App with the provided environment.
func New(env map[string]string) (*App, error) {
var app App
appVar := env["VCAP_APPLICATION"]
if err := json.Unmarshal([]byte(appVar), &app); err != nil {
return nil, err
}
app.Home = env["HOME"]
app.MemoryLimit = env["MEMORY_LIMIT"]
app.WorkingDir = env["PWD"]
app.TempDir = env["TMPDIR"]
app.User = env["USER"]
var rawServices map[string]interface{}
servicesVar := env["VCAP_SERVICES"]
if err := json.Unmarshal([]byte(servicesVar), &rawServices); err != nil {
return nil, err
}
services := make(map[string][]Service)
for k, v := range rawServices {
var serviceInstances []Service
if err := mapstructure.WeakDecode(v, &serviceInstances); err != nil {
return nil, err
}
services[k] = serviceInstances
}
app.Services = services
return &app, nil
}
// Current creates a new App with the current environment.
func Current() (*App, error) {
return New(CurrentEnv())
}