Skip to content

Instantly share code, notes, and snippets.

View L-Briand's full-sized avatar

Lionel Briand L-Briand

  • Orandja
  • Bordeaux
View GitHub Profile
#!/usr/bin/env sh
usage() {
echo "usage:"
echo " $0 -k <key.pk8> -c <cert.x509.pem> [-o <keystore.jks>] [-s <keystore_password>]"
if [ -n "$1" ] ; then
echo "FILE: \"$1\" NOT FOUND"
fi
exit 1
}
@L-Briand
L-Briand / GiteaInstall.sh
Last active March 20, 2024 09:15
Installation script for Gitea on debian 12 with systemd service.
#!/bin/bash
GITEA_VERSION="1.21.8"
apt update && apt upgrade -y
apt install git openssh-server -y
adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git
wget -O gitea https://dl.gitea.io/gitea/$GITEA_VERSION/gitea-$GITEA_VERSION-linux-amd64
chmod +x gitea
@L-Briand
L-Briand / ResourcePool.kt
Created February 14, 2024 08:33
Resource sharing with semaphore and mutex in kotlin
/**
* Thread safe shared resources.
*
* ```kotlin
* // await 1s with the resource
* fun run(pool: ResourcePool<*>) = launch { pool { delay(1.seconds) } }
*
* // Pool of 2 usable elements at the same time
* val pool = ResourcePool<Unit>(2) { Unit }
* (0 ..< 120).map { run(pool) }.joinAll() // elapsed time ~60s
@L-Briand
L-Briand / LineSequence.kt
Created February 5, 2024 09:55
Read lines as sequence from the given stream or reader.
/**
* Read the [InputStream] and yield a [CharSequence] every time a new line is found.
*
* @receiver The [InputStream] to parse.
* @return a sequence of each line in the stream. (without new lines)
*/
fun InputStream.lineSequence(): Sequence<CharSequence> = lineSequence { read() }
fun Reader.lineSequence(): Sequence<CharSequence> = lineSequence { read() }
/**
@L-Briand
L-Briand / GnuParse.kt
Last active January 24, 2024 13:16
Parse arguments with gnu syntax in Kotlin.
/**
* Parse arguments with gnu syntax.
*
* When parsing, the name given to [onArgument] can be:
* - `null` if found value has no hyphen.
* - A short option if the value starts with a hyphen. (`-s`)
* - A long option if the value starts with a double hyphen. (`--long`)
*
* The `getValue` can be used to get the next non-hyphen value
import kotlinx.coroutines.*
fun CoroutineScope.onMain(action: suspend CoroutineScope.() -> Unit) =
launch(Dispatchers.Main, block = action)
fun CoroutineScope.onIo(action: suspend CoroutineScope.() -> Unit) =
launch(Dispatchers.IO, block = action)
fun CoroutineScope.onDefault(action: suspend CoroutineScope.() -> Unit) =
launch(Dispatchers.Default, block = action)
@L-Briand
L-Briand / HexValue.kt
Last active May 3, 2023 18:09
Helper class to work with hexadecimal strings.
import kotlinx.serialization.Serializable
/**
* Utility class to work with hexadecimal strings.
*
* Two hex values with same [raw] should be equals.
* Even if one was created with lower cased hex and the other with upper cased hex.
*
* It contains :
* - static helpers to encode [fromString] and decode [toString] in hexadecimal strings.
@L-Briand
L-Briand / #Observable.kt
Last active May 4, 2023 13:33
Observable pattern with kotlinx coroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.lang.ref.WeakReference
inline fun <reified T : Enum<T>> enumGetBy(default: T, predicate: (T) -> Boolean): T =
enumGetByOrNull(predicate) ?: default
inline fun <reified T : Enum<T>> enumGetByOrNull(predicate: (T) -> Boolean): T? =
enumValues<T>().find(predicate)
inline fun <reified T : Enum<T>> enumGetByName(name: String, default: T): T =
enumGetBy(default) { it.name == name }
inline fun <reified T : Enum<T>> enumGetByNameOrNull(name: String): T? =
@L-Briand
L-Briand / Either.kt
Last active May 17, 2023 07:17
Utility Optionated classes
sealed class Either<out L, out R> {
abstract val left: L
abstract val right: R
abstract fun invert(): Either<R, L>
}
class Left<out L>(override val left: L) : Either<L, Nothing>() {
companion object {
@JvmStatic