Skip to content

Commit

Permalink
Don't run with --help or an unknown flag
Browse files Browse the repository at this point in the history
Co-authored-by: Danail Branekov <danailster@gmail.com>
  • Loading branch information
mnitchev and danail-branekov committed Jun 1, 2021
1 parent ca31e07 commit d587788
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
24 changes: 24 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,30 @@ var _ = Describe("Integration", func() {
Eventually(gdnSession).Should(gexec.Exit(131))
})
})

When("passed the --help flag", func() {
BeforeEach(func() {
cmd.Args = append(cmd.Args, "--help")
})

It("prints the usage and exits", func() {
Expect(session.ExitCode()).To(Equal(0))
Expect(session).To(gbytes.Say("Usage"))
Expect(filepath.Join(sandboxDir, "var/vcap/data/tmp/")).NotTo(BeADirectory())
})
})

When("passed an unknown flag", func() {
BeforeEach(func() {
cmd.Args = append(cmd.Args, "--hello")
})

It("prints the usage and exits", func() {
Expect(session.ExitCode()).To(Equal(1))
Expect(session.Err).To(gbytes.Say("unknown flag"))
Expect(filepath.Join(sandboxDir, "var/vcap/data/tmp/")).NotTo(BeADirectory())
})
})
})

func tarballShouldContainFile(tarballPath, filePath string) {
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -32,7 +33,7 @@ func main() {
SigQUIT bool `long:"sigquit" description:"Send a SIGQUIT to the gdn process"`
}

flags.ParseArgs(&opts, os.Args)
handleFlagErrors(flags.ParseArgs(&opts, os.Args))

checkIsRoot()
checkIsNotBpm()
Expand Down Expand Up @@ -154,3 +155,14 @@ func createReportDir(baseDir string) string {
}
return path
}

func handleFlagErrors(_ []string, err error) {
flagErr := &flags.Error{}
if errors.As(err, &flagErr) && flagErr.Type == flags.ErrHelp {
os.Exit(0)
}

if err != nil {
os.Exit(1)
}
}

0 comments on commit d587788

Please sign in to comment.