Skip to content

Commit a7163c5

Browse files
committed
Add toml-query command
1 parent 43c1d20 commit a7163c5

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

src/main/java/me/itzg/helpers/McImageHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import me.itzg.helpers.errors.ExceptionHandler;
2121
import me.itzg.helpers.errors.ExitCodeMapper;
2222
import me.itzg.helpers.fabric.InstallFabricLoaderCommand;
23+
import me.itzg.helpers.files.TomlQueryCommand;
2324
import me.itzg.helpers.find.FindCommand;
2425
import me.itzg.helpers.forge.InstallForgeCommand;
2526
import me.itzg.helpers.forge.InstallNeoForgeCommand;
@@ -91,6 +92,7 @@
9192
Sync.class,
9293
SyncAndInterpolate.class,
9394
TestLoggingCommand.class,
95+
TomlQueryCommand.class,
9496
YamlPathCmd.class,
9597
VanillaTweaksCommand.class,
9698
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package me.itzg.helpers.files;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.dataformat.toml.TomlFactory;
6+
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
7+
import com.jayway.jsonpath.JsonPath;
8+
import java.io.IOException;
9+
import java.nio.file.Path;
10+
import java.util.Map;
11+
import java.util.concurrent.Callable;
12+
import picocli.CommandLine.Command;
13+
import picocli.CommandLine.ExitCode;
14+
import picocli.CommandLine.Parameters;
15+
16+
@Command(name = "toml-query")
17+
public class TomlQueryCommand implements Callable<Integer> {
18+
19+
public static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {
20+
};
21+
22+
@Parameters(index = "0", arity = "1",
23+
paramLabel = "query",
24+
description = "JSON path expression where root element $ can be omitted")
25+
String query;
26+
27+
@Parameters(index = "1", arity = "0..1",
28+
paramLabel = "file",
29+
description = "TOML file or reads stdin")
30+
Path path;
31+
32+
@Override
33+
public Integer call() throws Exception {
34+
final Map<String,Object> content;
35+
try (JsonParser parser = loadParser()) {
36+
if (path != null) {
37+
content = new TomlMapper().readValue(parser, MAP_TYPE);
38+
}
39+
else {
40+
content = new TomlMapper().readValue(parser, MAP_TYPE);
41+
}
42+
}
43+
final Object result = JsonPath.read(content,
44+
// if user left off root element reference, then add it
45+
// maybe using a shell where $ triggers interpolation
46+
query.startsWith("$") ? query : "$" + query
47+
);
48+
49+
System.out.println(result);
50+
51+
return ExitCode.OK;
52+
}
53+
54+
private JsonParser loadParser() throws IOException {
55+
final JsonParser parser;
56+
if (path != null) {
57+
parser = new TomlFactory().createParser(path.toFile());
58+
}
59+
else {
60+
parser = new TomlFactory().createParser(System.in);
61+
}
62+
return parser;
63+
}
64+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package me.itzg.helpers.files;
2+
3+
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
import java.nio.file.Paths;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.ValueSource;
9+
import picocli.CommandLine;
10+
import picocli.CommandLine.ExitCode;
11+
12+
class TomlQueryCommandTest {
13+
14+
@ParameterizedTest
15+
@ValueSource(strings = {"$.bind", ".bind"})
16+
void extractsVelocityBind(String queryPath) throws Exception {
17+
final String out = tapSystemOut(() -> {
18+
final int exitCode = new CommandLine(new TomlQueryCommand())
19+
.execute(
20+
queryPath,
21+
Paths.get("src/test/resources/velocity.toml").toString()
22+
);
23+
24+
assertThat(exitCode).isEqualTo(ExitCode.OK);
25+
26+
});
27+
28+
assertThat(out).isEqualTo("0.0.0.0:25565\n");
29+
}
30+
}

src/test/resources/velocity.toml

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Config version. Do not change this
2+
config-version = "2.7"
3+
4+
# What port should the proxy be bound to? By default, we'll bind to all addresses on port 25565.
5+
bind = "0.0.0.0:25565"
6+
7+
# What should be the MOTD? This gets displayed when the player adds your server to
8+
# their server list. Only MiniMessage format is accepted.
9+
motd = "<#09add3>A Velocity Server"
10+
11+
# What should we display for the maximum number of players? (Velocity does not support a cap
12+
# on the number of players online.)
13+
show-max-players = 500
14+
15+
# Should we authenticate players with Mojang? By default, this is on.
16+
online-mode = true
17+
18+
# Should the proxy enforce the new public key security standard? By default, this is on.
19+
force-key-authentication = true
20+
21+
# If client's ISP/AS sent from this proxy is different from the one from Mojang's
22+
# authentication server, the player is kicked. This disallows some VPN and proxy
23+
# connections but is a weak form of protection.
24+
prevent-client-proxy-connections = false
25+
26+
# Should we forward IP addresses and other data to backend servers?
27+
# Available options:
28+
# - "none": No forwarding will be done. All players will appear to be connecting
29+
# from the proxy and will have offline-mode UUIDs.
30+
# - "legacy": Forward player IPs and UUIDs in a BungeeCord-compatible format. Use this
31+
# if you run servers using Minecraft 1.12 or lower.
32+
# - "bungeeguard": Forward player IPs and UUIDs in a format supported by the BungeeGuard
33+
# plugin. Use this if you run servers using Minecraft 1.12 or lower, and are
34+
# unable to implement network level firewalling (on a shared host).
35+
# - "modern": Forward player IPs and UUIDs as part of the login process using
36+
# Velocity's native forwarding. Only applicable for Minecraft 1.13 or higher.
37+
player-info-forwarding-mode = "NONE"
38+
39+
# If you are using modern or BungeeGuard IP forwarding, configure a file that contains a unique secret here.
40+
# The file is expected to be UTF-8 encoded and not empty.
41+
forwarding-secret-file = "forwarding.secret"
42+
43+
# Announce whether or not your server supports Forge. If you run a modded server, we
44+
# suggest turning this on.
45+
#
46+
# If your network runs one modpack consistently, consider using ping-passthrough = "mods"
47+
# instead for a nicer display in the server list.
48+
announce-forge = false
49+
50+
# If enabled (default is false) and the proxy is in online mode, Velocity will kick
51+
# any existing player who is online if a duplicate connection attempt is made.
52+
kick-existing-players = false
53+
54+
# Should Velocity pass server list ping requests to a backend server?
55+
# Available options:
56+
# - "disabled": No pass-through will be done. The velocity.toml and server-icon.png
57+
# will determine the initial server list ping response.
58+
# - "mods": Passes only the mod list from your backend server into the response.
59+
# The first server in your try list (or forced host) with a mod list will be
60+
# used. If no backend servers can be contacted, Velocity won't display any
61+
# mod information.
62+
# - "description": Uses the description and mod list from the backend server. The first
63+
# server in the try (or forced host) list that responds is used for the
64+
# description and mod list.
65+
# - "all": Uses the backend server's response as the proxy response. The Velocity
66+
# configuration is used if no servers could be contacted.
67+
ping-passthrough = "DISABLED"
68+
69+
# If not enabled (default is true) player IP addresses will be replaced by <ip address withheld> in logs
70+
enable-player-address-logging = true
71+
72+
[servers]
73+
# Configure your servers here. Each key represents the server's name, and the value
74+
# represents the IP address of the server to connect to.
75+
lobby = "127.0.0.1:30066"
76+
factions = "127.0.0.1:30067"
77+
minigames = "127.0.0.1:30068"
78+
79+
# In what order we should try servers when a player logs in or is kicked from a server.
80+
try = [
81+
"lobby"
82+
]
83+
84+
[forced-hosts]
85+
# Configure your forced hosts here.
86+
"lobby.example.com" = [
87+
"lobby"
88+
]
89+
"factions.example.com" = [
90+
"factions"
91+
]
92+
"minigames.example.com" = [
93+
"minigames"
94+
]
95+
96+
[advanced]
97+
# How large a Minecraft packet has to be before we compress it. Setting this to zero will
98+
# compress all packets, and setting it to -1 will disable compression entirely.
99+
compression-threshold = 256
100+
101+
# How much compression should be done (from 0-9). The default is -1, which uses the
102+
# default level of 6.
103+
compression-level = -1
104+
105+
# How fast (in milliseconds) are clients allowed to connect after the last connection? By
106+
# default, this is three seconds. Disable this by setting this to 0.
107+
login-ratelimit = 3000
108+
109+
# Specify a custom timeout for connection timeouts here. The default is five seconds.
110+
connection-timeout = 5000
111+
112+
# Specify a read timeout for connections here. The default is 30 seconds.
113+
read-timeout = 30000
114+
115+
# Enables compatibility with HAProxy's PROXY protocol. If you don't know what this is for, then
116+
# don't enable it.
117+
haproxy-protocol = false
118+
119+
# Enables TCP fast open support on the proxy. Requires the proxy to run on Linux.
120+
tcp-fast-open = false
121+
122+
# Enables BungeeCord plugin messaging channel support on Velocity.
123+
bungee-plugin-message-channel = true
124+
125+
# Shows ping requests to the proxy from clients.
126+
show-ping-requests = false
127+
128+
# By default, Velocity will attempt to gracefully handle situations where the user unexpectedly
129+
# loses connection to the server without an explicit disconnect message by attempting to fall the
130+
# user back, except in the case of read timeouts. BungeeCord will disconnect the user instead. You
131+
# can disable this setting to use the BungeeCord behavior.
132+
failover-on-unexpected-server-disconnect = true
133+
134+
# Declares the proxy commands to 1.13+ clients.
135+
announce-proxy-commands = true
136+
137+
# Enables the logging of commands
138+
log-command-executions = false
139+
140+
# Enables logging of player connections when connecting to the proxy, switching servers
141+
# and disconnecting from the proxy.
142+
log-player-connections = true
143+
144+
# Allows players transferred from other hosts via the
145+
# Transfer packet (Minecraft 1.20.5) to be received.
146+
accepts-transfers = false
147+
148+
[query]
149+
# Whether to enable responding to GameSpy 4 query responses or not.
150+
enabled = false
151+
152+
# If query is enabled, on what port should the query protocol listen on?
153+
port = 25565
154+
155+
# This is the map name that is reported to the query services.
156+
map = "Velocity"
157+
158+
# Whether plugins should be shown in query response by default or not
159+
show-plugins = false

0 commit comments

Comments
 (0)