Omdat Kotlin de beschikking heeft over alle libraries die Java ook heeft en daarnaast overal werkt waar de JVM draait.Sandor_Clegane schreef op zaterdag 15 september 2018 @ 11:23:
Hmmm, waarom zou ik Kotlin over F# nemen? Serieuze vraag, geen flamebait ofzo.
Voor mijn hobbyprojecten thuis - waar alles op Linux draait - betekent dit dat ik een tool in handen heb dat goed ondersteund wordt op dit platform.
F# daarentegen doet het goed op .Net en zou juist op mijn werk - waar het meeste op Windows draait - beter tot zijn recht komen.
Maar eerlijk is eerlijk, Kotlin is vooral interessant omdat Java lang stil gestaan heeft.
De redenen dat ik op mijn werk geen F# gebruik, zijn:
1. Het bij Microsoft vaak een after thought was. Ze hebben eigenlijk pas recent hun commitment aan F# weer laten zien.
2. C# zoveel nieuwe features heeft gekregen, dat de noodzaak om F# te gebruiken vrij klein is.
PS
Ik heb alles al een beetje gemoderniseerd. De BufferedReader wordt nu netjes gesloten met een Kotlin use constructie, en ik gebruik een immutable data class voor de JobDetails en JobResult
Sowieso heb ik zoveel mogelijk var's vervangen door val's.
toon volledige berichtKotlin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 package mailer import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.dataformat.xml.XmlMapper import java.nio.file.Files import java.nio.file.Paths import org.apache.commons.mail.DefaultAuthenticator import org.apache.commons.mail.HtmlEmail import java.io.BufferedReader import java.io.InputStreamReader import java.net.InetAddress class MailConfiguration { var server: String = "127.0.0.1" var port: Int = 25 var useSSL: Boolean = false var useCredentials: Boolean = false var username: String? = null var password: String? = null } fun initMailConfiguration(): MailConfiguration { val xmlMapper = XmlMapper() xmlMapper.enable(SerializationFeature.INDENT_OUTPUT) val homePath = Paths.get(System.getProperty("user.home")) val configPath = homePath.resolve("Mailer.Config") if (Files.exists(configPath)) { val result = xmlMapper.readValue(configPath.toFile(), MailConfiguration::class.java) return result } else { var defaultConfiguration = MailConfiguration() xmlMapper.writeValue(configPath.toFile(), defaultConfiguration) return defaultConfiguration } } data class JobDetails ( val mailTo: String, val mailFrom: String, val jobName: String, val arguments: Array<String> ) class JobException(override var message: String) : Exception(message) fun initJobDetails(args: Array<String>): JobDetails { val mailTo = System.getenv("MAILTO") val mailFrom = System.getenv("MAILFROM") val jobName = System.getenv("JOBNAME") if (mailTo == null) throw JobException("MAILTO environment variable not set") if (mailFrom == null) throw JobException("MAILFROM environment variable not set") if (jobName == null) throw JobException("JOBNAME environment variable not set") if (args.count() == 0) throw JobException("Please specify a command") return JobDetails(mailTo, mailFrom, jobName, args) } data class JobResult(val exitCode: Int, val output: String) fun runJob(job: JobDetails): JobResult { val builder = ProcessBuilder(job.arguments.toList()) builder.redirectErrorStream(true) val process = builder.start() val sb = StringBuilder() BufferedReader(InputStreamReader(process.inputStream)).use { reader -> var line = reader.readLine() while (line != null) { sb.append(line) sb.append(System.getProperty("line.separator")) line = reader.readLine() } } process.waitFor() return JobResult(process.exitValue(), sb.toString()) } fun main(args: Array<String>) { try { val cfg = initMailConfiguration() val job = initJobDetails(args) val jobResult = runJob(job) val address = InetAddress.getLocalHost() val description = "${address.hostName}: ${job.jobName} ${if (jobResult.exitCode == 0) "succeeded" else "failed"}" val html = """ <html> <body> <h1>${description}</h1> <pre><code>${jobResult.output}</code></pre> </body> </html> """.trimIndent() // Mail the results val email = HtmlEmail() email.hostName = cfg.server email.setSmtpPort(cfg.port) if (cfg.useSSL) { email.isStartTLSEnabled = true } if (cfg.useCredentials) { email.setAuthenticator(DefaultAuthenticator(cfg.username, cfg.password)) } email.setFrom(job.mailFrom) email.addTo(job.mailTo) email.subject = description email.setHtmlMsg(html) email.send() } catch (ex: Exception) { println(ex.message) } }
[ Voor 77% gewijzigd door Lethalis op 15-09-2018 14:07 ]
Ask yourself if you are happy and then you cease to be.