Skip to content

Commit

Permalink
added admin command sections
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenag committed Feb 28, 2017
1 parent 154a429 commit 9f58ac6
Show file tree
Hide file tree
Showing 24 changed files with 580 additions and 124 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.neo4j.commandline.arguments.Arguments;
import org.neo4j.helpers.Service;
Expand Down Expand Up @@ -80,12 +81,23 @@ public List<Arguments> possibleArguments()
*/
public abstract String summary();

/**
* @return AdminCommandSection the command using the provider is grouped under
*/
public abstract AdminCommandSection commandSection();

/**
* @return A description for the command's help text.
*/
public abstract String description();

public abstract AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld );

public final void printSummary( Consumer<String> output )
{
output.accept( String.format( "%s", name() ) );
output.accept( " " + summary() );
}
}

interface Blocker
Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.admin;

import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

public abstract class AdminCommandSection
{
private static final AdminCommandSection GENERAL = new GeneralSection();

public abstract String printable();

public static AdminCommandSection general()
{
return GENERAL;
}

public final void printAllCommandsUnderSection( Consumer<String> output, List<AdminCommand.Provider> providers )
{
output.accept( printable() );
providers.sort( Comparator.comparing( AdminCommand.Provider::name ) );
providers.forEach( provider -> provider.printSummary( s -> output.accept( " " + s ) ) );
}

static class GeneralSection extends AdminCommandSection
{
@Override
public String printable()
{
return "General";
}
}
}
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.admin;

import java.util.function.Consumer;

import org.neo4j.commandline.arguments.Arguments;

import static java.lang.String.format;

class CommandUsage
{
private final AdminCommand.Provider command;
private final String scriptName;

CommandUsage( AdminCommand.Provider command, String scriptName )
{
this.command = command;
this.scriptName = scriptName;
}

void printDetailed( Consumer<String> output )
{
for ( Arguments arguments : command.possibleArguments() )
{
//Arguments arguments = command.arguments();

String left = format( "usage: %s %s", scriptName, command.name() );

output.accept( Arguments.rightColumnFormatted( left, arguments.usage(), left.length() + 1 ) );
}
output.accept( "" );
output.accept( command.allArguments().description( command.description() ) );
}
}
Expand Up @@ -51,6 +51,12 @@ public String summary()
return description();
}

@Override
public AdminCommandSection commandSection()
{
return AdminCommandSection.general();
}

@Override
public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
Expand Down
Expand Up @@ -22,11 +22,10 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.neo4j.commandline.arguments.Arguments;

import static java.lang.String.format;

public class Usage
Expand All @@ -40,75 +39,53 @@ public Usage( String scriptName, CommandLocator commands )
this.commands = commands;
}

public void printUsageForCommand( AdminCommand.Provider command, Consumer<String> output )
{
final CommandUsage commandUsage = new CommandUsage( command, scriptName );
commandUsage.printDetailed( output );
}

public void print( Consumer<String> output )
{
output.accept( format( "usage: %s <command>", scriptName ) );
output.accept( "" );
output.accept( "Manage your Neo4j instance." );
output.accept( "" );

output.accept( "environment variables:" );
output.accept( " NEO4J_DEBUG Set to anything to enable debug output." );
output.accept( " NEO4J_HOME Neo4j home directory." );
output.accept( " NEO4J_CONF Path to directory which contains neo4j.conf." );
output.accept( "" );
printEnvironmentVariables( output );

output.accept( "available commands:" );
printCommandsUnderASegment( output );
printCommands( output );

output.accept( "" );
output.accept( format( "Use %s help <command> for more details.", scriptName ) );
}

private void printCommandsUnderASegment( Consumer<String> output )
private void printEnvironmentVariables( Consumer<String> output )
{
List<AdminCommand.Provider> providers = new ArrayList<>();
commands.getAllProviders().forEach( providers::add );
providers.sort( Comparator.comparing( AdminCommand.Provider::name ) );
providers.forEach( command ->
{
final CommandUsage commandUsage = new CommandUsage( command, scriptName );
commandUsage.printIndentedSummary( output );
} );
}

