forked from cloudfoundry-community/go-cfenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfenv.go
59 lines (51 loc) · 1.63 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package cfenv provides information about the current app deployed on Cloud Foundry, including any bound service(s).
package cfenv
import (
"encoding/json"
"os"
"strconv"
"strings"
"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
}
// duplicate the InstanceID to the previously named ID field for backwards
// compatibility
app.ID = app.InstanceID
app.Home = env["HOME"]
app.MemoryLimit = env["MEMORY_LIMIT"]
if port, err := strconv.Atoi(env["PORT"]); err == nil {
app.Port = port
}
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; returns an error if the current environment is not a Cloud Foundry environment
func Current() (*App, error) {
return New(CurrentEnv())
}
// IsRunningOnCF returns true if the current environment is Cloud Foundry and false if it is not Cloud Foundry
func IsRunningOnCF() bool {
return strings.TrimSpace(os.Getenv("VCAP_APPLICATION")) != ""
}