Skip to content

Instantly share code, notes, and snippets.

@Toparvion
Toparvion / HOWTO.md
Last active May 23, 2024 02:35
JUnit test for conditional Spring Boot bean registration (@ConditionalOnProperty)

Problem

Suppose you have two classes that should be registered with Spring context exclusively, e.g. only one of the beans must exist in the context at any time based on some boolean property value. Of course you can do it by adding explicit if condition into your @Configuration class. But if the classes have no common interface it may be quite cumbersome. As an alternative you can use @ConditionalOnProperty annotation on your classes, e.g.:

@Service
@ConditionalOnProperty(name = "use-left-service", havingValue = "true", matchIfMissing = false)
public class LeftService
@guilhermesimoes
guilhermesimoes / line_count_benchmark.rb
Last active September 11, 2023 09:36
Ruby Benchmark: Counting the number of lines of a file
# gem install benchmark-ips
require "benchmark/ips"
path = "lib/rubycritic/cli/options.rb"
Benchmark.ips do |x|
x.report("read + each_line") { File.read(path).each_line.count }
x.report("open + each_line") { File.open(path, "r").each_line.count }
@solyarisoftware
solyarisoftware / idle.rb
Last active July 11, 2023 20:58
Ruby script to test how to fetch IMAP mails (IDLE "push" mode) without pulling (in "real-time")
# Encoding: utf-8
#
# idle.rb
#
# goal:
# Ruby script to test how to fetch IMAP mails with IDLE mode.
# IMAP IDLE allow a sort of "push" / "real-time" delivery.
#
# I used the script to test LATENCY (end-to-end delivery times)
@amaudy
amaudy / often.sh
Last active August 29, 2015 14:04
Bash script that often use.
# list and count files in folders
for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done
# list directory name
ls -l | grep ^d | awk '{print $NF}'
@chiradeep
chiradeep / postinstall_lxc.sh
Created April 29, 2014 00:48
post-install for a debian wheezy lxc container that installs stuff necessary for a systemvm
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@stevenrombauts
stevenrombauts / progressbar.sh
Created February 26, 2014 14:37
Bash Progress Bar
#!/bin/bash
# Slick Progress Bar
# Created by: Ian Brown (ijbrown@hotmail.com)
# Please share with me your modifications
# Note: From http://stackoverflow.com/questions/11592583/bash-progress-bar
# Functions
PUT(){ echo -en "\033[${1};${2}H";}
DRAW(){ echo -en "\033%";echo -en "\033(0";}
WRITE(){ echo -en "\033(B";}
HIDECURSOR(){ echo -en "\033[?25l";}
@pkuczynski
pkuczynski / parse_yaml.sh
Last active May 29, 2024 10:18
Read YAML file from Bash script
#!/bin/sh
parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
@aaronsaunders
aaronsaunders / nix-cheat.sh
Last active January 25, 2021 06:54
Quick reference for nix command line commands that I can never remember...
# Unix shell
# run if zero exit
cd tmp/a/b/c && tar xvf ~/archive.tar # untar if dir exists
# run if non-zero exit
cd tmp/a/b/c || mkdir -p tmp/a/b/c
cd tmp/a/b/c || mkdir -p tmp/a/b/c && tar xvf -C tmp/a/b/c ~/archive.tar
which
whereis
@ccstone
ccstone / BBEdit-TextWrangler_RegEx_Cheat_Sheet.txt
Last active May 10, 2024 15:41
BBEdit-TextWrangler Regular Expression Cheat-Sheet
————————————————————————————————————————————————————————————————————————————————————————————————————
BBEdit / BBEdit-Lite / TextWrangler Regular Expression Guide Modified: 2018/08/10 01:19
————————————————————————————————————————————————————————————————————————————————————————————————————
NOTES:
The PCRE engine (Perl Compatible Regular Expressions) is what BBEdit and TextWrangler use.
Items I'm unsure of are marked '# PCRE?'. The list while fairly comprehensive is not complete.
@dgolds
dgolds / dgSniffCode.sh
Last active October 11, 2015 18:37
Bash Script to do quick counts of common source code types under the current or specified directory
#!/bin/bash
# Bash Script to do quick counts of common source code types under the current or specified directory
# https://gist.github.com/3901863
# To quickly grab it, just type
# wget https://gist.github.com/raw/3901863/dgSniffCode.sh && chmod +x dgSniffCode.sh
# Features to add:
# multiple params ...A hack until I support * arguments: ls | xargs -L 1 ~/dgSniffCode