-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AdminTool.java
151 lines (134 loc) · 4.38 KB
/
AdminTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2002-2016 "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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.commandline.admin;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
import static java.lang.String.format;
public class AdminTool
{
public static void main( String[] args )
{
Path homeDir = Paths.get( System.getenv( "NEO4J_HOME" ) );
Path configDir = Paths.get( System.getenv( "NEO4J_CONF" ) );
String extraHelp = System.getenv( "NEO4J_EXTRA_HELP" );
boolean debug = System.getenv( "NEO4J_DEBUG" ) != null;
AdminTool tool = new AdminTool( CommandLocator.fromServiceLocator(), System.out::println, extraHelp, debug );
Result result = tool.execute( homeDir, configDir, args );
result.exit();
}
private final String scriptName = "neo4j-admin";
private final CommandLocator locator;
private final Output out;
private final boolean debug;
private final Usage usage;
public AdminTool( CommandLocator locator, Output out, String extraHelp, boolean debug )
{
this.locator = CommandLocator.withAdditionalCommand( help(), locator );
this.out = out;
this.debug = debug;
this.usage = new Usage( scriptName, out, this.locator, extraHelp );
}
public Result execute( Path homeDir, Path configDir, String... args )
{
try
{
String name = args[0];
String[] commandArgs = Arrays.copyOfRange( args, 1, args.length );
AdminCommand.Provider provider;
try
{
provider = locator.apply( name );
}
catch ( NoSuchElementException e )
{
return badUsage( name, commandArgs );
}
AdminCommand command = provider.create( homeDir, configDir );
try
{
command.execute( commandArgs );
return success();
}
catch ( IncorrectUsage e )
{
return badUsage( provider, e );
}
catch ( CommandFailed e )
{
return failure( e );
}
}
catch ( RuntimeException e )
{
return unexpected( e );
}
}
private Supplier<AdminCommand.Provider> help()
{
return () -> new HelpCommand.Provider( usage );
}
private Result badUsage( AdminCommand.Provider command, IncorrectUsage e )
{
new Usage.CommandUsage( command, out, scriptName ).print();
return failure( e.getMessage() );
}
private Result badUsage( String name, String[] commandArgs )
{
usage.print();
String message = commandArgs.length == 0 ? format( "unrecognized command: %s", name ) : commandArgs[0];
return failure( message );
}
private Result unexpected( RuntimeException e )
{
return failure( e, "unexpected error: " + e.getMessage() );
}
private Result failure( Throwable e )
{
if ( debug )
{
return failure( e, e.getMessage() );
}
else
{
return failure( e.getMessage() );
}
}
private Result failure( Throwable e, String message )
{
return () -> {
e.printStackTrace();
failure( message ).exit();
};
}
private static Result failure( String message )
{
return () -> {
System.err.println( message );
System.exit( 1 );
};
}
private static Result success()
{
return () -> System.exit( 0 );
}
}