1616import org .jline .reader .impl .DefaultParser ;
1717import org .jline .terminal .Terminal ;
1818import org .jline .terminal .TerminalBuilder ;
19+ import org .json .JSONArray ;
20+ import org .json .JSONObject ;
21+ import org .json .JSONTokener ;
1922import picocli .CommandLine ;
2023
2124import javax .annotation .Nonnull ;
2225import java .io .IOException ;
26+ import java .io .InputStream ;
27+ import java .net .URI ;
28+ import java .net .URL ;
29+ import java .net .URLConnection ;
2330import java .util .List ;
2431import java .util .Objects ;
32+ import java .util .Optional ;
2533import java .util .stream .Collectors ;
2634
2735/**
4654 mixinStandardHelpOptions = true ,
4755 version = "Print version information"
4856)
49- public class Cli extends Command {
57+ public class Cli extends Cmd {
58+ public static String version ;
59+
5060 /** Command line specification. */
5161 private @ CommandLine .Spec CommandLine .Model .CommandSpec spec ;
5262
5363 /** Unmatched command line arguments. */
5464 private @ CommandLine .Unmatched List <String > args ;
5565
56- @ Override public void run (@ Nonnull CommandContext ctx ) {
66+ @ Override public void run (@ Nonnull Context ctx ) {
5767 List <String > args = this .args .stream ()
5868 .filter (Objects ::nonNull )
5969 .map (String ::trim )
@@ -64,7 +74,7 @@ public class Cli extends Command {
6474 if ("-h" .equals (arg ) || "--help" .equals (arg )) {
6575 ctx .println (spec .commandLine ().getUsageMessage ());
6676 } else if ("-V" .equalsIgnoreCase (arg ) || "--version" .equals (arg )) {
67- ctx .println (VersionProvider . version ());
77+ ctx .println (ctx . getVersion ());
6878 } else {
6979 ctx .println ("Unknown command or option(s): " + args .stream ().collect (Collectors .joining (" " )));
7080 }
@@ -78,11 +88,13 @@ public class Cli extends Command {
7888 * @throws IOException If something goes wrong.
7989 */
8090 public static void main (String [] args ) throws IOException {
91+ version = checkVersion ();
8192 // set up the completion
8293 Cli jooby = new Cli ();
8394 CommandLine cmd = new CommandLine (jooby )
84- .addSubcommand (new CreateApp ())
85- .addSubcommand (new Exit ());
95+ .addSubcommand (new CreateCmd ())
96+ .addSubcommand (new ExitCmd ())
97+ .addSubcommand (new SetCmd ());
8698
8799 Terminal terminal = TerminalBuilder .builder ().build ();
88100 LineReader reader = LineReaderBuilder .builder ()
@@ -91,12 +103,12 @@ public static void main(String[] args) throws IOException {
91103 .parser (new DefaultParser ())
92104 .build ();
93105
94- CommandContextImpl context = new CommandContextImpl (reader );
106+ CommandContextImpl context = new CommandContextImpl (reader , version );
95107 jooby .setContext (context );
96108 cmd .getSubcommands ().values ().stream ()
97109 .map (CommandLine ::getCommand )
98- .filter (Command .class ::isInstance )
99- .map (Command .class ::cast )
110+ .filter (Cmd .class ::isInstance )
111+ .map (Cmd .class ::cast )
100112 .forEach (command -> command .setContext (context ));
101113
102114 if (args .length > 0 ) {
@@ -119,4 +131,25 @@ public static void main(String[] args) throws IOException {
119131 }
120132 }
121133 }
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+ }
122155}
0 commit comments