Skip to content

Instantly share code, notes, and snippets.

View anderseknert's full-sized avatar
👨‍💻
Hacking on all things OPA

Anders Eknert anderseknert

👨‍💻
Hacking on all things OPA
View GitHub Profile
#!/usr/bin/env bash
# As passing an array by value is not possible (that I know) this function
# requires that the array passed has been expanded (@)
function in_array() {
# Require both needle and haystack
if [ -z "$1" -o -z "$2" ]; then
return;
fi
# Use the first argument as needle
#!/usr/bin/env bash
logdir="/var/log/"
installed=""
# Scan all dpkg logs for installed packages
for file in ${logdir}dpkg.log*; do
if [ ${file##*.} = "gz" ]; then
installed=$installed`gzip -dc $file | grep ' installed' | cut -d' ' -f5`
else
installed=$installed`cat $file | grep ' installed' | cut -d' ' -f5`
fi
@anderseknert
anderseknert / gist:5187213
Created March 18, 2013 13:42
Remove annoying for-pay content on www.dn.se
var removeIfPayOnly = function() {
var $t = $(this),
$free = $t.find('a')
.not('[href^=javascript]')
.not('[href^=http://www.dagensnyheter.se]');
if (!$free.length) {
$(this).remove();
}
};
$('.teaser, .subteaser, .half-item').each(removeIfPayOnly);

Keybase proof

I hereby claim:

  • I am andyroid83 on github.
  • I am eknert (https://keybase.io/eknert) on keybase.
  • I have a public key ASD2bKew40DQmZYLdJRh27N1C_doLmZF11ZvdUgpvynM_Qo

To claim this, I am signing this object:

@anderseknert
anderseknert / extract_emails.scm
Created March 27, 2019 08:46
Extract all e-mail addresses from a list of Outlook recepients (formatted as "FirstName LastName <actual@email.com>; ...")
#!/usr/bin/env guile
!#
(use-modules (ice-9 textual-ports)
(ice-9 regex))
(let* ((input (get-string-all (current-input-port)))
(matches (list-matches "[^<]*<([^>]+)>+" input))
(extractor (lambda (match) (match:substring match 1))))
(display (map extractor matches)))
#!/usr/bin/env bash
# Example of cython --embed producing a .c file for compilation with
# all dependencies statically linked. This to create a Python standalone
# application. Tested on Ubuntu 16.04. This was done as a learning exercise
# only - for real use see https://pyinstaller.readthedocs.io/
OS_ARCH=`echo $(uname -sm) | tr '[:upper:]' '[:lower:]' | tr ' ' '-'`
# ..or take from args
@anderseknert
anderseknert / pre-commit
Created September 13, 2019 19:03
My golang pre-commit hook
#!/bin/sh
# Fail on whitespace errors
exec git diff-index --check --cached $against --
exit_code=0
STAGED_GO_FILES=$(git diff --cached --name-only -- '*.go')
for file in $STAGED_GO_FILES; do
go fmt $file
@anderseknert
anderseknert / rando.py
Last active September 18, 2019 07:03
Generate random swedish personal ID
import datetime, random
ssid = "{}{:02d}{}".format(*[x for x in ['{:%y%m%d}'.format(datetime.datetime(1970, 1, 1) + datetime.timedelta(days=random.randint(-30, 30) * 365 + random.randint(0, 365))), random.randint(0, 99), random.randint(0, 9)]])
print("{}{}".format(ssid, (10 - (sum(map(lambda x: x%10 + int(x/10), map(lambda x,y: x*y, map(int, ssid), [2,1]*4 + [2]))) % 10)) % 10))
@anderseknert
anderseknert / rego.js
Created September 19, 2019 13:08
CodeMirror rego mode for syntax highlighting of rego policies
(function(mod) {
if (typeof exports === "object" && typeof module === "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define === "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
function wordRegexp(words) {
@anderseknert
anderseknert / total_size_recursive.py
Created November 11, 2019 20:39
Scan dir recursively and report total size of all files matching glob pattern
import os
import glob
from pathlib import Path
def total_size(dir):
"""Scan dir recursively and report total size of all files matching glob pattern"""
total_size = 0
last_reported = ''
for f in glob.iglob(dir, recursive=True):