-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Goksi/version-control
Adding version control on startup
- Loading branch information
Showing
8 changed files
with
77 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package tech.goksi.pterobot.util | ||
|
||
import dev.minn.jda.ktx.util.SLF4J | ||
import net.dv8tion.jda.api.utils.data.DataObject | ||
import okhttp3.* | ||
import java.io.IOException | ||
import java.util.Properties | ||
|
||
object VersionCheck { | ||
private val logger by SLF4J | ||
|
||
private const val API_URL = "https://api.github.com/repos/Goksi/PteroBot/releases/latest" | ||
|
||
fun checkVersion() { | ||
val properties = Properties() | ||
.apply { this.load(VersionCheck::class.java.classLoader.getResourceAsStream("version.properties")) } | ||
val currentVer = properties.getProperty("version")!! | ||
val client = OkHttpClient() | ||
val request = Request.Builder() | ||
.url(API_URL) | ||
.header("accept", "application/vnd.github+json") | ||
.get() | ||
.build() | ||
client.newCall(request).enqueue(object : Callback { | ||
override fun onFailure(call: Call, e: IOException) { | ||
logger.error("Error while requesting version !", e) | ||
} | ||
|
||
override fun onResponse(call: Call, response: Response) { | ||
response.use { | ||
if (!response.isSuccessful) { | ||
logger.error("Error while requesting version, unexpected status code: $response") | ||
return | ||
} | ||
val fetchedVer = DataObject.fromJson(response.body!!.byteStream()) | ||
.getString("tag_name") | ||
|
||
if (compareVersions(fetchedVer.replace("v", ""), currentVer) > 0) | ||
logger.warn("You are not running latest version of PteroBot ! Latest: $fetchedVer Current: v$currentVer") | ||
else logger.info("You are running latest version of PteroBot !") | ||
} | ||
|
||
} | ||
}) | ||
} | ||
|
||
private fun compareVersions(version1: String, version2: String): Int { | ||
var result = 0 | ||
|
||
val ver1List = version1.split(".") | ||
val ver2List = version2.split(".") | ||
|
||
for (i in 0..2) { | ||
val v1 = ver1List[i].toInt() | ||
val v2 = ver2List[i].toInt() | ||
val compare = v1.compareTo(v2) | ||
if (compare != 0) { | ||
result = compare | ||
break | ||
} | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version = ${project.version} |