Skip to content

Commit

Permalink
Proper error handling and exit code
Browse files Browse the repository at this point in the history
[#96606846]
  • Loading branch information
Jen Spinney and Michael Fraenkel authored and Jen Spinney and Michael Fraenkel committed Jun 19, 2015
1 parent 929fc16 commit cd9eaa4
Show file tree
Hide file tree
Showing 21 changed files with 290 additions and 314 deletions.
6 changes: 2 additions & 4 deletions cf-plugin/cmd/allow_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package cmd

import (
"fmt"
"io"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/models/space"
)

const AllowSSHUsage = "cf allow-space-ssh SPACE_NAME"

func AllowSSH(args []string, spaceFactory space.SpaceFactory, output io.Writer) error {
func AllowSSH(args []string, spaceFactory space.SpaceFactory) error {
if len(args) != 2 || args[0] != "allow-space-ssh" {
fmt.Fprintf(output, "FAILED\n\n%s\n%s", "Invalid usage", AllowSSHUsage)
return nil
return fmt.Errorf("%s\n%s", "Invalid usage", AllowSSHUsage)
}

space, err := spaceFactory.Get(args[1])
Expand Down
20 changes: 7 additions & 13 deletions cf-plugin/cmd/allow_ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"bytes"
"errors"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/cmd"
Expand All @@ -23,26 +22,21 @@ var _ = Describe("AllowSSH", func() {

Context("validation", func() {
It("requires an space name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.AllowSSH([]string{"allow-space-ssh"}, fakeSpaceFactory, writer)
err := cmd.AllowSSH([]string{"allow-space-ssh"}, fakeSpaceFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.AllowSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.AllowSSHUsage))
})

It("validates the command name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.AllowSSH([]string{"bogus", "space"}, fakeSpaceFactory, writer)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.AllowSSHUsage))
err := cmd.AllowSSH([]string{"bogus", "space"}, fakeSpaceFactory)
Expect(err).To(MatchError("Invalid usage\n" + cmd.AllowSSHUsage))
})
})

It("allows SSH on an space endpoint", func() {
fakeSpaceFactory.GetReturns(mySpace, nil)

err := cmd.AllowSSH([]string{"allow-space-ssh", "myspace"}, fakeSpaceFactory, nil)
err := cmd.AllowSSH([]string{"allow-space-ssh", "myspace"}, fakeSpaceFactory)
Expect(err).NotTo(HaveOccurred())

Expect(fakeSpaceFactory.GetCallCount()).To(Equal(1))
Expand All @@ -61,7 +55,7 @@ var _ = Describe("AllowSSH", func() {
})

It("returns an err", func() {
err := cmd.AllowSSH([]string{"allow-space-ssh", "myspace"}, fakeSpaceFactory, nil)
err := cmd.AllowSSH([]string{"allow-space-ssh", "myspace"}, fakeSpaceFactory)
Expect(err).To(MatchError("get failed"))
Expect(fakeSpaceFactory.GetCallCount()).To(Equal(1))
Expect(fakeSpaceFactory.SetBoolCallCount()).To(Equal(0))
Expand All @@ -75,7 +69,7 @@ var _ = Describe("AllowSSH", func() {
})

It("returns an err", func() {
err := cmd.AllowSSH([]string{"allow-space-ssh", "myspace"}, fakeSpaceFactory, nil)
err := cmd.AllowSSH([]string{"allow-space-ssh", "myspace"}, fakeSpaceFactory)
Expect(err).To(MatchError("set failed"))
Expect(fakeSpaceFactory.GetCallCount()).To(Equal(1))
Expect(fakeSpaceFactory.SetBoolCallCount()).To(Equal(1))
Expand Down
6 changes: 2 additions & 4 deletions cf-plugin/cmd/disable_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package cmd

import (
"fmt"
"io"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/models/app"
)

const DisableSSHUsage = "cf disable-ssh APP_NAME"

func DisableSSH(args []string, appFactory app.AppFactory, output io.Writer) error {
func DisableSSH(args []string, appFactory app.AppFactory) error {
if len(args) != 2 || args[0] != "disable-ssh" {
fmt.Fprintf(output, "FAILED\n\n%s\n%s", "Invalid usage", DisableSSHUsage)
return nil
return fmt.Errorf("%s\n%s", "Invalid usage", DisableSSHUsage)
}

app, err := appFactory.Get(args[1])
Expand Down
19 changes: 7 additions & 12 deletions cf-plugin/cmd/disable_ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"bytes"
"errors"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/cmd"
Expand All @@ -23,26 +22,22 @@ var _ = Describe("DisableSsh", func() {

Context("validation", func() {
It("requires an application name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.DisableSSH([]string{"disable-ssh"}, fakeAppFactory, writer)
err := cmd.DisableSSH([]string{"disable-ssh"}, fakeAppFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.DisableSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.DisableSSHUsage))
})

It("validates the command name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.DisableSSH([]string{"disable-ss", "app"}, fakeAppFactory, writer)
err := cmd.DisableSSH([]string{"disable-ss", "app"}, fakeAppFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.DisableSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.DisableSSHUsage))
})
})

It("disables SSH on an app endpoint", func() {
fakeAppFactory.GetReturns(myApp, nil)

err := cmd.DisableSSH([]string{"disable-ssh", "myapp"}, fakeAppFactory, nil)
err := cmd.DisableSSH([]string{"disable-ssh", "myapp"}, fakeAppFactory)
Expect(err).NotTo(HaveOccurred())

Expect(fakeAppFactory.GetCallCount()).To(Equal(1))
Expand All @@ -61,7 +56,7 @@ var _ = Describe("DisableSsh", func() {
})

It("returns an err", func() {
err := cmd.DisableSSH([]string{"disable-ssh", "myapp"}, fakeAppFactory, nil)
err := cmd.DisableSSH([]string{"disable-ssh", "myapp"}, fakeAppFactory)
Expect(err).To(MatchError("get failed"))
Expect(fakeAppFactory.GetCallCount()).To(Equal(1))
Expect(fakeAppFactory.SetBoolCallCount()).To(Equal(0))
Expand All @@ -75,7 +70,7 @@ var _ = Describe("DisableSsh", func() {
})

It("returns an err", func() {
err := cmd.DisableSSH([]string{"disable-ssh", "myapp"}, fakeAppFactory, nil)
err := cmd.DisableSSH([]string{"disable-ssh", "myapp"}, fakeAppFactory)
Expect(err).To(MatchError("set failed"))
Expect(fakeAppFactory.GetCallCount()).To(Equal(1))
Expect(fakeAppFactory.SetBoolCallCount()).To(Equal(1))
Expand Down
6 changes: 2 additions & 4 deletions cf-plugin/cmd/disallow_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package cmd

import (
"fmt"
"io"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/models/space"
)

const DisallowSSHUsage = "cf disallow-space-ssh SPACE_NAME"

func DisallowSSH(args []string, spaceFactory space.SpaceFactory, output io.Writer) error {
func DisallowSSH(args []string, spaceFactory space.SpaceFactory) error {
if len(args) != 2 || args[0] != "disallow-space-ssh" {
fmt.Fprintf(output, "FAILED\n\n%s\n%s", "Invalid usage", DisallowSSHUsage)
return nil
return fmt.Errorf("%s\n%s", "Invalid usage", DisallowSSHUsage)
}

space, err := spaceFactory.Get(args[1])
Expand Down
19 changes: 7 additions & 12 deletions cf-plugin/cmd/disallow_ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"bytes"
"errors"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/cmd"
Expand All @@ -23,26 +22,22 @@ var _ = Describe("DisallowSSH", func() {

Context("validation", func() {
It("requires an space name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.DisallowSSH([]string{"disallow-space-ssh"}, fakeSpaceFactory, writer)
err := cmd.DisallowSSH([]string{"disallow-space-ssh"}, fakeSpaceFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.DisallowSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.DisallowSSHUsage))
})

It("validates the command name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.DisallowSSH([]string{"bogus", "space"}, fakeSpaceFactory, writer)
err := cmd.DisallowSSH([]string{"bogus", "space"}, fakeSpaceFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.DisallowSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.DisallowSSHUsage))
})
})

It("disallows SSH on an space endpoint", func() {
fakeSpaceFactory.GetReturns(mySpace, nil)

err := cmd.DisallowSSH([]string{"disallow-space-ssh", "myspace"}, fakeSpaceFactory, nil)
err := cmd.DisallowSSH([]string{"disallow-space-ssh", "myspace"}, fakeSpaceFactory)
Expect(err).NotTo(HaveOccurred())

Expect(fakeSpaceFactory.GetCallCount()).To(Equal(1))
Expand All @@ -61,7 +56,7 @@ var _ = Describe("DisallowSSH", func() {
})

It("returns an err", func() {
err := cmd.DisallowSSH([]string{"disallow-space-ssh", "myspace"}, fakeSpaceFactory, nil)
err := cmd.DisallowSSH([]string{"disallow-space-ssh", "myspace"}, fakeSpaceFactory)
Expect(err).To(MatchError("get failed"))
Expect(fakeSpaceFactory.GetCallCount()).To(Equal(1))
Expect(fakeSpaceFactory.SetBoolCallCount()).To(Equal(0))
Expand All @@ -75,7 +70,7 @@ var _ = Describe("DisallowSSH", func() {
})

It("returns an err", func() {
err := cmd.DisallowSSH([]string{"disallow-space-ssh", "myspace"}, fakeSpaceFactory, nil)
err := cmd.DisallowSSH([]string{"disallow-space-ssh", "myspace"}, fakeSpaceFactory)
Expect(err).To(MatchError("set failed"))
Expect(fakeSpaceFactory.GetCallCount()).To(Equal(1))
Expect(fakeSpaceFactory.SetBoolCallCount()).To(Equal(1))
Expand Down
6 changes: 2 additions & 4 deletions cf-plugin/cmd/enable_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package cmd

import (
"fmt"
"io"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/models/app"
)

const EnableSSHUsage = "cf enable-ssh APP_NAME"

func EnableSSH(args []string, appFactory app.AppFactory, output io.Writer) error {
func EnableSSH(args []string, appFactory app.AppFactory) error {
if len(args) != 2 || args[0] != "enable-ssh" {
fmt.Fprintf(output, "FAILED\n\n%s\n%s", "Invalid usage", EnableSSHUsage)
return nil
return fmt.Errorf("%s\n%s", "Invalid usage", EnableSSHUsage)
}

app, err := appFactory.Get(args[1])
Expand Down
19 changes: 7 additions & 12 deletions cf-plugin/cmd/enable_ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"bytes"
"errors"

"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/cmd"
Expand All @@ -23,26 +22,22 @@ var _ = Describe("EnableSsh", func() {

Context("validation", func() {
It("requires an application name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.EnableSSH([]string{"enable-ssh"}, fakeAppFactory, writer)
err := cmd.EnableSSH([]string{"enable-ssh"}, fakeAppFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.EnableSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.EnableSSHUsage))
})

It("validates the command name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.EnableSSH([]string{"enable-ss", "app"}, fakeAppFactory, writer)
err := cmd.EnableSSH([]string{"enable-ss", "app"}, fakeAppFactory)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.EnableSSHUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.EnableSSHUsage))
})
})

It("enables SSH on an app endpoint", func() {
fakeAppFactory.GetReturns(myApp, nil)

err := cmd.EnableSSH([]string{"enable-ssh", "myapp"}, fakeAppFactory, nil)
err := cmd.EnableSSH([]string{"enable-ssh", "myapp"}, fakeAppFactory)
Expect(err).NotTo(HaveOccurred())

Expect(fakeAppFactory.GetCallCount()).To(Equal(1))
Expand All @@ -61,7 +56,7 @@ var _ = Describe("EnableSsh", func() {
})

It("returns an err", func() {
err := cmd.EnableSSH([]string{"enable-ssh", "myapp"}, fakeAppFactory, nil)
err := cmd.EnableSSH([]string{"enable-ssh", "myapp"}, fakeAppFactory)
Expect(err).To(MatchError("get failed"))
Expect(fakeAppFactory.GetCallCount()).To(Equal(1))
Expect(fakeAppFactory.SetBoolCallCount()).To(Equal(0))
Expand All @@ -75,7 +70,7 @@ var _ = Describe("EnableSsh", func() {
})

It("returns an err", func() {
err := cmd.EnableSSH([]string{"enable-ssh", "myapp"}, fakeAppFactory, nil)
err := cmd.EnableSSH([]string{"enable-ssh", "myapp"}, fakeAppFactory)
Expect(err).To(MatchError("set failed"))
Expect(fakeAppFactory.GetCallCount()).To(Equal(1))
Expect(fakeAppFactory.SetBoolCallCount()).To(Equal(1))
Expand Down
3 changes: 1 addition & 2 deletions cf-plugin/cmd/ssh_allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const SSHAllowedUsage = "cf space-ssh-allowed SPACE_NAME"

func SSHAllowed(args []string, spaceFactory space.SpaceFactory, output io.Writer) error {
if len(args) != 2 || args[0] != "space-ssh-allowed" {
fmt.Fprintf(output, "FAILED\n\n%s\n%s", "Invalid usage", SSHAllowedUsage)
return nil
return fmt.Errorf("%s\n%s", "Invalid usage", SSHAllowedUsage)
}

space, err := spaceFactory.Get(args[1])
Expand Down
12 changes: 4 additions & 8 deletions cf-plugin/cmd/ssh_allowed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ var _ = Describe("SSHAllowed", func() {

Context("validation", func() {
It("requires an spacelication name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.SSHAllowed([]string{"space-ssh-allowed"}, fakeSpaceFactory, writer)
err := cmd.SSHAllowed([]string{"space-ssh-allowed"}, fakeSpaceFactory, nil)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.SSHAllowedUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.SSHAllowedUsage))
})

It("validates the command name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.SSHAllowed([]string{"bogus", "space"}, fakeSpaceFactory, writer)
err := cmd.SSHAllowed([]string{"bogus", "space"}, fakeSpaceFactory, nil)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.SSHAllowedUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.SSHAllowedUsage))
})
})

Expand Down
3 changes: 1 addition & 2 deletions cf-plugin/cmd/ssh_enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const SSHEnabledUsage = "cf ssh-enabled APP_NAME"

func SSHEnabled(args []string, appFactory app.AppFactory, output io.Writer) error {
if len(args) != 2 || args[0] != "ssh-enabled" {
fmt.Fprintf(output, "FAILED\n\n%s\n%s", "Invalid usage", SSHEnabledUsage)
return nil
return fmt.Errorf("%s\n%s", "Invalid usage", SSHEnabledUsage)
}

app, err := appFactory.Get(args[1])
Expand Down
12 changes: 4 additions & 8 deletions cf-plugin/cmd/ssh_enabled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ var _ = Describe("SSHEnabled", func() {

Context("validation", func() {
It("requires an application name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.SSHEnabled([]string{"ssh-enabled"}, fakeAppFactory, writer)
err := cmd.SSHEnabled([]string{"ssh-enabled"}, fakeAppFactory, nil)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.SSHEnabledUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.SSHEnabledUsage))
})

It("validates the command name", func() {
writer := bytes.NewBuffer(nil)
err := cmd.SSHEnabled([]string{"ssh-enable", "app"}, fakeAppFactory, writer)
err := cmd.SSHEnabled([]string{"ssh-enable", "app"}, fakeAppFactory, nil)

Expect(err).NotTo(HaveOccurred())
Expect(writer.String()).To(Equal("FAILED\n\nInvalid usage\n" + cmd.SSHEnabledUsage))
Expect(err).To(MatchError("Invalid usage\n" + cmd.SSHEnabledUsage))
})
})

Expand Down
8 changes: 8 additions & 0 deletions cf-plugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "github.com/cloudfoundry/cli/plugin"

func main() {
sshPlugin := &SSHPlugin{}
plugin.Start(sshPlugin)
}
Loading

0 comments on commit cd9eaa4

Please sign in to comment.