Skip to content

Commit

Permalink
Move the "no command provided" out of the shell script.
Browse files Browse the repository at this point in the history
  • Loading branch information
srbaker committed Aug 3, 2016
1 parent 56b4457 commit 53cc306
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Expand Up @@ -58,6 +58,10 @@ public Result execute( Path homeDir, Path configDir, String... args )
{
try
{
if ( args.length == 0 )
{
return badUsage( "you must provide a command" );
}
String name = args[0];
String[] commandArgs = Arrays.copyOfRange( args, 1, args.length );

Expand All @@ -68,7 +72,7 @@ public Result execute( Path homeDir, Path configDir, String... args )
}
catch ( NoSuchElementException e )
{
return badUsage( name, commandArgs );
return badUsage( format( "unrecognized command: %s", name ) );
}

AdminCommand command = provider.create( homeDir, configDir );
Expand Down Expand Up @@ -103,10 +107,9 @@ private Result badUsage( AdminCommand.Provider command, IncorrectUsage e )
return failure( e.getMessage() );
}

private Result badUsage( String name, String[] commandArgs )
private Result badUsage( String message )
{
usage.print();
String message = commandArgs.length == 0 ? format( "unrecognized command: %s", name ) : commandArgs[0];
return failure( message );
}

Expand Down
Expand Up @@ -51,6 +51,15 @@ public void shouldAddTheHelpCommand()
verify( output ).line( "neo4j-admin help" );
}

@Test
public void shouldProvideErrorMessageWhenNoCommandIsProvided()
{
Output output = mock( Output.class );
new AdminTool( new NullCommandLocator(), output, false ).execute( null, null, new String[]{} );

verify( output ).line( "Usage:" );
}

private static class RecordingCommand implements AdminCommand
{
public boolean executed;
Expand Down
Expand Up @@ -23,23 +23,12 @@ set -o errexit -o nounset -o pipefail
readonly NEO4J_BIN
. "${NEO4J_BIN}/neo4j-shared.sh"

pass_through() {
main() {
setup_environment
check_java
build_classpath
export NEO4J_HOME NEO4J_CONF
exec "${JAVA_CMD}" -cp "${CLASSPATH}" -Dfile.encoding=UTF-8 "org.neo4j.commandline.admin.AdminTool" "$@"
}

bad_usage() {
pass_through bad-usage "$1"
}

main() {
[[ -z "${1:-}" ]] && bad_usage "you must provide a command"
declare -r command="$1"
shift
pass_through "${command}" "$@"
}

main "$@"

0 comments on commit 53cc306

Please sign in to comment.