Skip to content

Instantly share code, notes, and snippets.

View OndraZizka's full-sized avatar
🫖

Ondrej Zizka OndraZizka

🫖
View GitHub Profile
@OndraZizka
OndraZizka / StackBasedContext.kt
Created October 10, 2023 21:44
A Stack-based Processing Context for Kotlin
class StackBasedContext(
val nestingStack: Deque<NestingLevel> = ArrayDeque(),
val problems: MutableList<Pair<String, List<NestingLevel>>> = mutableListOf(),
) {
override fun toString() = nestingStack.ifEmpty { listOf("(root)") }.joinToString("/")
fun addProblem(msg: String) { problems.add(msg to nestingStack.descendingIterator().asSequence().toList()) }
fun pushLevel(dataParam: KParameter): PopOnClose { nestingStack.push(NestingLevel(dataParam)); return PopOnClose() }
fun pushLevel(collectionIndex: Int): PopOnClose { nestingStack.push(NestingLevel(collectionIndex = collectionIndex)); return PopOnClose() }
@OndraZizka
OndraZizka / ResourceUtils.kt
Created September 1, 2023 21:45
Kotlin - utilities for loading resources from the classpath.
import java.io.FileNotFoundException
import java.io.InputStream
import java.nio.file.Path
object ResourceUtils {
fun getCallingClassName(): String? {
var testUtilsSpotted = false;
for (stackItem in Thread.currentThread().stackTrace) {
if (stackItem.className == ResourceUtils::class.java.name) {
@OndraZizka
OndraZizka / startup.md
Created April 15, 2023 21:54 — forked from dsyer/startup.md
Notes on Spring Boot startup performance

Anatomy of Spring Boot Start Up Timing

When a Spring Boot app starts up with default (INFO) logging, there are some noticeable gaps (pauses). It's worth focusing on the gaps when looking for efficiency savings because of the amount of time they take, and because no-one bothered to log anything, so the chances are the app is doing something repetitive. We can tweak the logging levels to try and fill in the gaps and find out what is going on in there.

Basic empty web app with actuators has three such gaps:

0                                                                        1410ms
|------|---------------------------|-----|------|---------|--------|--------|
       |           578             |     |144(5)|         | 133(6) |
@OndraZizka
OndraZizka / fetchAllGitRepos.sh
Last active April 18, 2023 19:07
Fetch all Git repos in subdirectories
#!/bin/bash
## Run this in a directory to fetch all Git repos within it.
find -name .git | sed 's#.git##' | sort | xargs -t -n1 -I % git -C % fetch --all
## CRON job: You can also put this into `crontab -e` in order to keep all repos up-to-date automatically:
# # m h DoM mth DoW command
# */5 * * * * find /home/ondra/work -name .git | sed 's#.git##' | sort | xargs -t -n1 -I % git -C % fetch --all --tags --force
@OndraZizka
OndraZizka / enumCliOptionParsing.kt
Created November 27, 2022 00:23
Kotlin: Parsing CLI options easily using ENUMs
private inline fun <reified T : OptionEnum> tryParseEnumOption(enumArgumentDefault: T, arg: String): T? {
val optionIntro = "--${enumArgumentDefault.optionName}"
if (!arg.startsWith(optionIntro))
return null
if (arg == optionIntro || arg == optionIntro + "=" + enumArgumentDefault.optionValue)
return enumArgumentDefault
val valueStr = arg.substringAfter(optionIntro).removePrefix("=")
@OndraZizka
OndraZizka / Slf4jLazy.kt
Last active February 22, 2024 11:28
Kotlin SLF4J log message lazy evaluation based on log level
/*
Lazy evaluation of the messages sent to Slf4j.
Usage:
log.trace { "State dump: " + expensiveLongSerialisation(state) }
See also https://jira.qos.ch/browse/SLF4J-371
*/
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass
@OndraZizka
OndraZizka / installMaven38.sh
Last active March 20, 2023 16:19
Linux: install Maven 3.8.6
#!/bin/bash
## Because Ubuntu 22.10 still has Maven 3.6.3, this fixes it:
wget -q https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz -P /tmp
mkdir ~/sw/
tar xf /tmp/apache-maven-*.tar.gz -C ~/sw/
rm /tmp/apache-maven-*.tar.gz
mv /sw/apache-maven-* -C ~/sw/maven
echo 'MAVEN_HOME=~/sw/maven' >> ~/.bashrc
@OndraZizka
OndraZizka / ubuntu-openjdk-maven-docker.Dockerfile
Created October 27, 2022 21:37
Dockerfile: Ubuntu 22.04 + Docker + OpenJDK 18 + Maven 3.8
FROM ubuntu:22.04
ENV qqy="-qq -y -o=Dpkg::Use-Pty=0"
RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get $qqy update > /dev/null \
&& apt-get $qqy install ca-certificates curl wget gnupg lsb-release > /dev/null \
&& mkdir -p /etc/apt/keyrings \
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg \
@OndraZizka
OndraZizka / apple-sceen-brightness-howto.sh
Created October 4, 2022 11:54
Apple screen brightness control script for Linux (Ubuntu)
##
## This is not a script, rather a sequence of commands to do.
## I have tested it and it works.
## Use at your own responsibility.
## Compiled from https://www.dionysopoulos.me/apple-display-brightness-controls-in-ubuntu-desktop.html
## See https://github.com/yhaenggi/acdcontrol.git for more info.
##
sudo apt-install g++ make
git clone https://github.com/yhaenggi/acdcontrol.git
@OndraZizka
OndraZizka / GoogleSheets-getCellLink.gs
Created August 27, 2022 09:22
Google Sheets custom function: Get a link from a given cell.