/
CLI.java
107 lines (96 loc) · 3.91 KB
/
CLI.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
/* Copyright (c) 2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - extract main() method from GeoGigCLI
*/
package org.locationtech.geogig.cli.app;
import org.locationtech.geogig.cli.CLIContextBuilder;
import org.locationtech.geogig.cli.Console;
import org.locationtech.geogig.cli.GeogigCLI;
import org.locationtech.geogig.porcelain.ConfigOp;
import org.locationtech.geogig.porcelain.ConfigOp.ConfigAction;
import org.locationtech.geogig.porcelain.ConfigOp.ConfigScope;
import org.locationtech.geogig.repository.Hints;
import org.locationtech.geogig.repository.impl.GeoGIG;
import org.locationtech.geogig.repository.impl.GlobalContextBuilder;
public class CLI {
/**
* Entry point for the command line interface.
*
* @param args
*/
public static void main(String[] args) {
String repoURI = null;
if (args.length > 1 && "--repo".equals(args[0])) {
repoURI = args[1];
String[] norepoArgs = new String[args.length - 2];
if (args.length > 2) {
System.arraycopy(args, 2, norepoArgs, 0, args.length - 2);
}
args = norepoArgs;
}
GlobalContextBuilder.builder(new CLIContextBuilder());
Logging.tryConfigureLogging();
Console consoleReader = new Console();
final GeogigCLI cli = new GeogigCLI(consoleReader);
cli.setRepositoryURI(repoURI);
GeoGIG geogig = cli.getGeogig();
boolean disableAnsi = false;
boolean closeIt = geogig == null;
if (closeIt) {
// we're not in a repository, need a geogig anyways to check the global config
geogig = cli.newGeoGIG(Hints.readOnly());
if (geogig.command(ConfigOp.class).setScope(ConfigScope.GLOBAL)
.setAction(ConfigAction.CONFIG_GET).setName("ansi.enabled").toString()
.equalsIgnoreCase("false")) {
disableAnsi = true;
} else if (geogig.command(ConfigOp.class).setScope(ConfigScope.GLOBAL)
.setAction(ConfigAction.CONFIG_GET).setName("ansi.enabled").toString()
.equalsIgnoreCase("true")) {
disableAnsi = false;
}
geogig.close();
} else {
if (geogig.getRepository().configDatabase().get("ansi.enabled").or("")
.equalsIgnoreCase("false")) {
cli.getConsole().setForceAnsi(false);
disableAnsi = true;
} else if (geogig.getRepository().configDatabase().get("ansi.enabled").or("")
.equalsIgnoreCase("true")) {
disableAnsi = false;
cli.getConsole().setForceAnsi(true);
}
}
if (disableAnsi) {
cli.getConsole().disableAnsi();
} else {
cli.getConsole().enableAnsi();
}
addShutdownHook(cli);
int exitCode = cli.execute(args);
if (exitCode != 0 || cli.isExitOnFinish()) {
cli.close();
System.exit(exitCode);
}
}
static void addShutdownHook(final GeogigCLI cli) {
// try to grafefully shutdown upon CTRL+C
Runtime.getRuntime().addShutdownHook(new Thread() {
private GeogigCLI geogig = cli;
@Override
public void run() {
if (cli.isRunning()) {
System.err.println("Forced shut down, wait for geogig to be closed...");
System.err.flush();
geogig.close();
System.err.println("geogig closed.");
System.err.flush();
}
}
});
}
}