Skip to content

Instantly share code, notes, and snippets.

@geberl
geberl / file_path_build_vs_run.go
Created October 28, 2019 07:43
Reliably access a file that resides next to the code/binary, whether it's started via go run or go build
package main
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
)
func main() {
@geberl
geberl / get_flac_info.go
Last active November 2, 2019 21:20
Get metadata and stream info from a flac audio file
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/mewkiz/flac"
"github.com/mewkiz/flac/meta"
@geberl
geberl / struct_method_pass_by_val_vs_ref.go
Created October 19, 2019 05:10
Using struct methods with "pass by value" vs "pass by reference"
package main
import (
"fmt"
)
type file struct {
Size int `json:"size"`
Name string `json:"name"`
Extension string `json:"extension"`
@geberl
geberl / resize_big_images.sh
Created October 16, 2019 09:33
Use convert (from imagemagick) to resize big image files to 50% with and height
#!/bin/sh
# SETTINGS -----------------------
IMAGE_BASE_DIR="/Users/guenther/Downloads/imagefiles"
SIZE_LIMIT="2M" # or "500k"?
RESIZE_PERCENTAGE="50"
# --------------------------------
find ${IMAGE_BASE_DIR} -type f -size +${SIZE_LIMIT} | while read filepath; do
echo "Resizing $filepath ..."
@geberl
geberl / channel_global_vars_and_wait_group.go
Created October 9, 2019 09:02
Using a goroutine to fetch some data from a mock API periodically, and save the result to global variables using a global channel that is listened to in another goroutine
package main
import (
"fmt"
"sync"
"time"
)
var messages chan string
var currentConditions string
@geberl
geberl / get_repository_infos.sh
Created September 4, 2019 11:15
Use git and some common unix tools to print info about a repository
#!/bin/sh
commitLong=`git describe --always --abbrev=0`
echo "commitLong:" $commitLong
commitShort=`git describe --always`
echo "commitShort:" $commitShort
commitDatetime=`git log -1 --date=iso --format=%cd`
echo "commitDatetime:" $commitDatetime
@geberl
geberl / ViewController.swift
Created August 16, 2019 15:35
Example of an iOS ViewController that does some REST API calls, in Swift
//
// ViewController.swift
// RestApiCallExample
//
// Created by Günther Eberl on 16.08.19.
// Copyright © 2019 Günther Eberl. All rights reserved.
//
import UIKit
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Documentation:
- Docker Engine API: https://docs.docker.com/develop/sdk/
- Docker SDK for Python: https://docker-py.readthedocs.io/en/stable/
"""
from dateutil import parser
#! /usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import json
import logging
import requests
from urllib import urlencode
from urllib2 import Request, urlopen
from tzlocal import get_localzone
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import requests
def get_token(auth_url, image_name):
payload = {
'service': 'registry.docker.io',
'scope': 'repository:library/{image}:pull'.format(image=image_name)