Skip to content

Commit

Permalink
Cli now explicitly handles UserInterruptException and EndOfFileException
Browse files Browse the repository at this point in the history
  • Loading branch information
ppanopticon committed Mar 9, 2021
1 parent fd6bec6 commit 3bcae34
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions backend/src/main/kotlin/dev/dres/api/cli/Cli.kt
Expand Up @@ -13,11 +13,11 @@ import org.jline.builtins.Completers
import org.jline.reader.*
import org.jline.reader.impl.completer.AggregateCompleter
import org.jline.reader.impl.completer.StringsCompleter
import org.jline.terminal.Terminal
import org.jline.terminal.TerminalBuilder
import java.io.IOException
import java.util.*
import java.util.regex.Pattern
import kotlin.system.exitProcess


object Cli {
Expand Down Expand Up @@ -46,14 +46,11 @@ object Cli {
ExecutionCommand()
)

var terminal: Terminal? = null
try {
terminal = TerminalBuilder.terminal() //basic terminal
val terminal = try {
TerminalBuilder.builder().streams(System.`in`, System.out).build()
} catch (e: IOException) {
System.err.println("Could not initialize Terminal: ")
System.err.println(e.message)
System.err.println("Exiting...")
System.exit(-1)
System.err.println("Could not initialize terminal: ${e.message}")
exitProcess(-1)
}

val completer = DelegateCompleter(
Expand Down Expand Up @@ -88,36 +85,37 @@ object Cli {
.build()

while (true) {

val line = lineReader.readLine(PROMPT).trim()
if (line.toLowerCase() == "exit" || line.toLowerCase() == "quit") {
break
}
if (line.toLowerCase() == "help") {
println(clikt.getFormattedHelp()) //TODO overwrite with something more useful in a cli context
continue
}
if (line.isBlank()) {
continue
}

try {
execute(line)
} catch (e: Exception) {

when (e) {
is com.github.ajalt.clikt.core.NoSuchSubcommand -> println("command not found")
is com.github.ajalt.clikt.core.PrintHelpMessage -> println(e.command.getFormattedHelp())
is com.github.ajalt.clikt.core.MissingParameter -> println(e.localizedMessage)
is com.github.ajalt.clikt.core.NoSuchOption -> println(e.localizedMessage)
else -> e.printStackTrace()
val line = lineReader.readLine(PROMPT).trim()
if (line.toLowerCase() == "exit" || line.toLowerCase() == "quit") {
break
}
if (line.toLowerCase() == "help") {
println(clikt.getFormattedHelp()) //TODO overwrite with something more useful in a cli context
continue
}
if (line.isBlank()) {
continue
}

try {
execute(line)
} catch (e: Exception) {
when (e) {
is com.github.ajalt.clikt.core.NoSuchSubcommand -> println("command not found")
is com.github.ajalt.clikt.core.PrintHelpMessage -> println(e.command.getFormattedHelp())
is com.github.ajalt.clikt.core.MissingParameter -> println(e.localizedMessage)
is com.github.ajalt.clikt.core.NoSuchOption -> println(e.localizedMessage)
else -> e.printStackTrace()
}
}
} catch (e: EndOfFileException) {
System.err.println("Could not read from terminal due to EOF. If you're running DRES in Docker, try running the container in interactive mode.")
break
} catch (e: UserInterruptException) {
break
}

}


}

fun execute(line: String){
Expand Down

0 comments on commit 3bcae34

Please sign in to comment.