Skip to content

Commit

Permalink
Add standard UI outputs to commands
Browse files Browse the repository at this point in the history
Co-authored-by: Glen Rodgers <[email protected]>
  • Loading branch information
edwardecook and gmrodgers committed Sep 25, 2019
1 parent 38cb06c commit 764ea8f
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 31 deletions.
2 changes: 2 additions & 0 deletions commands/bosh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type BoshCommand struct {
File bool `short:"f" long:"file" description:"write a script file but do not run it"`

Env EnvReader
UI UI
BoshRunner ToolRunner
}

Expand All @@ -24,5 +25,6 @@ func (c *BoshCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# bosh\n")
return c.BoshRunner.Run(data, c.File, args...)
}
8 changes: 8 additions & 0 deletions commands/bosh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ var _ = Describe("Bosh command", func() {
command *BoshCommand

envReader *fakes.FakeEnvReader
ui *fakes.FakeUI
boshRunner *fakes.FakeToolRunner
commandArgs []string
)

BeforeEach(func() {
envReader = new(fakes.FakeEnvReader)
ui = new(fakes.FakeUI)
boshRunner = new(fakes.FakeToolRunner)
commandArgs = []string{"arg1", "arg2"}

command = &BoshCommand{
Env: envReader,
UI: ui,
BoshRunner: boshRunner,
}
})
Expand Down Expand Up @@ -65,6 +68,11 @@ var _ = Describe("Bosh command", func() {
envReader.ReadReturns(environment.Config{Name: "env-name"}, nil)
})

It("displays bosh as a comment", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# bosh\n"))
})

