Skip to content

Commit

Permalink
Fixing breaking CLI API changes!
Browse files Browse the repository at this point in the history
  • Loading branch information
James Thomas committed Sep 9, 2016
1 parent 8143861 commit 24355fd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
Binary file modified bin/linux/copyenv
Binary file not shown.
Binary file modified bin/osx/copyenv
Binary file not shown.
Binary file modified bin/windows/copyenv.exe
Binary file not shown.
51 changes: 30 additions & 21 deletions copyenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,43 @@ func (c *CopyEnv) ExtractAppName(args []string) (string, error) {
}

func (c *CopyEnv) RetrieveAppNameEnv(cliConnection plugin.CliConnection, app_name string) ([]string, error) {
output, err := cliConnection.CliCommandWithoutTerminalOutput("env", app_name)

app, err := cliConnection.GetApp(app_name)

if err != nil {
msg := fmt.Sprintf("Failed to retrieve enviroment for \"%s\", is the app name correct?", app_name)
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?", app_name)
err = errors.New(msg)
}

return output, err
return output, err
}
return nil, err
}

func (c *CopyEnv) ExtractCredentialsJSON(credKey string, output []string) ([]byte, error) {
func (c *CopyEnv) ExtractCredentialsJSON(envParent string, credKey string, output []string) ([]byte, error) {
err := errors.New("missing service credentials for application")
var b []byte

for _, val := range output {
if strings.Contains(val, credKey) {
var f interface{}
err = json.Unmarshal([]byte(val), &f)
if err != nil {
return nil, err
}

m := f.(map[string]interface{})
b, err = json.Marshal(m[credKey])
if err != nil {
return nil, err
}
val := strings.Join(output, "")
if strings.Contains(val, credKey) {
var f interface{}
err = json.Unmarshal([]byte(val), &f)
if err != nil {
return nil, err
}

envJson := f.(map[string]interface{})
envParentJson := envJson[envParent].(map[string]interface{})
b, err = json.Marshal(envParentJson[credKey])
if err != nil {
return nil, err
}
}

Expand All @@ -67,8 +76,8 @@ func (c *CopyEnv) ExportCredsAsShellVar(cred_key string, creds string) {
fmt.Println(vcap_services)
}

func (c *CopyEnv) ExtractAndExportCredentials(cred_key string, app_env []string) {
creds, err := c.ExtractCredentialsJSON(cred_key, app_env)
func (c *CopyEnv) ExtractAndExportCredentials(env_parent string, cred_key string, app_env []string) {
creds, err := c.ExtractCredentialsJSON(env_parent, cred_key, app_env)
fatalIf(err)
c.ExportCredsAsShellVar(cred_key, string(creds[:]))
}
Expand All @@ -84,11 +93,11 @@ func (c *CopyEnv) Run(cliConnection plugin.CliConnection, args []string) {
fatalIf(err)

if len(args) > 2 && args[2] == "--all" {
c.ExtractAndExportCredentials("VCAP_APPLICATION", app_env)
c.ExtractAndExportCredentials("application_env_json", "VCAP_APPLICATION", app_env)
fmt.Println("")
}

c.ExtractAndExportCredentials("VCAP_SERVICES", app_env)
c.ExtractAndExportCredentials("system_env_json", "VCAP_SERVICES", app_env)
}

func (c *CopyEnv) GetMetadata() plugin.PluginMetadata {
Expand Down
10 changes: 6 additions & 4 deletions copyenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main_test
import (
. "."
"errors"
"github.com/cloudfoundry/cli/plugin/models"
"github.com/cloudfoundry/cli/plugin/pluginfakes"
io_helpers "github.com/cloudfoundry/cli/testhelpers/io"
. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -30,19 +31,20 @@ var _ = Describe("Cloud Foundry Copyenv Command", func() {

It("Retrieve Application Environment Variables From Name", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{"SOME", "OUTPUT", "COMMAND"}, nil)
fakeCliConnection.GetAppReturns(plugin_models.GetAppModel{Guid: "testing"}, nil)
output, err := callCopyEnvCommandPlugin.RetrieveAppNameEnv(fakeCliConnection, "APP_NAME")
Ω(err).ShouldNot(HaveOccurred())
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputCallCount()).Should(Equal(1))
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputArgsForCall(0)).Should(Equal([]string{"env", "APP_NAME"}))
Ω(fakeCliConnection.CliCommandWithoutTerminalOutputArgsForCall(0)).Should(Equal([]string{"curl", "/v2/apps/testing/env"}))
Ω(output).Should(Equal([]string{"SOME", "OUTPUT", "COMMAND"}))
})

It("Return Service Credentials From Appplication Environment", func() {
_, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", []string{""})
_, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("system_env_json", "VCAP_SERVICES", []string{""})
Ω(err).Should(MatchError("missing service credentials for application"))

service_creds := []string{"{\"VCAP_SERVICES\":{\"service\": [ { \"credentials\": {} } ]}}"}
b, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", service_creds)
service_creds := []string{"{\"system_env_json\": {\"VCAP_SERVICES\":{\"service\": [ { \"credentials\": {} } ]}}}"}
b, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("system_env_json", "VCAP_SERVICES", service_creds)
Ω(err).ShouldNot(HaveOccurred())
Ω(string(b[:])).Should(Equal("{\"service\":[{\"credentials\":{}}]}"))
})
Expand Down

0 comments on commit 24355fd

Please sign in to comment.