Skip to content

Instantly share code, notes, and snippets.

View arturtamborski's full-sized avatar
💃

Artur Tamborski arturtamborski

💃
View GitHub Profile
@arturtamborski
arturtamborski / diff.js
Created March 11, 2024 16:43
pretty diff with json for stubborn google chat
const whitespacesToCharacter = char => s => s.replace(/(^\s+)/gm, p => (new Array(p.length+1).join(char))) ;
const replacer = whitespacesToCharacter("<200b> ");
const diff = require('diff');
out = diff.diffJson(one, other).map((part) => {
const c = part.added ? "#00FF00" : part.removed ? "#FF0000" : "#000000";
return `<font color="${c}">${replacer(part.value)}</font>`;
}
).join("");
@arturtamborski
arturtamborski / wait-delay.js
Created February 20, 2024 18:26
wait/delay in js
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const wait = (ms) => {const n = Date.now(); while (Date.now() - n < ms) {}}
const randomDelay = () => delay(Math.random() * 1000);
const randomWait = () => wait(Math.random() * 1000);
@arturtamborski
arturtamborski / time-curl-with-jq.sh
Last active February 14, 2024 10:55
get request timing details with curl
#!/usr/bin/env bash
# https://curl.se/docs/manpage.html
# CTRL+F -> '--write-out' to see more
curl -sw '%{json}' -o /dev/null "$@" | jq '{
time_appconnect,
time_connect,
time_namelookup,
@arturtamborski
arturtamborski / jq.py
Created February 13, 2024 08:52
passthrough json trough jq
jq = subprocess.Popen(
args=['jq', '-r', args.filter],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdin = json.dumps(context)
stdout, stderr = jq.communicate(stdin.encode())
stdout, stderr = stdout.decode(), stderr.decode()
def deepmerge(left, right):
left, right = left.copy(), right.copy()
for k, v in left.items():
if k in right and all(isinstance(e, dict) for e in (v, right[k])):
right[k] = deepmerge(v, right[k])
left.update(right)
return left
@arturtamborski
arturtamborski / bash.sh
Created February 1, 2024 17:15
cool way for setting multiline environment variables
read -r -d '' TF_CLI_ARGS_init << \
----------------------------------------------
-reconfigure
-otheroption
-etc
----------------------------------------------
# problem: write to file, don't show it on the screen
$ get-passwords > passwords.txt
# works okay
# problem 2: write to file with sudo, don't show it on the screen
@arturtamborski
arturtamborski / recurset.py
Last active November 21, 2023 10:05
recursively set value in dict
def recurset(key, val, dct, sep='/'):
out = dct
*parts, last = key.split(sep)
for part in parts:
if part not in out:
out[part] = {}
out = out[part]
out[last] = val
return dct
@arturtamborski
arturtamborski / gist:020d6f859882910d942992c9449e1cbf
Last active September 25, 2023 10:33
jq snippets for terraform state
# all resources left in all state files
find . -type f | xargs cat | jq -rs '.[].resources | select(. != []) | .[].instances | .[].attributes.arn | select(. != null)'
@arturtamborski
arturtamborski / abomination.pl
Created August 7, 2023 10:26
parse hcl with perl xD
use strict;
use warnings;
use JSON 'encode_json';
my $code = `find .. -name '*.tf' -exec cat {} +`;
my @blocks = map { $_ =~ m/ (?^:((?:\{(?:(?>[^\{\}]+)|(?-1))*\}))) /xg } $code;
my @repos = grep { m| \s* source \s*=\s* ".+/modules/repository" |xg } @blocks;
my @names = map { $_ =~ m/ \s* name \s*=\s* "((?:[^"]+|"")*)" /xg } @repos;
my @unique = sort { $a cmp $b } do { my %seen; grep { !$seen{$_}++ } @names };
print encode_json { data => encode_json { repos => \@unique } };