It("runs the bosh tool using the retrieved environment config", func() {
Expect(boshRunner.RunCallCount()).To(Equal(1))

Expand Down
1 change: 1 addition & 0 deletions commands/cf_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *CFLoginCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# cf-login\n")
c.UI.DisplayText(fmt.Sprintf("Logging in to CF at: %s\n", data.OpsManager.URL.String()))

return c.CFLoginRunner.Run(data, c.File)
Expand Down
5 changes: 3 additions & 2 deletions commands/cf_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ var _ = Describe("cf login command", func() {
})

It("displays that the cf is being logged into", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Logging in to CF at: www.test-cf.io\n"))
Expect(ui.DisplayTextCallCount()).To(Equal(2))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# cf-login\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Logging in to CF at: www.test-cf.io\n"))
})

It("runs the cf login tool using the retrieved environment config", func() {
Expand Down
2 changes: 2 additions & 0 deletions commands/om.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type OMCommand struct {
File bool `short:"f" long:"file" description:"write a script file but do not run it"`

Env EnvReader
UI UI
OMRunner ToolRunner
}

Expand All @@ -24,5 +25,6 @@ func (c *OMCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# om\n")
return c.OMRunner.Run(data, c.File, args...)
}
8 changes: 8 additions & 0 deletions commands/om_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ var _ = Describe("om command", func() {
command *OMCommand

envReader *fakes.FakeEnvReader
ui *fakes.FakeUI
omRunner *fakes.FakeToolRunner
commandArgs []string
)

BeforeEach(func() {
envReader = new(fakes.FakeEnvReader)
ui = new(fakes.FakeUI)
omRunner = new(fakes.FakeToolRunner)
commandArgs = []string{"arg1", "arg2"}

command = &OMCommand{
Env: envReader,
UI: ui,
OMRunner: omRunner,
File: true,
}
Expand Down Expand Up @@ -66,6 +69,11 @@ var _ = Describe("om command", func() {
envReader.ReadReturns(environment.Config{Name: "env-name"}, nil)
})

It("displays om as a comment", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# om\n"))
})

It("runs the om tool using the retrieved environment config", func() {
Expect(omRunner.RunCallCount()).To(Equal(1))

Expand Down
3 changes: 2 additions & 1 deletion commands/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (c *OpenCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# open\n")
if c.Show {
c.UI.DisplayText(fmt.Sprintf("%s\n", data.OpsManager.URL.String()))
c.UI.DisplayText(fmt.Sprintf("username: %s\n", data.OpsManager.Username))
Expand All @@ -39,7 +40,7 @@ func (c *OpenCommand) Execute(args []string) error {

c.UI.DisplayText(fmt.Sprintf("Opening: %s\n", data.OpsManager.URL.String()))
c.UI.DisplayText(fmt.Sprintf("Username is: %s\n", data.OpsManager.Username))
c.UI.DisplayText(fmt.Sprint("Password is in the clipboard\n"))
c.UI.DisplayText("Password is in the clipboard\n")

return c.OpenRunner.Run(data, c.File)
}
18 changes: 10 additions & 8 deletions commands/open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ var _ = Describe("open command", func() {
})

It("prints out that it is opening the ops manager and details on username and password", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(3))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Opening: www.test-cf.io\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Username is: test-username\n"))
Expect(ui.DisplayTextArgsForCall(2)).To(Equal("Password is in the clipboard\n"))
Expect(ui.DisplayTextCallCount()).To(Equal(4))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# open\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Opening: www.test-cf.io\n"))
Expect(ui.DisplayTextArgsForCall(2)).To(Equal("Username is: test-username\n"))
Expect(ui.DisplayTextArgsForCall(3)).To(Equal("Password is in the clipboard\n"))
})

It("runs the open tool using the retrieved environment config", func() {
Expand Down Expand Up @@ -157,10 +158,11 @@ var _ = Describe("open command", func() {
})

It("prints out the url, username and password for the ops manager", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(3))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("www.test-cf.io\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("username: test-username\n"))
Expect(ui.DisplayTextArgsForCall(2)).To(Equal("password: test-password\n"))
Expect(ui.DisplayTextCallCount()).To(Equal(4))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# open\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("www.test-cf.io\n"))
Expect(ui.DisplayTextArgsForCall(2)).To(Equal("username: test-username\n"))
Expect(ui.DisplayTextArgsForCall(3)).To(Equal("password: test-password\n"))
})
})
})
Expand Down
1 change: 1 addition & 0 deletions commands/pks_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *PKSLoginCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# pks-login\n")
c.UI.DisplayText(fmt.Sprintf("Logging in to PKS at: %s\n", data.OpsManager.URL.String()))

return c.PKSLoginRunner.Run(data, c.File)
Expand Down
5 changes: 3 additions & 2 deletions commands/pks_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ var _ = Describe("pks login command", func() {
})

It("displays that the pks is being logged into", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Logging in to PKS at: www.test-pks.io\n"))
Expect(ui.DisplayTextCallCount()).To(Equal(2))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# pks-login\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Logging in to PKS at: www.test-pks.io\n"))
})

It("runs the pks login tool using the retrieved environment config", func() {
Expand Down
2 changes: 2 additions & 0 deletions commands/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (c *SSHDirectorCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# ssh-director\n")
c.UI.DisplayText(fmt.Sprintf("Connecting to: %s\n", data.Name))

return c.SSHRunner.Run(data, c.File)
Expand All @@ -64,6 +65,7 @@ func (c *SSHOpsManagerCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# ssh-opsman\n")
c.UI.DisplayText(fmt.Sprintf("Connecting to: %s\n", data.Name))

return c.SSHRunner.Run(data, c.File)
Expand Down
10 changes: 6 additions & 4 deletions commands/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ var _ = Describe("ssh command", func() {
})

It("displays that the connection is being started", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Connecting to: env-name\n"))
Expect(ui.DisplayTextCallCount()).To(Equal(2))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# ssh-opsman\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Connecting to: env-name\n"))
})

It("runs the ssh tool using the retrieved environment config", func() {
Expand Down Expand Up @@ -189,8 +190,9 @@ var _ = Describe("ssh command", func() {
})

It("displays that the connection is being started", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("Connecting to: env-name\n"))
Expect(ui.DisplayTextCallCount()).To(Equal(2))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# ssh-director\n"))
Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Connecting to: env-name\n"))
})

It("runs the ssh tool using the retrieved environment config", func() {
Expand Down
2 changes: 2 additions & 0 deletions commands/sshuttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type SshuttleCommand struct {
File bool `short:"f" long:"file" description:"write a script file but do not run it"`

Env EnvReader
UI UI
SshuttleRunner ToolRunner
}

Expand All @@ -24,5 +25,6 @@ func (c *SshuttleCommand) Execute(args []string) error {
return err
}

c.UI.DisplayText("# sshuttle\n")
return c.SshuttleRunner.Run(data, c.File)
}
8 changes: 8 additions & 0 deletions commands/sshuttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ var _ = Describe("sshuttle command", func() {
command *SshuttleCommand

envReader *fakes.FakeEnvReader
ui *fakes.FakeUI
sshuttleRunner *fakes.FakeToolRunner
commandArgs []string
)

BeforeEach(func() {
envReader = new(fakes.FakeEnvReader)
ui = new(fakes.FakeUI)
sshuttleRunner = new(fakes.FakeToolRunner)
commandArgs = []string{"arg1", "arg2"}

command = &SshuttleCommand{
Env: envReader,
UI: ui,
SshuttleRunner: sshuttleRunner,
File: true,
}
Expand Down Expand Up @@ -66,6 +69,11 @@ var _ = Describe("sshuttle command", func() {
envReader.ReadReturns(environment.Config{Name: "env-name"}, nil)
})

It("displays sshuttle as a comment", func() {
Expect(ui.DisplayTextCallCount()).To(Equal(1))
Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# sshuttle\n"))
})

It("runs the sshuttle tool using the retrieved environment config", func() {
Expect(sshuttleRunner.RunCallCount()).To(Equal(1))

Expand Down
6 changes: 4 additions & 2 deletions integration/bosh_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var _ = Describe("BOSH", func() {
Eventually(string(session.Err.Contents())).Should(Equal(""))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
contents, err := ioutil.ReadFile(output)
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(Equal(LoadFixture("bosh_creds_script.sh")))
Expand All @@ -48,7 +49,8 @@ var _ = Describe("BOSH", func() {
Eventually(string(session.Err.Contents())).Should(Equal(""))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
contents, err := ioutil.ReadFile(output)
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(Equal(LoadFixture("bosh_cmd_script.sh")))
Expand Down
3 changes: 1 addition & 2 deletions integration/cf_login_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ var _ = Describe("CF", func() {
Eventually(session.Out).Should(Say("Logging in to CF at: https://pcf.manatee.cf-app.com"))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
lines := strings.Split(output, "\n")
pathToFile := lines[len(lines)-1]
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expand Down
8 changes: 5 additions & 3 deletions integration/om_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var _ = Describe("OM", func() {
Eventually(session).Should(Exit(0))
Eventually(string(session.Err.Contents())).Should(Equal(""))

pathToFile := strings.TrimSuffix(string(session.Out.Contents()), "\n")
output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -49,7 +50,8 @@ var _ = Describe("OM", func() {
Eventually(session).Should(Exit(0))
Eventually(string(session.Err.Contents())).Should(Equal(""))

pathToFile := strings.TrimSuffix(string(session.Out.Contents()), "\n")
output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -65,7 +67,7 @@ var _ = Describe("OM", func() {

Eventually(session).Should(Exit(0))
Eventually(string(session.Err.Contents())).Should(Equal(""))
Eventually(string(session.Out.Contents())).Should(Equal("export OM_TARGET=https://pcf.manatee.cf-app.com\nexport OM_USERNAME=pivotalcf\nexport OM_PASSWORD=fakePassword\n"))
Eventually(string(session.Out.Contents())).Should(Equal("# om\nexport OM_TARGET=https://pcf.manatee.cf-app.com\nexport OM_USERNAME=pivotalcf\nexport OM_PASSWORD=fakePassword\n"))
})
})
})
3 changes: 1 addition & 2 deletions integration/open_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ var _ = Describe("Open", func() {
Eventually(session.Out).Should(Say("Password is in the clipboard"))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
lines := strings.Split(output, "\n")
pathToFile := lines[len(lines)-1]
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expand Down
3 changes: 1 addition & 2 deletions integration/pks_login_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ var _ = Describe("pks", func() {
Eventually(session.Out).Should(Say("Logging in to PKS at: https://pcf.manatee.cf-app.com"))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
lines := strings.Split(output, "\n")
pathToFile := lines[len(lines)-1]
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expand Down
3 changes: 1 addition & 2 deletions integration/ssh_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ var _ = Describe("SSH", func() {
Eventually(session.Out).Should(Say("Connecting to: manatee"))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
lines := strings.Split(output, "\n")
pathToFile := lines[len(lines)-1]
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expand Down
3 changes: 2 additions & 1 deletion integration/sshuttle_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var _ = Describe("sshuttle", func() {
Eventually(string(session.Err.Contents())).Should(Equal(""))

output := strings.TrimSuffix(string(session.Out.Contents()), "\n")
contents, err := ioutil.ReadFile(output)
pathToFile := LastLine(output)
contents, err := ioutil.ReadFile(pathToFile)
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(Equal(LoadFixture("sshuttle_script.sh")))
Expand Down
6 changes: 6 additions & 0 deletions integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package integration
import (
"io/ioutil"
"path"
"strings"

. "github.com/onsi/gomega"
)
Expand All @@ -22,3 +23,8 @@ func LoadFixture(name string) string {
Expect(err).NotTo(HaveOccurred())
return string(contents)
}

func LastLine(output string) string {
lines := strings.Split(output, "\n")
return lines[len(lines)-1]
}
Loading

0 comments on commit 764ea8f

Please sign in to comment.