Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


wipe dynamodb table via terraform -- 17.09.2021 %

Recently I came across the problem of how to wipe a dynamodb table. I though there would be an ui button for this or at lease an api/sdk method but from what I found the only way it to individually delete the items by key. This method can be batched up to 10 items per call but is still very slow and incurs write costs.

Since I’m used Terraform to create the table in the first place I thought I’ll just destroy it and remake it… Then came the problem of not wanting to destroy everything but simply remake the table. After some digging in Terraform docs I found a pretty clean solution.

main.tf

module "dynamodb_table" {
  source   = "terraform-aws-modules/dynamodb-table/aws"

  name     = "poverty-line-county-gps-map"
  hash_key = "id"
  stream_enabled = false

  attributes = [
    {
      name = "id"
      type = "S"
    }
  ]

  tags = {
    Terraform   = "true"
    Project = "poverty_line"
  }
}

In this case I wanted to remake “poverty-line-county-gps-map”. To find out what exactly it is called I ran terraform state:

$ terraform state list
aws_sqs_queue.dead_letter_queue
aws_sqs_queue.terraform_queue
module.dynamodb_table.aws_dynamodb_table.this[0]

from here the resource can be remake like:

$ terraform apply -replace="module.dynamodb_table.aws_dynamodb_table.this[0]"

Happy times remaking things. 👋

pushing to remote branch with different name -- 08.09.2021 %

Some times you rename a branch locally and want to push it to a remote branch with a different name. This is how:

$ git push -u origin localBranch:remoteBranch

source: stack-overflow

\- [ git ]

measuring node.js function performance -- 23.08.2021 %

How fast is that new function?

 import { xx } from 'xx';
+import { performance } from 'perf_hooks';
@@ -160,7 +161,10 @@ const userSessionMiddleware = async (req: XRequest, res: ExpressResponse, ne
+        var t0 = performance.now();
         req.isBot = headersIndicateBot(req.headers);
+        var t1 = performance.now();
+        console.log('Took', (t1 - t0).toFixed(4), 'milliseconds to calculate is bot');

source: https://www.sitepoint.com/measuring-javascript-functions-performance/

\- [ js, node-js ]

init yubikey -- 16.07.2021 %

Some servers at work require yubikey authentication and for some reason I have to fetch and verify every time I want to use it.

$ export SSH_AUTH_SOCK=~/.gnupg/S.gpg-agent.ssh
$ gpg --card-edit

fetch
verify
enter key
via pass yubikey
quit

$ ssh-add -L
$ ssh travis@serverX
\- [ yubikey, gpg ]

add types to a javascript when using typescript -- 12.07.2021 %

Today I had a .js file that I needed to import into the rest of a TypeScript app but could not convert it to a .ts file. To get around this limitation I added an accompanying .d.ts file.

countryConfig.js

const countries = [
    {
        code: 'de',
        domainExtension: '.de'
    },
    ...
];

exports.countries = countries;

countryConfig.d.ts

interface Country {
    code: string;
    domainExtension: string;
}

export const countries: Array<Country>;

app.ts

import countries from './countryConfig'

...

source: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

\- [ js, typescript ]

filtered git diff -- 25.06.2021 %

How to browse git diff of two hashes but exclude some files. In this case any file with test in the name won’t be in the diff.

gd xxxsha001xxx...xxxsha002xxx -- $(gd --name-only xxxsha001xxx...xxxsha002xxx | rg --invert-match test)

“gd” == “git diff”, via zsh git plugin

source

\- [ git ]

locally host istanbul js results -- 23.06.2021 %

At work we use istanbul js for code coverage but at times the cli output is not enough to debug. Luckily istanbul also outputs a HTML report that can be viewed with the following command:

$ npx http-server ./coverage/lcov-report
\- [ js, npx, npm ]

markdown to org file conversion -- 21.06.2021 %

I use the Bear Notes app on IOS which has a nice markdown export feature. From there it is easy to air drop the file to my mac then I paste it into my org notes after converting it.

$ pandoc -f markdown -t org -o note.org /tmp/md_note.md && pbcopy < /tmp/md_note.md

source: stackexchange

\- [ org, markdown ]

gpg usb workflow -- 13.06.2021 %

How to use a GPG key stored on a flash drive to encrypt files? I was perplexed for sometime. Eventually I figured out instead of exporting, importing, file system linking.. you just use a remote key ring that contains the keys you want!

  1. Create the new key on the flash drive with
$ gpg --full-generate-key --homedir /Volumes/usb_flash_stick/key_homedir
  1. Use that new public key to encrypt files
$ gpg --encrypt-files --homedir /Volumes/usb_flash_stick/key_homedir -r XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ./file_a

This also bring the possibility of only storing the public key locally and having the secret key safe on the USB. See how to move keys snippet.

\- [ gpg ]

list tar contents -- 13.06.2021 %

Before putting a tar file somewhere hard to access like S3 Glacier I note what is in it. I create a manifest file. Simply list the file names within:

$ tar tvf example.tar

With more automation worked in I came up with this:

  • list all the tar files using fd (rust find replacment)
  • list content of each one
  • pipe that into a file for safe keeping
$ for x in $(fd -e tar) ; do (tar tvf "$x" && echo "\n") ; done > /tmp/example_manifest
\- [ tar ]