public void printUsageForCommand( AdminCommand.Provider command, Consumer<String> output )
{
final CommandUsage commandUsage = new CommandUsage( command, scriptName );
commandUsage.printDetailed( output );
output.accept( " NEO4J_CONF Path to directory which contains neo4j.conf." );
output.accept( " NEO4J_DEBUG Set to anything to enable debug output." );
output.accept( " NEO4J_HOME Neo4j home directory." );
output.accept( "" );
}

public static class CommandUsage
private void printCommands( Consumer<String> output )
{
private final AdminCommand.Provider command;
private final String scriptName;

public CommandUsage( AdminCommand.Provider command, String scriptName )
{
this.command = command;
this.scriptName = scriptName;
}

public void printSummary( Consumer<String> output )
{
output.accept( format( "%s", command.name() ) );
output.accept( " " + command.summary() );
}

public void printIndentedSummary( Consumer<String> output )
{
printSummary( s -> output.accept( " " + s ) );
}
Map<AdminCommandSection,List<AdminCommand.Provider>> groupedProviders = groupProvidersBySegment();

public void printDetailed( Consumer<String> output )
{
for ( Arguments arguments : command.possibleArguments() )
{
//Arguments arguments = command.arguments();
AdminCommandSection.general()
.printAllCommandsUnderSection( output, groupedProviders.remove( AdminCommandSection.general() ) );

String left = format( "usage: %s %s", scriptName, command.name() );
groupedProviders.entrySet().stream()
.sorted( Comparator.comparing( groupedProvider -> groupedProvider.getKey().printable() ) )
.forEach(entry -> entry.getKey().printAllCommandsUnderSection( output, entry.getValue() ) );
}

output.accept( Arguments.rightColumnFormatted( left, arguments.usage(), left.length() + 1 ) );
}
output.accept( "" );
output.accept( command.allArguments().description( command.description() ) );
}
private Map<AdminCommandSection,List<AdminCommand.Provider>> groupProvidersBySegment()
{
List<AdminCommand.Provider> providers = new ArrayList<>();
commands.getAllProviders().forEach( providers::add );
return providers.stream().collect( Collectors.groupingBy( AdminCommand.Provider::commandSection ) );
}
}
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.admin;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;
import java.util.function.Consumer;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.inOrder;

public class AdminCommandSectionTest
{
@Mock
private Consumer<String> out;

@Before
public void setUp()
{
MockitoAnnotations.initMocks( this );
}

@Test
public void shouldPrintUsageForAllCommandsAlphabetically()
{
AdminCommandSection generalSection = AdminCommandSection.general();

List<AdminCommand.Provider> providers = asList(
new StubProvider( "restore", "Restore", generalSection ),
new StubProvider( "bam", "A summary", generalSection ),
new StubProvider( "zzzz-last-one", "Another summary", generalSection ) );
generalSection.printAllCommandsUnderSection( out, providers );

InOrder ordered = inOrder( out );
ordered.verify( out ).accept( "General" );
ordered.verify( out ).accept( " bam" );
ordered.verify( out ).accept( " A summary" );
ordered.verify( out ).accept( " restore" );
ordered.verify( out ).accept( " Restore" );
ordered.verify( out ).accept( " zzzz-last-one" );
ordered.verify( out ).accept( " Another summary" );
ordered.verifyNoMoreInteractions();
}
}
Expand Up @@ -169,9 +169,8 @@ public void shouldProvideFeedbackIfTheCommandReportsAUsageProblem()
{
throw new IncorrectUsage( "the-usage-message" );
};
new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ).execute(
null, null,
"exception" );
new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false )
.execute( null, null, "exception" );
InOrder inOrder = inOrder( outsideWorld );
inOrder.verify( outsideWorld ).stdErrLine( "the-usage-message" );
verify( outsideWorld ).exit( 1 );
Expand Down Expand Up @@ -278,6 +277,11 @@ public String summary()
return "";
}

public AdminCommandSection commandSection()
{
return AdminCommandSection.general();
}

@Override
public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
Expand Down Expand Up @@ -327,6 +331,11 @@ public String summary()
}

@Override
public AdminCommandSection commandSection()
{
return AdminCommandSection.general();
}

public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
return args ->
Expand Down

0 comments on commit 9f58ac6

Please sign in to comment.