handle help error in concourse cmd properly

following what fly cli does, so it won't exit 1 with '--help'

Signed-off-by: Rui Yang <ryang@pivotal.io>
This commit is contained in:
Rui Yang 2019-11-08 22:12:07 -05:00
parent 71130c430a
commit b8dfeba1d9
2 changed files with 12 additions and 9 deletions

View File

@ -209,8 +209,6 @@ type RunCommand struct {
ConfigRBAC string `long:"config-rbac" description:"Customize RBAC role-action mapping."`
}
var HelpError = errors.New("must specify one of `--current-db-version`, `--supported-db-version`, or `--migrate-db-to-version`")
type Migration struct {
Postgres flag.PostgresConfig `group:"PostgreSQL Configuration" namespace:"postgres"`
EncryptionKey flag.Cipher `long:"encryption-key" description:"A 16 or 32 length key used to encrypt sensitive information before storing it in the database."`
@ -229,7 +227,8 @@ func (m *Migration) Execute(args []string) error {
if m.MigrateDBToVersion > 0 {
return m.migrateDBToVersion()
}
return HelpError
return errors.New("must specify one of `--current-db-version`, `--supported-db-version`, or `--migrate-db-to-version`")
}
func (cmd *Migration) currentDBVersion() error {

View File

@ -5,7 +5,6 @@ import (
"os"
"github.com/concourse/concourse"
"github.com/concourse/concourse/atc/atccmd"
flags "github.com/jessevdk/go-flags"
"github.com/vito/twentythousandtonnesofcrudeoil"
)
@ -29,13 +28,18 @@ func main() {
twentythousandtonnesofcrudeoil.TheEnvironmentIsPerfectlySafe(parser, "CONCOURSE_")
_, err := parser.Parse()
handleError(parser, err)
}
func handleError(helpParser *flags.Parser, err error) {
if err != nil {
if err == atccmd.HelpError {
parser.WriteHelp(os.Stdout)
os.Exit(1)
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
fmt.Println(err)
os.Exit(0)
} else {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
fmt.Fprintf(os.Stderr, "error: %s\n", err)
}
os.Exit(1)
}
}