Skip to content

Commit 0cb3a9b

Browse files
committed
cli: refactor + checkstyle fixes
1 parent ed7d6cf commit 0cb3a9b

File tree

6 files changed

+66
-54
lines changed

6 files changed

+66
-54
lines changed

jooby/src/main/java/io/jooby/XSS.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* Set of escaping routines for fixing cross-site scripting (XSS).
1818
*/
1919
public final class XSS {
20-
private XSS() {}
20+
private XSS() {
21+
}
2122

2223
/**
2324
* <p>

modules/jooby-cli/src/main/java/io/jooby/cli/Cli.java

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import io.jooby.internal.cli.CommandContextImpl;
99
import io.jooby.internal.cli.JLineCompleter;
10-
import io.jooby.internal.cli.VersionProvider;
1110
import org.jline.reader.EndOfFileException;
1211
import org.jline.reader.LineReader;
1312
import org.jline.reader.LineReaderBuilder;
@@ -16,20 +15,12 @@
1615
import org.jline.reader.impl.DefaultParser;
1716
import org.jline.terminal.Terminal;
1817
import org.jline.terminal.TerminalBuilder;
19-
import org.json.JSONArray;
20-
import org.json.JSONObject;
21-
import org.json.JSONTokener;
2218
import picocli.CommandLine;
2319

2420
import javax.annotation.Nonnull;
2521
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.net.URI;
28-
import java.net.URL;
29-
import java.net.URLConnection;
3022
import java.util.List;
3123
import java.util.Objects;
32-
import java.util.Optional;
3324
import java.util.stream.Collectors;
3425

3526
/**
@@ -50,13 +41,11 @@
5041
*/
5142
@CommandLine.Command(
5243
name = "jooby",
53-
versionProvider = VersionProvider.class,
44+
versionProvider = Version.class,
5445
mixinStandardHelpOptions = true,
5546
version = "Print version information"
5647
)
5748
public class Cli extends Cmd {
58-
public static String version;
59-
6049
/** Command line specification. */
6150
private @CommandLine.Spec CommandLine.Model.CommandSpec spec;
6251

@@ -76,7 +65,8 @@ public class Cli extends Cmd {
7665
} else if ("-V".equalsIgnoreCase(arg) || "--version".equals(arg)) {
7766
ctx.println(ctx.getVersion());
7867
} else {
79-
ctx.println("Unknown command or option(s): " + args.stream().collect(Collectors.joining(" ")));
68+
ctx.println(
69+
"Unknown command or option(s): " + args.stream().collect(Collectors.joining(" ")));
8070
}
8171
}
8272
}
@@ -88,7 +78,6 @@ public class Cli extends Cmd {
8878
* @throws IOException If something goes wrong.
8979
*/
9080
public static void main(String[] args) throws IOException {
91-
version = checkVersion();
9281
// set up the completion
9382
Cli jooby = new Cli();
9483
CommandLine cmd = new CommandLine(jooby)
@@ -103,7 +92,7 @@ public static void main(String[] args) throws IOException {
10392
.parser(new DefaultParser())
10493
.build();
10594

106-
CommandContextImpl context = new CommandContextImpl(reader, version);
95+
CommandContextImpl context = new CommandContextImpl(reader, Version.VERSION);
10796
jooby.setContext(context);
10897
cmd.getSubcommands().values().stream()
10998
.map(CommandLine::getCommand)
@@ -131,25 +120,4 @@ public static void main(String[] args) throws IOException {
131120
}
132121
}
133122
}
134-
135-
private static String checkVersion() {
136-
try {
137-
URL url = URI
138-
.create("http://search.maven.org/solrsearch/select?q=+g:io.jooby+a:jooby&start=0&rows=1")
139-
.toURL();
140-
URLConnection connection = url.openConnection();
141-
try(InputStream in = connection.getInputStream()) {
142-
JSONObject json = new JSONObject(new JSONTokener(in));
143-
JSONObject response = json.getJSONObject("response");
144-
JSONArray docs = response.getJSONArray("docs");
145-
JSONObject jooby = docs.getJSONObject(0);
146-
return jooby.getString("latestVersion");
147-
}
148-
} catch (Exception x) {
149-
return Optional.ofNullable(VersionProvider.class.getPackage())
150-
.map(Package::getImplementationVersion)
151-
.filter(Objects::nonNull)
152-
.orElse("2.0.5");
153-
}
154-
}
155123
}

modules/jooby-cli/src/main/java/io/jooby/cli/Context.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.jooby.cli;
77

88
import javax.annotation.Nonnull;
9-
import java.io.FileNotFoundException;
109
import java.io.IOException;
1110
import java.nio.file.Path;
1211
import java.nio.file.attribute.PosixFilePermission;
@@ -90,6 +89,7 @@ void copyResource(@Nonnull String source, @Nonnull Path dest,
9089
* Set workspace/working directory.
9190
*
9291
* @param workspace Workspace/working directory.
92+
* @throws IOException When directory doesn't exist.
9393
*/
9494
void setWorkspace(@Nonnull Path workspace) throws IOException;
9595
}

modules/jooby-cli/src/main/java/io/jooby/cli/SetCmd.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
import javax.annotation.Nonnull;
1111
import java.nio.file.Path;
1212

13+
/**
14+
* Set/configure options used by the tool (for now is just for workspace).
15+
*
16+
* @since 2.0.6
17+
*/
1318
@CommandLine.Command(name = "set", description = "Set and save options in the ~/.jooby file")
1419
public class SetCmd extends Cmd {
1520
@CommandLine.Spec
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.cli;
7+
8+
import org.json.JSONArray;
9+
import org.json.JSONObject;
10+
import org.json.JSONTokener;
11+
import picocli.CommandLine;
12+
13+
import java.io.InputStream;
14+
import java.net.URI;
15+
import java.net.URL;
16+
import java.net.URLConnection;
17+
import java.util.Objects;
18+
import java.util.Optional;
19+
20+
/**
21+
* Jooby version. It try to fetch latest version from maven repository or fallback to package
22+
* implementation version.
23+
*
24+
*/
25+
public class Version implements CommandLine.IVersionProvider {
26+
27+
/** VERSION. */
28+
public static final String VERSION = doVersion();
29+
30+
@Override public String[] getVersion() {
31+
return new String[]{VERSION};
32+
}
33+
34+
private static String doVersion() {
35+
try {
36+
URL url = URI
37+
.create("http://search.maven.org/solrsearch/select?q=+g:io.jooby+a:jooby&start=0&rows=1")
38+
.toURL();
39+
URLConnection connection = url.openConnection();
40+
try (InputStream in = connection.getInputStream()) {
41+
JSONObject json = new JSONObject(new JSONTokener(in));
42+
JSONObject response = json.getJSONObject("response");
43+
JSONArray docs = response.getJSONArray("docs");
44+
JSONObject jooby = docs.getJSONObject(0);
45+
return jooby.getString("latestVersion");
46+
}
47+
} catch (Exception x) {
48+
return Optional.ofNullable(Version.class.getPackage())
49+
.map(Package::getImplementationVersion)
50+
.filter(Objects::nonNull)
51+
.orElse("2.0.5");
52+
}
53+
}
54+
}

modules/jooby-cli/src/main/java/io/jooby/internal/cli/VersionProvider.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)