Skip to content

Instantly share code, notes, and snippets.

View mikybars's full-sized avatar
🥛

Miguel Ibars mikybars

🥛
  • Madrid
  • 08:41 (UTC +01:00)
View GitHub Profile
@mikybars
mikybars / Application.java
Last active July 1, 2024 15:42
Java Records + Spring configuration properties
// @EnableConfigurationProperties(AppSettings.class)
@ConfigurationPropertiesScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@mikybars
mikybars / gum.sh
Last active March 2, 2023 09:28
🎀 A swiss knife tool for writing beautiful CLIs in Bash
#!/bin/sh
# https://github.com/charmbracelet/gum
# More examples: https://github.com/charmbracelet/gum/tree/main/examples
gum choose "fix" "feat" "docs" "style" "refactor" "test" "chore" "revert"
# Input with placeholder
gum input --placeholder "Summary of this change"
@mikybars
mikybars / magic-regexp.js
Last active March 2, 2023 23:31
🦄 magic-regexp: A compiled-away, type-safe, readable RegExp alternative.
// https://regexp.dev
import { createRegExp, exactly, oneOrMore, digit, char } from 'magic-regexp'
// hover to see the compiled regexp
const semver = createRegExp(
oneOrMore(digit)
.groupedAs('major')
.and('.')
.and(oneOrMore(digit).groupedAs('minor'))
.and(exactly('.').and(oneOrMore(char).groupedAs('patch')).optionally())
@mikybars
mikybars / validate_dataclass.py
Last active May 24, 2023 19:11
Generic solution for `@dataclass` validation in Python with custom setters
from dataclasses import dataclass
class Validations:
def __setattr__(self, prop, val):
if (validator := getattr(self, f"validate_{prop}", None)):
object.__setattr__(self, prop, validator(val) or val)
else:
super().__setattr__(prop, val)
@mikybars
mikybars / commit-msg.sh
Last active March 10, 2022 06:15
[Prepend commit message with Jira ticket id] The Jira key is parsed from the branch name #git #jira
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".
# Uncomment the below to add a Signed-off-by line to the message.
@mikybars
mikybars / RestClientLoggingEndpoint.java
Last active February 10, 2022 06:19
[Spring Boot Actuator endpoint to enable/disable RestTemplate logging at runtime] Logging is automatically disabled after a certain amount of time to prevent clogging #spring
package com.inditex.mecretcond.infra.actuator;
import static java.time.Instant.now;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
@mikybars
mikybars / awk.sh
Created September 17, 2021 14:52
[Print field names along their indexes from tabular data] Most useful for big log files #csv #awk
awk '(NR==1) {for (i=1;i<=NF;i++) print i ": " $i}' <data.csv
# 1: date
# 2: time
# 3: x-edge-location
# 4: sc-bytes
# 5: c-ip
# 6: cs-method
# 7: cs(Host)
# ...
@mikybars
mikybars / sdkman.sh
Created September 17, 2021 12:03
[Shell command proxy] Lazy initialization of heavy tool (SDKMAN!) + interception of `sdk use java` to fuzzy filter installed versions when none specified. #shell
export SDKMAN_DIR="/usr/local/sdkman"
# https://mharrison.org/post/bashfunctionoverride/
copy_function() {
local orig_fn=$(declare -f $1)
local new_fn="$2${orig_fn#$1}"
eval "$new_fn"
}
sdk_fuzzy_find_local_version() {
@mikybars
mikybars / awk.sh
Last active September 17, 2021 14:47
[Select rows and columns from tabular data] #csv #awk
# data.csv:
#
# Año,Marca,Modelo,Descripción,Precio
# 1997,Ford,E350,"ac, abs, moon",3000.00
# 1999,Chevyr,Venture,Extended Edition,4900.00
# 1999,Chevy,Venture,"Extended Edition, Very Large",5000.00
# 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00
awk -F, 'NR==2,NR==5 {print $2 " " $3}' <data.csv
@mikybars
mikybars / git-change.sh
Last active October 1, 2021 14:40
[Git interactive branch filter with hot reloading] Git helper script that lists remote branches sorted by their last update time. The branch list is fed into a fuzzy finder for interactive filtering and the selected branch is checked out. Finally a keyboard shortcut enables dynamic reloading of the branches after fetching the remote. #shell #fzf…
#!/usr/bin/env bash
# git-change
#
# Git helper script that lists remote branches sorted by their last update time. The branch list is fed
# into a fuzzy finder for interactive filtering and the selected branch is checked out. Finally a
# keyboard shortcut enables dynamic reloading of the branches after fetching the remote.
# External dependencies:
# - fzf: brew install fzf