Skip to content

Instantly share code, notes, and snippets.

View swsnr's full-sized avatar
✌️
Hapaschnapapopapapapa

Sebastian Wiesner swsnr

✌️
Hapaschnapapopapapapa
View GitHub Profile
@swsnr
swsnr / log.rs
Created May 29, 2021 11:06
Filter loggers with env_logger
// Copyright Sebastian Wiesner <sebastian@swsnr.de>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! Logging.
use env_logger::filter::Filter;
use log::{Log, Metadata, Record};
@swsnr
swsnr / Decode irregular JSON with Circe.md
Last active August 27, 2019 19:43
Decode irregular JSON with Circe

The popular CI server Jenkins provides a rich API to access information about builds.
This API speaks JSON but the JSON it returns has a rather strange shape. I needed to extract the Git revision built by a job, but Jenkins hides this information in a specific object in a “catch-all” actions array which contains JSON objects of different shapes, many of which may or may not be present. With Circe and some Shapeless magic I managed to decode this irregular JSON in a type-safe and fail-safe way (ignoring unknown JSON objects).

The JSON

This actions in the JSON model of a build looks as follows. I converted the JSON to YAML to remove the syntactic boilerplate of JSON and make the snippet easier to read, and I also removed irrelevant parts:

@swsnr
swsnr / Response.scala
Created August 27, 2019 19:38
Decode flat ADT with circe
import io.circe._
import io.circe.generic.semiauto._
sealed trait Response
final case class Success(`type`: String, id: Int, version: Int)
extends Response
object Success {
implicit val successDecoder: Decoder[Success] = deriveDecoder[Success]
@swsnr
swsnr / MessageByType.scala
Last active August 9, 2019 11:49
Akka Stream operator to split a stream by type
import akka.NotUsed
import akka.http.scaladsl.model.ws
import akka.http.scaladsl.model.ws.{BinaryMessage, Message, TextMessage}
import akka.stream.scaladsl.{Broadcast, Flow, GraphDSL}
import akka.stream.{FanOutShape, Graph, Outlet}
import scala.collection.immutable
/**
@swsnr
swsnr / sbt.groovy
Last active September 20, 2019 08:08
SBT Jenkins Pipeline Step
#!/usr/bin/groovy
/**
* Run SBT via sh with the given arguments.
*
* Injects a file pointed to by $NEXUS_CREDENTIALS env var for publishing credentials.
* Use the Credentials Jenkins plugin to manage these credentials in Jenkins and inject
* them into a build.
*
* @param sbtArgs Arguments for SBT
@swsnr
swsnr / install.bash
Last active September 2, 2019 14:44
withYarn SBT Pipeline Step
#!/bin/bash
set -e
# The keyring we use to verify the signature of our download
keyring_file="$1"
# The version of yarn to install
version="$2"
# The directory install yarn versions to
basedir="$HOME/.yarn/versions/"
@swsnr
swsnr / showSbtDependencies.groovy
Last active August 29, 2019 06:49
showSbtDependencies Jenkins Pipeline Step
#!/usr/bin/groovy
/**
* Extract project versions from all SBT POMs and publish them as summary badge.
*
* Requires Badge and Pipeline Utility Step plugins.
*/
def call() {
// Clear out old badge if any
@swsnr
swsnr / Directives.scala
Last active August 26, 2019 21:07
Build webapp to webjar with SBT and Yarn
import akka.http.scaladsl.model.StatusCodes.MovedPermanently
import akka.http.scaladsl.model.Uri
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import org.webjars.WebJarAssetLocator
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
object Directives {
@swsnr
swsnr / ShapelessCaseClassAttributes.scala
Created April 6, 2016 13:40
Get a list of all case class attributes with shapeless
import shapeless._
import shapeless.poly._
import shapeless.record._
import shapeless.ops.record._
import shapeless.ops.hlist.{Mapper,ToTraversable}
import shapeless.tag._
final case class Message(id: Int, title: String, body: String)
trait ToAttributes[T] {
@swsnr
swsnr / redirect_fd.py
Created July 20, 2012 14:37
Suppress output to file descriptors in Python
import os
from contextlib import contextmanager
@contextmanager
def suppress_output(fd):
"""
Suppress output to the given ``fd``::
with suppress_fd(sys.stderr):
# in this block any output to standard error is suppressed