Skip to content

Commit

Permalink
Update check now works
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-richardson committed Nov 14, 2019
1 parent 02a8bcf commit f8d9cbd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/web3j/console/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public static void main(String[] args) throws Exception {
Updater updater = new Updater(config);
if (!updater.updateCurrentlyAvailable()) {
new Thread(updater::onlineUpdateCheck).start();
} else {
System.out.println(String.format("A Web3j update is available; please run the following command to update: %s", config.getUpdatePrompt()));
}


Thread.sleep(20000);
if (args.length < 1) {
Console.exitError(USAGE);
Expand Down Expand Up @@ -85,7 +86,5 @@ public static void main(String[] args) throws Exception {
Console.exitError(USAGE);
}
}


}
}
18 changes: 11 additions & 7 deletions src/main/java/org/web3j/console/config/CliConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

public class CliConfig {
private static File web3jHome = new File(System.getProperty("user.home"), ".web3j");
private static File web3jConfigFile = new File(web3jHome, ".config");

private static CliConfig initializeDefaultConfig(File configFile) throws IOException {
if (!web3jHome.exists() && !web3jHome.mkdirs()) {
Expand All @@ -39,7 +40,6 @@ private static CliConfig getSavedConfig(File configFile) throws IOException {
}

public static CliConfig getConfig() throws IOException {
File web3jConfigFile = new File(web3jHome, ".config");
if (web3jConfigFile.exists()) {
return getSavedConfig(web3jConfigFile);
} else {
Expand Down Expand Up @@ -69,7 +69,8 @@ public static OS determineOS() {
return OS.DARWIN;
} else if (osName.toLowerCase().startsWith("linux")) {
return OS.LINUX;
} else if (osName.toLowerCase().startsWith("sunos") || osName.toLowerCase().startsWith("solaris")) {
} else if (osName.toLowerCase().startsWith("sunos")
|| osName.toLowerCase().startsWith("solaris")) {
return OS.SOLARIS;
} else if (osName.toLowerCase().startsWith("aix")) {
return OS.AIX;
Expand All @@ -84,16 +85,15 @@ public static OS determineOS() {
}
}


private transient final String version;
private final transient String version;
private String clientId;

private CliConfig() throws IOException {
version = Version.getVersion();
}

public CliConfig(
String clientId, boolean updateAvailable, String updatePrompt) throws IOException {
public CliConfig(String clientId, boolean updateAvailable, String updatePrompt)
throws IOException {
version = Version.getVersion();
this.clientId = clientId;
this.updateAvailable = updateAvailable;
Expand All @@ -103,7 +103,6 @@ public CliConfig(
private boolean updateAvailable;
private String updatePrompt;


public String getVersion() {
return version;
}
Expand All @@ -127,4 +126,9 @@ public String getUpdatePrompt() {
public void setUpdatePrompt(String updatePrompt) {
this.updatePrompt = updatePrompt;
}

public void save() throws IOException {
String jsonToWrite = new Gson().toJson(this);
Files.writeString(web3jConfigFile.toPath(), jsonToWrite);
}
}
26 changes: 20 additions & 6 deletions src/main/java/org/web3j/console/update/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
*/
package org.web3j.console.update;

import java.io.IOException;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.web3j.console.config.CliConfig;

import java.io.IOException;
import org.web3j.console.config.CliConfig;
import org.web3j.utils.Version;

public class Updater {

private static final String BASE_URL = "http://localhost:8000";


private CliConfig config;

public Updater(CliConfig config) {
Expand Down Expand Up @@ -63,12 +64,25 @@ public void onlineUpdateCheck() {
Response sendRawResponse = client.newCall(updateCheckRequest).execute();
if (sendRawResponse.code() == 200) {
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(sendRawResponse.body().string()).getAsJsonObject();
System.out.println(rootObj.toString());
JsonObject rootObj =
parser.parse(sendRawResponse.body().string())
.getAsJsonObject()
.get("latest")
.getAsJsonObject();
String currentVersion = rootObj.get("version").getAsString();
if (!currentVersion.equals(Version.getVersion())) {
config.setUpdateAvailable(true);
config.setUpdatePrompt(
rootObj.get(
CliConfig.determineOS() == CliConfig.OS.WINDOWS
? "install_win"
: "install_unix")
.getAsString());
config.save();
}
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

0 comments on commit f8d9cbd

Please sign in to comment.