Author: penguin

  • Extract parts of a git repo as new one

    Situation: You have a repository with a subdirectory my/repo/part, which became large and own-project-worthy. You can copy the files into a new repo, but you’ll lose all git history.

    What would be just perfect is: From all commits ever made in this repository, …

    • only keep the parts of all commits which concern files under ./my/repo/part/**
    • remove the path prefix ./my/repo/part/.

    So, basically, rewrite and filter all git commits.

    There’s an app for that: git-filter-repo. And that’s how you use it:

    # clone the repository parallel to the existing one
    git clone git@github.com:me-and/my-repo fool-around-here
    cd fool-around-here
    
    # remove all other files except the ones you want
    # (yup, it's that easy)
    git-filter-repo --path my/repo/part
    
    # move all files to the repository root
    git-filter-repo --subdirectory-filter my/repo/part
    
    # set a new origin URL
    git remote set-url origin git@github.com:me-and/my-new-empty-prepared-repo
    
    # push.
    git push
    

    The git history has been rewritten 1984-style. For git it has always been exactly like this now.

  • jq snippets

    Delete any object which contains a "keyname": "keyvalue" pair anywhere in the JSON:

    cat x.json| jq 'del(..|select(objects and .keyname == "keyvalue"))'
    

    If keyname contains a dash (“-“), use one of those notations:

    cat x.json | jq 'del(..|select(objects and .["key-name"] == "keyvalue"))'
    cat x.json | jq 'del(..|select(objects and ."key-name" == "keyvalue"))'
    

    Delete all objects anywhere that contain a certain key:

    cat x.json | jq 'del(..|select(objects and .key?)'
    

    Delete a certain key from all objects everywhere:

    cat x.json | jq 'del(..|.response?)'
    

    Useful sources:

  • Lexoffice API notes

    Minimal invoice creation using the API.

    {
      "voucherDate": "2021-08-18T12:26:50.748Z",
      "address": {
        // if this is given the other fields become optional
        "contactId": "139bdad3-7786-43e2-acf0-8c3eaf0ee8c7"
      },
      "lineItems": [
        {
          "type": "service",
          "name": "Dienstleistungen",
          "description": "Beschreibung",
          "quantity": 5,
          "unitName": "Stunde",
          "unitPrice": {
            "currency": "EUR",
            "netAmount": 1,
            "taxRatePercentage": 19
          }
        }
      ],
      "totalPrice": {
        "currency": "EUR"
      },
      "taxConditions": {
        "taxType": "net"
      },
      "shippingConditions": {
        // the API documentation is wrong, the API only accepts: custom / none / text
        "shippingType": "none"
      },
      "title": "Rechnung",
      "introduction": "Ihre bestellten Positionen stellen wir Ihnen hiermit in Rechnung",
      "remark": "Vielen Dank für das entgegengebrachte Vertrauen"
    }
    
  • JavaScript ISO date with local timezone

    It seems JS has until today no native (!) solution to get an ISO 8601 date & time string in the local time zone (it’s always in UTC). (Super weird, but okay).

    I updated the solution on Stackoverflow a bit using “new” ES2017 functionality like this:

    function getISOLocalString() {
      let date = new Date();
      let tzo = -date.getTimezoneOffset();
    
      if (tzo === 0) {
        return date.toISOString();
      } else {
        let dif = tzo >= 0 ? "+" : "-";
        let pad = function (num, digits = 2) {
          return String(num).padStart(digits, "0");
        };
    
        return (
          date.getFullYear() +
          "-" +
          pad(date.getMonth() + 1) +
          "-" +
          pad(date.getDate()) +
          "T" +
          pad(date.getHours()) +
          ":" +
          pad(date.getMinutes()) +
          ":" +
          pad(date.getSeconds()) +
          dif +
          pad(tzo / 60) +
          ":" +
          pad(tzo % 60) +
          "." +
          pad(date.getMilliseconds(), 3)
        );
      }
    }
    
    console.log(getISOLocalString());
    
  • Backup tool evaluation

    (Update 2021-10-17: Read about the final decision here)

    Because now I need backup.

    Tools considered:

    Not considered:

    • arq – Mac/Win only, not free
    • bacula – waaay to complicated, apparently no S3 support
    • bup – no s3 support
    • bupstash – self-called alpha software, no S3 support
    • clonezilla – no S3 support, only block device backups
    • duplicacy – only free for non-commercial use

    Links:

  • remark42 comments

    This is a test post. There should be a comment box below, powered by the pretty cool and really well documented remark42 commenting engine.

    Let’s see … 🙂

  • GOPATH and GOROOT

    go has some unique concepts, one of them is a system-wide (user-wide) central go directory where things “happen” (e.g. modules being downloaded, etc.). For example, if you perform hugo mod get you essentially perform go mod get, which then downloads required modules … into your $GOPATH. Or $GOROOT/something, I really don’t quite get it, and I don’t care unless it stops working. Which it did.

    You often read “Just look into $GOPATH and see if it’s there”. Unfortunately I never set the value so there is no environment variable. The default of $HOME/go does not exist on my mac. Now what?

    Thanks to 0x434d53 I now know to do this, which prints the values used by go:

    go env
    
  • Completely delete teams on a Mac

    If teams is acting up sometimes (read: almost always) the only way to stop it is by deleting it completely. Microsoft has an answer on Microsoft answers on how to do it, I put it into a script. Here it is.

    This might not be absolutely everything though, cause it still remembered my account which I used last time.

    #!/usr/bin/env bash
    
    # source: microsoft
    # https://answers.microsoft.com/en-us/msteams/forum/all/how-to-completely-remove-teams-from-mac/2499239d-d491-43cb-a9c1-acbee7223b8d
    
    
    # $1 - full path to file / dir to delete
    delete_teams_file() {
        DELETE_ME=$1
        if [[ -d $1 ]] ; then
            echo -n "DELETE      $DELETE_ME ... "
            rm -rf "$DELETE_ME"
            echo "done."
        else
            echo "Not found - $DELETE_ME"
        fi
    }
    
    set -euo pipefail
    
    # prepare sudo entry
    echo "Prepare sudo ..."
    sudo echo "sudo prep successful :)"
    
    DELETE_BASE="$HOME/Library/Caches/com.microsoft."
    for DELETE_DIR in teams teams.shipit ; do
        delete_teams_file "${DELETE_BASE}${DELETE_DIR}"
    done
    
    DELETE_BASE="$HOME/Library/Application Support/Microsoft/Teams"
    delete_teams_file "$DELETE_BASE"
    
    sudo rm -rf "/Applications/Microsoft Teams.app"
    
    echo "All done."
    
  • Hugo migration experiences

    The goal was to switch to hugo as a static blogging engine. My reasons were:

    • super easy hosting (planned was S3 & CloudFront, it is now Netlify – which is insanely easy and I don’t need my homepage-builder image any longer)
    • no database any more, everything in git, including git push deployments
    • super quick page loads

    While trying this I found a couple of drawbacks, mainly …

    • way less themes than WordPress (well, big surprise), in by far not the same quality
    • theme switching is, if it gets improved a lot, still a PITA (right now let’s just not talk about it)
    • it is insanely hard to debug your page if something does not work
    • currently no comment engine

    It is a double-edged sword. During the migration process I often wished for a quick WYSIWYG “new blog post” editor like in WordPress, at the same time I loved the security git gave me – it’s nearly impossible to lose images, posts, whatever in your blog, it will always be “somewhere” in git. But there are already ways to get around that (it seems forestry.io is exactly that).

    So, here we are now. The state is:

    • currently, no comments available
    • cause I hate ads and don’t want to introduce ad-supported Disqus, and I also don’t want to pay for it
    • the plan is to add a self-hosted commenting engine though
    • all posts transferred, including all images (as far as I can tell)
    • a somewhat final theme (50:50 that this will be changed again)

    … and all of this took WAY longer that I expected.

  • Auto-rebuild Docker images if base image changes using GitHub actions

    Let’s say you have a rather static docker image which is based on another image which changes frequently. Now your image should be rebuilt on two occasions: if your content changes, but also if the image yours is based on changed. This can be done relatively simple using a GitHub actions cron schedule. You just have to create an actions workflow file in your repository.

    Adjust to your needs accordingly, and probably one can optimize even more by templating this.

    # FILE LOCATION IN REPOSITORY:
    #
    #   .github/workflows/auto-build-on-base-image-change.yml
    #
    
    name: Regular base image update check
    on:
      schedule:
        - cron: "23 13 3 * *"
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
          - uses: actions/checkout@v2
    
          - name: Docker Image Update Checker
            id: baseupdatecheck
            uses: lucacome/docker-image-update-checker@v1.1.0
            with:
              base-image: library/golang:alpine
              image: flypenguin/homepage-builder # update for your image
    
          # only execute subsequent steps if an update is actually NEEDED.
          # unfortunately we need to add an if-condition to all steps now
          # because a clean exit can't be triggered within a job it seems
          # (a cancellation is NOT the same and triggers a failure email)
          # see also https://github.com/actions/runner/issues/662
    
          - name: Login to DockerHub
            uses: docker/login-action@v1
            with:
              username: ${{ secrets.DOCKERHUB_USERNAME }}
              password: ${{ secrets.DOCKERHUB_TOKEN }}
            if: steps.baseupdatecheck.outputs.needs-updating == 'true'
    
          - name: Build and push Docker images
            uses: docker/build-push-action@v2.6.1
            with:
              context: "${{ github.workspace }}"
              push: true
              tags: flypenguin/homepage-builder:latest # update for your image
            if: steps.baseupdatecheck.outputs.needs-updating == 'true'
    

    This particular image is a builder image for Hugo sites. Hugo switched from git submodule based theme management to a Go module based theme & plugin management. So the my builder image needs Go installed now. The easiest way to do this is to base this image on the official golang image. Since installing Go on every build adds time and network traffic this seemd “uncool”, so auto-updating the base image it was.

    Sources:

    • https://git.io/JZns3
    • https://git.io/JB2AX
    • https://git.io/JB2x0