Skip to content

Commit 7cd85b0

Browse files
committed
plugin: Friendly error when run without arguments
Return a friendly error message when running a cf-cli plugin binary without any arguments. I noticed this while repeatedly testing whether a dynamically linked plugin would work on Alpine Linux. It is also noted as an issue for a plugin that we use: bluemixgaragelondon/cf-blue-green-deploy#24 Before: ➜ cf-blue-green-deploy git:(master) ✗ ./cf-blue-green-deploy panic: runtime error: index out of range goroutine 1 [running]: code.cloudfoundry.org/cli/plugin.Start(0x1594860, 0xc42011b560) /Users/dcarley/projects/gocode/src/code.cloudfoundry.org/cli/plugin/plugin_shim.go:16 +0x3bd main.main() /Users/dcarley/projects/gocode/src/github.com/bluemixgaragelondon/cf-blue-green-deploy/main.go:198 +0xe9 After: ➜ cf-blue-green-deploy git:(master) ✗ ./cf-blue-green-deploy This cf CLI plugin is not intended to be run on its own
1 parent 1c591b0 commit 7cd85b0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

plugin/plugin_shim.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import (
1313
* SendMetadata - used to fetch the plugin metadata
1414
**/
1515
func Start(cmd Plugin) {
16-
cliConnection := NewCliConnection(os.Args[1])
16+
if len(os.Args) < 2 {
17+
fmt.Printf("This cf CLI plugin is not intended to be run on its own\n\n")
18+
os.Exit(1)
19+
}
1720

21+
cliConnection := NewCliConnection(os.Args[1])
1822
cliConnection.pingCLI()
1923
if isMetadataRequest(os.Args) {
2024
cliConnection.sendPluginMetadataToCliServer(cmd.GetMetadata())

plugin/plugin_shim_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ var _ = Describe("Command", func() {
1919
)
2020

2121
Describe(".Start", func() {
22+
It("Exits with status 1 and error message if no arguments are passed", func() {
23+
args := []string{}
24+
session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
25+
Expect(err).ToNot(HaveOccurred())
26+
Eventually(session, 2).Should(Exit(1))
27+
Expect(session).To(gbytes.Say("This cf CLI plugin is not intended to be run on its own"))
28+
})
29+
2230
It("Exits with status 1 if it cannot ping the host port passed as an argument", func() {
2331
args := []string{"0", "0"}
2432
session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)

0 commit comments

Comments
 (0)