-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
McImageHelper.java
195 lines (176 loc) · 6.61 KB
/
McImageHelper.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package me.itzg.helpers;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import me.itzg.helpers.assertcmd.AssertCommand;
import me.itzg.helpers.curseforge.CurseForgeFilesCommand;
import me.itzg.helpers.curseforge.InstallCurseForgeCommand;
import me.itzg.helpers.errors.ExceptionHandler;
import me.itzg.helpers.errors.ExitCodeMapper;
import me.itzg.helpers.fabric.InstallFabricLoaderCommand;
import me.itzg.helpers.find.FindCommand;
import me.itzg.helpers.forge.InstallForgeCommand;
import me.itzg.helpers.forge.InstallNeoForgeCommand;
import me.itzg.helpers.get.GetCommand;
import me.itzg.helpers.github.GithubCommands;
import me.itzg.helpers.modrinth.InstallModrinthModpackCommand;
import me.itzg.helpers.modrinth.ModrinthCommand;
import me.itzg.helpers.mvn.MavenDownloadCommand;
import me.itzg.helpers.paper.InstallPaperCommand;
import me.itzg.helpers.patch.PatchCommand;
import me.itzg.helpers.properties.SetPropertiesCommand;
import me.itzg.helpers.purpur.InstallPurpurCommand;
import me.itzg.helpers.quilt.InstallQuiltCommand;
import me.itzg.helpers.singles.Asciify;
import me.itzg.helpers.singles.HashCommand;
import me.itzg.helpers.singles.NetworkInterfacesCommand;
import me.itzg.helpers.singles.YamlPathCmd;
import me.itzg.helpers.sync.InterpolateCommand;
import me.itzg.helpers.sync.MulitCopyCommand;
import me.itzg.helpers.sync.Sync;
import me.itzg.helpers.sync.SyncAndInterpolate;
import me.itzg.helpers.users.ManageUsersCommand;
import me.itzg.helpers.vanillatweaks.VanillaTweaksCommand;
import me.itzg.helpers.versions.CompareVersionsCommand;
import me.itzg.helpers.versions.JavaReleaseCommand;
import me.itzg.helpers.versions.ResolveMinecraftVersionCommand;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.IVersionProvider;
import picocli.CommandLine.Option;
@Command(name = "mc-image-helper",
versionProvider = McImageHelper.AppVersionProvider.class,
subcommands = {
Asciify.class,
AssertCommand.class,
CompareVersionsCommand.class,
CurseForgeFilesCommand.class,
FindCommand.class,
GetCommand.class,
GithubCommands.class,
HashCommand.class,
InstallCurseForgeCommand.class,
InstallFabricLoaderCommand.class,
InstallForgeCommand.class,
InstallModrinthModpackCommand.class,
InstallNeoForgeCommand.class,
InstallPaperCommand.class,
InstallPurpurCommand.class,
InstallQuiltCommand.class,
InterpolateCommand.class,
JavaReleaseCommand.class,
ManageUsersCommand.class,
MavenDownloadCommand.class,
ModrinthCommand.class,
MulitCopyCommand.class,
NetworkInterfacesCommand.class,
PatchCommand.class,
ResolveMinecraftVersionCommand.class,
SetPropertiesCommand.class,
Sync.class,
SyncAndInterpolate.class,
YamlPathCmd.class,
VanillaTweaksCommand.class,
}
)
@Slf4j
public class McImageHelper {
//language=RegExp
public static final String OPTION_SPLIT_COMMAS = "\\s*,\\s*";
//language=RegExp
public static final String SPLIT_COMMA_NL = "\\n|\\s*,\\s*";
public static final String SPLIT_SYNOPSIS_COMMA_NL = ",|<nl>";
//language=RegExp
public static final String VERSION_REGEX = "\\d+(\\.\\d+)+";
@Option(names = {"-h",
"--help"}, usageHelp = true, description = "Show this usage and exit")
boolean showHelp;
@Option(names = {"-V", "--version"}, versionHelp = true)
boolean showVersion;
@ArgGroup
LoggingOptions loggingOptions = new LoggingOptions();
static class LoggingOptions {
@Option(names = "--debug", description = "Enable debug output."
+ " Can also set environment variables DEBUG_HELPER or DEBUG",
defaultValue = "${env:DEBUG_HELPER:-${env:DEBUG}}")
void setDebug(boolean enabled) {
setLevel(enabled, Level.DEBUG);
}
@Option(names = "--logging", description = "Set logging to specific level.\nValid values: ${COMPLETION-CANDIDATES}",
defaultValue = "${env:HELPER_LOGGING_LEVEL}",
converter = LogbackLevelConverter.class
)
void setLoggingLevel(Level level) {
setLevel(true, level);
}
private static void setLevel(boolean enabled, Level level) {
((Logger) LoggerFactory.getLogger("me.itzg.helpers")).setLevel(
enabled ? level : Level.INFO);
if (Level.TRACE.isGreaterOrEqual(level)) {
((Logger) LoggerFactory.getLogger("org.apache.hc.client5.http")).setLevel(
enabled ? level : Level.INFO);
}
}
}
@Option(names = {"-s", "--silent"}, description = "Don't output logs even if there's an error")
@Getter
boolean silent;
@Getter
private static String version;
public static void main(String[] args) {
final McImageHelper rootCommand = new McImageHelper();
try {
version = McImageHelper.loadVersion();
} catch (IOException e) {
log.error("Failed to load version", e);
System.exit(1);
}
System.exit(
new CommandLine(rootCommand)
.setExitCodeExceptionMapper(new ExitCodeMapper())
.setExecutionExceptionHandler(new ExceptionHandler(rootCommand))
.setCaseInsensitiveEnumValuesAllowed(true)
.execute(args)
);
}
private static String loadVersion() throws IOException {
final Enumeration<URL> resources = McImageHelper.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
final URL url = resources.nextElement();
try (InputStream inputStream = url.openStream()) {
final Manifest manifest = new Manifest(inputStream);
final Attributes attributes = manifest.getMainAttributes();
if ("mc-image-helper".equals(attributes.getValue(Name.IMPLEMENTATION_TITLE))) {
return attributes.getValue(Name.IMPLEMENTATION_VERSION);
}
}
}
return "???";
}
public static class AppVersionProvider implements IVersionProvider {
@Override
public String[] getVersion() {
return new String[]{
"${COMMAND-FULL-NAME}",
version
};
}
}
private static class LogbackLevelConverter implements ITypeConverter<Level> {
@Override
public Level convert(String value) {
return Level.toLevel(value);
}
}
}