Skip to content

Instantly share code, notes, and snippets.

View klingtnet's full-sized avatar
a damn fine cup of coffee

Andreas Linz klingtnet

a damn fine cup of coffee
View GitHub Profile
@klingtnet
klingtnet / parse.go
Last active August 20, 2020 06:00
How to properly parts of an unknown YAML file in Go
package main
import (
"fmt"
"log"
"os"
"strings"
"gopkg.in/yaml.v2"
)
@klingtnet
klingtnet / progressreader.go
Created July 18, 2020 10:21
Progress Reader
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"time"
)
@klingtnet
klingtnet / presentation-mode.bash
Last active June 22, 2020 11:30
Disable auto suspend in Gnome (presentation mode)
#!/bin/bash
set -euo pipefail
_cmd=${1:-'help'}
case $_cmd in
"on")
dconf write /org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-type "'nothing'"
;;
@klingtnet
klingtnet / swapon-failed.md
Last active June 13, 2020 11:49
swapon failed

How to fix a bad swapfile

I recently saw a bunch of those warnings when booting Arch Linux:

$ systemctl status swapfile.swap | grep -i invalid
Jun 12 12:34:25 m604 swapon[22623]: swapon: /swapfile: swapon failed: Invalid argument

This was weird since the swapfile worked fine before and the file existed and had secure permissions:

@klingtnet
klingtnet / content-type-reader.go
Last active May 15, 2020 22:03
A reader that detects the mime/content-type in Go
package main
import (
"bytes"
"io"
"log"
"net/http"
"github.com/gabriel-vasile/mimetype"
)
@klingtnet
klingtnet / flac-to-opus-parallel.bash
Last active May 11, 2020 11:04
Parallel flac to opus transcoding using ffmpeg and GNU parallel
#!/bin/bash
set -eu
IFS=\n && parallel ffmpeg -i {} -c:a libopus -b:a 128k {.}.opus' ::: **/*.flac
@klingtnet
klingtnet / prefix-output-of-subprocess.go
Last active April 27, 2020 12:47
Prefix the output of a subprocess
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
@klingtnet
klingtnet / match_groups.go
Created April 16, 2020 14:56
Golang map of a regular experssions capture groups
package main
import (
"fmt"
"regexp"
)
func capturingGroups(re *regexp.Regexp, input string) map[string]string {
groups := make(map[string]string)
matches := re.FindStringSubmatch(input)
@klingtnet
klingtnet / refresh.sh
Created April 15, 2020 12:51
Restart a Go web service on file change
#!/bin/sh
set -euo pipefail
command -v reflex &>/dev/null || { cat <<HEREDOC
Please install reflex:
go get -u github.com/cespare/reflex
HEREDOC
exit 1
}
@klingtnet
klingtnet / control-monitor-brightness-from-terminal-or-gui-in-linux.md
Created April 15, 2020 08:43
Control monitor/display brightness from terminal or GUI in Linux

The following commands assume that Arch Linux is installed, they should be applicable to any systemd based distro whatsoever.

# install ddccontrol or gddccontrol if you wish a GUI
$ pacman -S ddccontrol
# insert the required kernel module
$ modprobe i2c-dev
# make sure that the i2c-dev kernel module is inserted automatically on boot
$ cat <<HEREDOC> /etc/modules-load.d/i2c-dev.conf 
# Load i2c-dev module for ddccontrol