Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / find_public_keys.py
Created Feb 9, 2021
Python - Find public keys matching a given list of patterns
View find_public_keys.py
def contents(ssh, files):
for file in files:
path = '{}/{}'.format(ssh, file)
yield open(path).read().replace("\n", "")
def keysFor(args):
ssh = '{}/.ssh'.format(os.environ['HOME'])
extensions = [ '.pub' ]
files = [f for f in os.listdir(ssh) if os.path.splitext(f)[1] in extensions]
keys = [k for k in contents(ssh, files) if k.split(' ')[2] in args]
@frgomes
frgomes / pattern_mathing_on_runtimeClass.scala
Last active Jan 29, 2021
Scala - Pattern matching on a runtime class employing ClassTag and stable identifiers
View pattern_mathing_on_runtimeClass.scala
import com.typesafe.config.Config
implicit class ImplicitConfigHelper(val c: Option[Config]) extends AnyVal {
import scala.reflect.ClassTag
import scala.util.control.NonFatal
import ImplicitConfigHelper._
implicit def valueOf[T : ClassTag](prop: String)(implicit ev: ClassTag[T]) : T =
ev.runtimeClass match { // you need stable identifiers here
case String_ => c.get.getString(prop).asInstanceOf[T]
case Int_ => c.get.getInt(prop).asInstanceOf[T]
@frgomes
frgomes / ChainingTry.scala
Last active Nov 18, 2020
Scala - Chaining Try/Success/Failure, return first successful
View ChainingTry.scala
def a: Try[String] = Failure(new RuntimeException)
def b: Try[String] = Failure(new RuntimeException)
def c: Try[String] = Success("C")
def d: Try[String] = Failure(new RuntimeException)
def e: Try[String] = Success("E")
def f: Try[String] = Failure(new RuntimeException)
val result = a orElse b orElse c orElse d orElse e orElse f
// result is Success("C")
@frgomes
frgomes / CaseClassFactory.scala
Last active May 20, 2020
Scala - Obtain structure of a case class and build an instance of it from data
View CaseClassFactory.scala
// NOTE: This trickery below allows us to obtain the type of
// fields of a case class, so that we can play with generic
// pattern matching on field types.
// The post below is somewhat related.
// See also: http://www.alessandrolacava.com/blog/scala-case-classes-in-depth/
import scala.reflect.runtime.universe._
@frgomes
frgomes / fqdn.scala
Last active May 20, 2020
Scala - Obtain FQDN
View fqdn.scala
def fqdn: Option[String] = {
val marker = "domain "
def ipv4(canonical: String): Boolean = {
def isAllDigits(x: String) = x forall Character.isDigit
canonical != null && !canonical.isEmpty && {
val parts = canonical.split('.')
parts.length > 1 && !parts.map(part => isAllDigits(part)).reduce(_ & _)
}
}
val addr: java.net.InetAddress = java.net.InetAddress.getLocalHost
@frgomes
frgomes / keybase.md
Last active Feb 20, 2020
keybase proof
View keybase.md

Keybase proof

I hereby claim:

  • I am frgomes on github.
  • I am frgomes (https://keybase.io/frgomes) on keybase.
  • I have a public key ASC31pcPI9EG4dsquRu8NbKIIHYtjUkhHAL3LyVzzuwjyQo

To claim this, I am signing this object:

@frgomes
frgomes / install_retropie.sh
Last active Dec 12, 2019
bash - install RetroPie on UDOO Bold V8
View install_retropie.sh
#!/bin/bash
##
## This script is instended to install RetroPie on a newly installed Debian Buster box.
##
## Hardware requirements:
## This script is known to work well with UDOO Bolt V8, which has an AMD Ryzen 1000 SoC.
##
## Software Requirements:
##
@frgomes
frgomes / firefox_via_ssh_tunnel.sh
Last active Sep 13, 2019
Bash - Firefox via SSH tunnel
View firefox_via_ssh_tunnel.sh
# create a SSH tunnel between 127.0.0.1:18080 <--> internal.example.com:8080
$ ssh -L 127.0.0.1:18080:internal.example.com:8080 username@router.example.com
# open Firefox on the local computer, which will send and receive data via the SSH tunnel
$ firefox http://127.0.0.1:18080
@frgomes
frgomes / InputStream_from_String.scala
Created Jan 17, 2018
Scala - Create java.io.InputStream from String
View InputStream_from_String.scala
val lines =
Seq(
"Item,Qty,Price",
"Banana,1,0.50")
val text = lines.mkString("\n")
val stream: java.io.InputStream = new java.io.ByteArrayInputStream(text.getBytes(java.nio.charset.StandardCharsets.UTF_8.name))
@frgomes
frgomes / kubectl_helpers.sh
Last active Aug 18, 2019
k8s: find cluster names in kubectl configuration
View kubectl_helpers.sh
#!/bin/bash
// one-liner which converts YAML to JSON
function yaml2json {
python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print(json.dumps(y))'
}
function kubectl_clusters {
cat ~/.kube/config | yaml2json | jq '.clusters[].name' | sed 's/"//g'
}