Skip to content

Commit

Permalink
feat: hide stacktrace in case of failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Vacxe committed Jun 30, 2023
1 parent b27e4bf commit 4f6a73b
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/main/kotlin/com/github/vacxe/googleplaycli/core/BaseCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,78 @@ import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.validate
import com.github.vacxe.googleplaycli.PlayStoreApi
import com.google.api.client.googleapis.json.GoogleJsonResponseException
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.security.InvalidParameterException

abstract class BaseCommand(name: String, actionDescription: String = "") : CliktCommand(name = name, help = actionDescription) {
abstract class BaseCommand(name: String, actionDescription: String = "") :
CliktCommand(name = name, help = actionDescription) {

private val serviceAccountJsonFile: String by option("--config-file", "-cf", help = "service account json file path")
.default(System.getenv("PLAYSTORE_SERVICE_ACCOUNT_JSON_FILE")
?: "")
private val serviceAccountJsonFile: String by option(
"--config-file",
"-cf",
help = "service account json file path"
)
.default(
System.getenv("PLAYSTORE_SERVICE_ACCOUNT_JSON_FILE")
?: ""
)

private val serviceAccountJsonContent: String by option("--config-content", "-cc", help = "service account json content")
.default(System.getenv("PLAYSTORE_SERVICE_ACCOUNT_JSON_CONTENT")
?: "")
private val serviceAccountJsonContent: String by option(
"--config-content",
"-cc",
help = "service account json content"
)
.default(
System.getenv("PLAYSTORE_SERVICE_ACCOUNT_JSON_CONTENT")
?: ""
)

val packageName: String by option("--package-name", "-p", help = "package name (example: com.my.app)")
.default(System.getenv("APP_PACKAGE_NAME") ?: "")
.validate { require(it.isNotEmpty()) { "Please provide a valid $help" } }
.default(System.getenv("APP_PACKAGE_NAME") ?: "")
.validate { require(it.isNotEmpty()) { "Please provide a valid $help" } }

private val debug: String by option("--debug", help = "enable debug logs")
.default("false")

private val serviceAccountInputStream: InputStream
get() {
if(serviceAccountJsonFile.isNotEmpty() && serviceAccountJsonContent.isNotEmpty()) {
if (serviceAccountJsonFile.isNotEmpty() && serviceAccountJsonContent.isNotEmpty()) {
throw InvalidParameterException("Service account file or content can't be specified together")
}

if(serviceAccountJsonFile.isEmpty() && serviceAccountJsonContent.isEmpty()) {
if (serviceAccountJsonFile.isEmpty() && serviceAccountJsonContent.isEmpty()) {
throw InvalidParameterException("Service account File or Content need to be specified")
}

return when {
serviceAccountJsonContent.isNotEmpty() -> ByteArrayInputStream(serviceAccountJsonContent.toByteArray())
serviceAccountJsonFile.isNotEmpty() -> {
val serviceAccountFile = File(serviceAccountJsonFile)
if(!serviceAccountFile.exists())
if (!serviceAccountFile.exists())
throw InvalidParameterException("Service account file $serviceAccountJsonFile not exist")
FileInputStream(serviceAccountFile)
}

else -> throw InvalidParameterException("Service account input stream can't be defined")
}
}

final override fun run() {
val manager = PlayStoreApi(serviceAccountInputStream, packageName)
run(manager)?.let {
println(it)
try {
run(manager)?.let {
println(it)
}
} catch (e: GoogleJsonResponseException) {
if (debug.toBoolean()) {
e.printStackTrace()
} else {
println(e.content)
}
}
}

Expand Down

0 comments on commit 4f6a73b

Please sign in to comment.