Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions command/v7/service_key_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ type ServiceKeyCommand struct {

RequiredArgs flag.ServiceInstanceKey `positional-args:"yes"`
GUID bool `long:"guid" description:"Retrieve and display the given service-key's guid. All other output is suppressed."`
JSON bool `long:"json" description:"Output credentials as JSON. All other output is suppressed."`
}

func (cmd ServiceKeyCommand) Execute(args []string) error {
if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
return err
}

switch cmd.GUID {
case true:
switch {
case cmd.GUID:
return cmd.guid()
case cmd.JSON:
return cmd.json()
default:
return cmd.details()
}
Expand All @@ -38,14 +41,27 @@ func (cmd ServiceKeyCommand) guid() error {
cmd.RequiredArgs.ServiceKey,
cmd.Config.TargetedSpace().GUID,
)
switch err.(type) {
case nil:
cmd.UI.DisplayText(key.GUID)
return nil
default:
if err != nil {
cmd.UI.DisplayWarnings(warnings)
return err
}

cmd.UI.DisplayText(key.GUID)
return nil
}

func (cmd ServiceKeyCommand) json() error {
details, warnings, err := cmd.Actor.GetServiceKeyDetailsByServiceInstanceAndName(
cmd.RequiredArgs.ServiceInstance,
cmd.RequiredArgs.ServiceKey,
cmd.Config.TargetedSpace().GUID,
)
if err != nil {
cmd.UI.DisplayWarnings(warnings)
return err
}

return cmd.UI.DisplayJSON("", details)
}

func (cmd ServiceKeyCommand) details() error {
Expand All @@ -72,10 +88,5 @@ func (cmd ServiceKeyCommand) details() error {

cmd.UI.DisplayNewline()

err = cmd.UI.DisplayJSON("", details)
if err != nil {
return err
}

return nil
return cmd.UI.DisplayJSON("", details)
}
59 changes: 59 additions & 0 deletions command/v7/service_key_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ var _ = Describe("service-key Command", func() {
Expect(actualSpace).To(BeTrue())
})

When("checking target fails", func() {
BeforeEach(func() {
fakeSharedActor.CheckTargetReturns(errors.New("not logged in"))
})

It("returns the error", func() {
Expect(executeErr).To(MatchError("not logged in"))
})
})

When("getting details", func() {
const fakeUserName = "fake-user-name"

Expand Down Expand Up @@ -167,4 +177,53 @@ var _ = Describe("service-key Command", func() {
})
})
})

When("getting JSON output", func() {
BeforeEach(func() {
fakeActor.GetServiceKeyDetailsByServiceInstanceAndNameReturns(
resources.ServiceCredentialBindingDetails{
Credentials: map[string]interface{}{"foo": "bar", "pass": "<3test"},
},
v7action.Warnings{"a warning"},
nil,
)

setFlag(&cmd, "--json")
})

It("delegates to the actor", func() {
Expect(fakeActor.GetServiceKeyDetailsByServiceInstanceAndNameCallCount()).To(Equal(1))
actualServiceInstanceName, actualKeyName, actualSpaceGUID := fakeActor.GetServiceKeyDetailsByServiceInstanceAndNameArgsForCall(0)
Expect(actualServiceInstanceName).To(Equal(fakeServiceInstanceName))
Expect(actualKeyName).To(Equal(fakeServiceKeyName))
Expect(actualSpaceGUID).To(Equal(fakeSpaceGUID))
})

It("prints JSON without intro message or warnings", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(testUI.Err).NotTo(Say("a warning"))
Expect(testUI.Out).NotTo(Say("Getting key"))
Expect(testUI.Out).To(SatisfyAll(
Say(`\{\n`),
Say(` "foo": "bar",\n`),
Say(` "pass": "<3test"\n`),
Say(`\}\n`),
))
})

When("actor returns an error", func() {
BeforeEach(func() {
fakeActor.GetServiceKeyDetailsByServiceInstanceAndNameReturns(
resources.ServiceCredentialBindingDetails{},
v7action.Warnings{"a warning"},
errors.New("bang"),
)
})

It("prints warnings and returns an error", func() {
Expect(testUI.Err).To(Say("a warning"))
Expect(executeErr).To(MatchError("bang"))
})
})
})
})
Loading