-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.java
92 lines (82 loc) · 2.4 KB
/
Program.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
package io.belin.which;
import java.io.File;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.List;
import java.util.stream.Collectors;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.IVersionProvider;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
/**
* Find the instances of an executable in the system path.
*/
@Command(
name = "which",
description = "Find the instances of an executable in the system path.",
mixinStandardHelpOptions = true,
versionProvider = Program.class
)
class Program implements Callable<Integer>, IVersionProvider {
/**
* Value indicating whether to list all executables found.
*/
@Option(
names = {"-a", "--all"},
description = "List all executable instances found (instead of just the first one)."
)
private boolean all;
/**
* The name of the executable to find.
*/
@Parameters(
index = "0",
description = "The name of the executable to find."
)
private String command;
/**
* Value indicating whether to silence the output.
*/
@Option(
names = {"-s", "--silent"},
description = "Silence the output, just return the exit code (0 if any executable is found, otherwise 404)."
)
private boolean silent;
/**
* Application entry point.
* @param args The command line arguments.
*/
public static void main(String... args) {
System.exit(new CommandLine(new Program()).execute(args));
}
/**
* Runs this program.
* @return The exit code.
*/
@Override
@SuppressWarnings("PMD.SystemPrintln")
public Integer call() {
var finder = new Finder();
var resultSet = new Finder.ResultSet(command, finder);
var executables = all ? resultSet.all() : resultSet.first().map(List::of);
if (!silent) {
if (executables.isPresent()) {
var paths = executables.get().stream().map(Path::toString);
System.out.println(paths.collect(Collectors.joining(System.lineSeparator())));
}
else {
var paths = finder.paths.stream().map(Path::toString);
System.err.printf("No '%s' in (%s)%n", command, paths.collect(Collectors.joining(Finder.isWindows ? ";" : File.pathSeparator)));
}
}
return executables.isEmpty() ? 404 : 0;
}
/**
* Gets the package version of this program.
* @return The package version of this program.
*/
@Override public String[] getVersion() {
return new String[] {getClass().getPackage().getImplementationVersion()};
}
}