forked from jthomas/copyenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyenv.go
133 lines (110 loc) · 3.37 KB
/
copyenv.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"code.cloudfoundry.org/cli/plugin"
)
// CopyEnv plugin
type CopyEnv struct{}
// fatalIf will print the error and exit when err is not nil
func fatalIf(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "error: ", err)
os.Exit(1)
}
}
// extractAppName return the application name parameter from the command line
func (c *CopyEnv) extractAppName(args []string) (string, error) {
if len(args) < 2 {
return "", errors.New("missing application name")
}
return args[1], nil
}
func (c *CopyEnv) retrieveAppNameEnv(cliConnection plugin.CliConnection, appName string) ([]string, error) {
app, err := cliConnection.GetApp(appName)
if err != nil {
msg := fmt.Sprintf("Failed to retrieve enviroment for \"%s\", is the app name correct?", appName)
err = errors.New(msg)
} else {
url := fmt.Sprintf("/v2/apps/%s/env", app.Guid)
output, err := cliConnection.CliCommandWithoutTerminalOutput("curl", url)
if err != nil {
msg := fmt.Sprintf("Failed to retrieve enviroment for \"%s\", is the app name correct?", appName)
err = errors.New(msg)
}
return output, err
}
return nil, err
}
func (c *CopyEnv) extractCredentialsJSON(envParent string, credKey string, output []string) ([]byte, error) {
err := errors.New("missing service credentials for application")
var envJson []byte
envKey := strings.Join(output, "")
if strings.Contains(envKey, credKey) {
var f interface{}
err = json.Unmarshal([]byte(envKey), &f)
if err != nil {
return nil, err
}
envJSON := f.(map[string]interface{})
envParentJSON := envJSON[envParent].(map[string]interface{})
envJson, err = json.Marshal(envParentJSON[credKey])
if err != nil {
return nil, err
}
}
return envJson, err
}
func (c *CopyEnv) exportCredsAsShellVar(credKey string, creds string) {
vcapServices := fmt.Sprintf("export %s='%s';", credKey, creds)
fmt.Println(vcapServices)
}
func (c *CopyEnv) extractAndExportCredentials(envParent string, credKey string, appEnv []string) {
creds, err := c.extractCredentialsJSON(envParent, credKey, appEnv)
fatalIf(err)
c.exportCredsAsShellVar(credKey, string(creds[:]))
}
// Run plugin start
func (c *CopyEnv) Run(cliConnection plugin.CliConnection, args []string) {
if len(args) > 0 && args[0] == "CLI-MESSAGE-UNINSTALL" {
return
}
appName, err := c.extractAppName(args)
fatalIf(err)
appEnv, err := c.retrieveAppNameEnv(cliConnection, appName)
fatalIf(err)
if len(args) > 2 && args[2] == "--all" {
c.extractAndExportCredentials("application_env_json", "VCAP_APPLICATION", appEnv)
fmt.Println("")
}
c.extractAndExportCredentials("system_env_json", "VCAP_SERVICES", appEnv)
}
// GetMetadata returns plugin metadata
func (c *CopyEnv) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "copyenv",
Version: plugin.VersionType{
Major: 1,
Minor: 2,
Build: 4,
},
Commands: []plugin.Command{
plugin.Command{
Name: "copyenv",
HelpText: "Export application VCAP_SERVICES to local environment variable.",
UsageDetails: plugin.Usage{
Usage: "copyenv APP_NAME [--all] - Retrieve and export remote application VCAP_SERVICES to local developer environment.",
Options: map[string]string{
"all": "Retrieve both VCAP_SERVICES and VCAP_APPLICATION from remote application",
},
},
},
},
}
}
func main() {
plugin.Start(new(CopyEnv))
}