Skip to content

Instantly share code, notes, and snippets.

View randomstatistic's full-sized avatar

Jeff Wartes randomstatistic

View GitHub Profile
@randomstatistic
randomstatistic / ClasspathReader.scala
Created April 24, 2017 16:57
Resource file reader
import java.io.FileNotFoundException
import java.util.zip.GZIPInputStream
import scala.io.Codec
object ClasspathReader {
def getResourceAsStream(filename: String) = {
// why doesn't the more scala-like getClass.getResourceAsStream work?
val inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)
if (inputStream == null) throw new FileNotFoundException(s"Couldn't find $filename on the classpath")
@randomstatistic
randomstatistic / TryWithResources.scala
Created July 13, 2018 18:21
Scala try-with-resources equivelent
import java.io.Closable
import scala.util.control.NonFatal
import scala.util.{ Success, Try }
// This should really be scala-standard-lib.
object TryWithResources {
def withClose[T <: Closeable, V](closable: T)(f: T => V): V = {
(Try(f(closable)), Try(closable.close())) match {
case (Success(v), Success(_)) => v
case (a, b) => throw preferFirstException(a, b).get
@randomstatistic
randomstatistic / MGet.scala
Last active October 9, 2018 18:10
Get 200 random keys from a redis cluster
import io.lettuce.core.RedisURI
import io.lettuce.core.cluster.{ ClusterClientOptions, ClusterTopologyRefreshOptions, RedisClusterClient }
import scala.util.Random
object MGet {
def main(args: Array[String]): Unit = {
val host = args(0)
val port = args(1).toInt
@randomstatistic
randomstatistic / ConfigMask.scala
Created June 21, 2019 18:54
Mask sensitive TypeSafe-Config values for rendering
import java.util.Map.Entry
import com.typesafe.config.{ Config, ConfigRenderOptions, ConfigValue, ConfigValueFactory }
import org.mindrot.jbcrypt.BCrypt
import scala.io.{ Codec, Source }
import scala.collection.JavaConverters._
import scala.util.Try
import scala.util.matching.Regex