Snippets
Collection of my code snippets
Tags:
filtered git diff
25-06-21
%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
- [ git]
locally host istanbul js results
23-06-21
%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
markdown to org file conversion
21-06-21
%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
gpg usb workflow
13-06-21
%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!
- Create the new key on the flash drive with
$ gpg --full-generate-key --homedir /Volumes/usb_flash_stick/key_homedir
- 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-21
%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]
split files
13-06-21
%To prevent upload errors to S3 Glacier I keep my files ~500mb. So larger ones must be split.
$ split -b 500mb example.txt example.txt.part
- [ split]
org-roam capture templates
06-04-21
%Recently I've started using org-roam, so far so good. Utilizing capture buffers I create notes for work and my reefing aquarium hobby. Adding the roam tags manually became a pain so now I've figured out a way to prefill them with capture templates.
(setq org-roam-capture-templates
'(("r" "reef" plain (function org-roam-capture--get-point)
"%?"
:file-name "%<%Y%m%d%H%M%S>-${slug}"
:head "#+title: ${title}\n#+roam_tags: reef"
:unnarrowed t)
("w" "work" plain (function org-roam-capture--get-point)
"%?"
:file-name "%<%Y%m%d%H%M%S>-${slug}"
:head "#+title: ${title}\n#+roam_tags: work"
:unnarrowed t)
("d" "default" plain (function org-roam-capture--get-point)
"%?"
:file-name "%<%Y%m%d%H%M%S>-${slug}"
:head "#+title: ${title}\n"
:unnarrowed t)))
source: https://www.reddit.com/r/orgmode/comments/lmlsdr/simple_question_re_orgroam_how_to_access_capture/
restart nginx
06-02-21
%Normally I use
$ sudo systemctl restart nginx
but recently I had to take more drastic measures.
sudo pkill -f nginx & wait $!
sudo systemctl start nginx
source -- stackoverflow
- [ nginx]
run changed tests
01-02-21
%npm run test -- $(git diff master --name-only | rg 'test.ts') --coverage=false
strip audio from video file
09-01-21
%Easy way to remove audio from a video file using ffmpeg
ffmpeg -i $input_file -c copy -an $output_file
source: superuser
- [ ffmpeg]
gzipping an existing tar
14-10-20
%Part of my work process is taking lots of screenshot, ~5 per day. Then I back them up in AWS S3 Glacier once a month, using freeze app. Like to start with creating a regular tar file in /tmp.
$ tar cvf /tmp/pic_dump_14_10_20.tar ~/Desktop/**/*.png
Then append few more images. r
in this case sanding for append..
$ tar rfv /tmp/pic_dump_14_10_20.tar ~/Pictures/resized/*
Now that the tar is complete I double check it by listing the files.
$ tar tf /tmp/pic_dump_14_10_20.tar
Lastly I need to compress the tar and I was confused if I could use tar command itself to compress a tar into a tar.gz but turns you use gunzip.
$ gzip /tmp/pic_dump_14_10_20.tar
source: https://alvinalexander.com/blog/post/linux-unix/how-work-files-tar-gzip-tgz/ (4)
tmux plus screen
17-08-20
%Recently I was sshed into my home pi server trying to sftp some big files from a remote server. Some of the transfers are huge, 30gb+. On our internet that will take a while. Not wanting to leave the shh terminal session open that whole time I used screen on the pi. Idea was to create a screen session start the transfer detach and comeback few hours later. Detaching was a bit tricky. <Ctrl><a> <d> is the default detach command for both tmux running on my mac and screen running on the pi. So when I tried to detach from the screen session tmux would detach instead. 😡
After messing with configs and some searching turns out if you press the tmux prefix key twice it sends it once to the child shell. So eventually I was able to detach from the screen session with:
<Ctrl><a> <Ctrl><a> <d>!!
sources:
npm i vs npm ci
17-08-20
%Today I discovered npm ci
from a colleague. It does a clean install wiping out the node_modules
before installing. For me this is perfect because I often find myself doing
$ rm -rf node_modules && npm i
no need just run npm ci
*note: you'll still want to do npm i
when installing packages as npm ci
does not update package*..
source:
prevent vim auto new lines
13-08-20
%Sometimes when typing vim will automatically start a newline. This is an expected behavior but at times can be really annoying. Ex working with macros, you can recored one on a short line that breaks on longer lines 😟. The amount of text before vim will break to new line while typing is controlled via the textwidth setting. So a fix is pretty simple. If don't want the behavior just set textwidth to a big number. ex:
: set tw=500
Here is a asciicast of the problem and solution in action:
{{< asciicast-with-caption id="353148" title="demo of setting tw" >}}
- [ vim]
auto find ssh keys
12-08-20
%I use to always pass a key when sshing ex:
$ ssh -i ~/.ssh/de2 travis@vxxxxxxxxxxxxxxxxxxx.megasrv.de
That can be a bit annoying. I know two fixes:
add private key to ssh-agent
$ ssh-add ~/.ssh/de2
Now when you ssh you need not include the -i ~/.ssh/de2
because the ssh-agent will find it
automatically.
*note: this resets once you reboot
configure individual host
You can configure individual hosts to use a spefic private key my editing your ~/.ssh/config
:
Host de2
HostName vxxxxxxxxxxxxxxxxxxxx.megasrv.de
User travis
IdentityFile ~/.ssh/de2
Now you only need to
$ ssh de2
source -- https://www.techrepublic.com/article/how-to-use-per-host-ssh-configuration/
- [ ssh]
using short server names
12-08-20
%It use to be when I was trying to ssh into one of my servers I would just:
By adding something like this to your ~/.ssh/config
:
Host de1
HostName vxxxxxxxxxxxxxxxxxxxx.megasrv.de
User travis
Host de2
HostName vyyyyyyyyyyyyyyyyyyyymegasrv.de
User travis
Host de3
HostName vzzzzzzzzzzzzzzzzzzz.megasrv.de
User travis
Host nyc1
HostName 123.123.123.1233
User travis
sshing becomes as easy as:
$ ssh de2
source -- https://www.techrepublic.com/article/how-to-use-per-host-ssh-configuration/
- [ ssh]
disable user
11-08-20
%In this case disabling the user named ubuntu from logging in. This includes logging in via ssh.
$ sudo usermod --expiredate 1 ubuntu
- [ sysadmin]
automatic tmux session names
13-07-20
%This month I ditched XQuartz and am back to using Tmux. One part I found tedious was manually naming sessions. I wrote this little alias help. When run from outside a tux session to creates a new session named after the current directory. Ex when in /users/t.shears/dev/cool-app
a session named cool-app is created.
Excerpt from my .zshrc
alias tmux_new="tmux new -s \$(pwd | awk -F "/" '{print \$NF}' | sed 's/\./_/g')"
- [ tmux]
file search plus size
02-07-20
%How big are the fonts in this project? Not sure where they are even. This command finds the files using fd then prints file size.
$ fd woff | xargs du -h
20K src/assets/fonts/brandon_text_medium.woff2
20K src/assets/fonts/brandon_text_regular.woff2
20K src/assets/fonts/chronicle_light.woff2
20K src/assets/fonts/chronicle_roman.woff2
4.0K types/woff
4.0K types/woff2
convert .mkv to .mp4
30-06-20
%Before I updated my OBS settings to record to .mp4 files I manually converted .mkv files to .mp4. This shell command does that for every recording in a directory deleting the original.
$ for x in $(ls *.mkv | awk -F "." '{print $1}') ; do ffmpeg -i $x.mkv -c copy $x.mp4 && rm $x.mkv ; sleep 3; done
- [ ffmpeg]
wipe a mongo collection
29-06-20
%Currently I run a shared mongo instance in my k8s cluster used by a bunch of my side projects. Every once in a while I have to manually execute mongo commands this is how I do so.
Manually ssh into the pod:
$ kubectl exec -it shared-mongo-xxxxxxxxxx-xxxxx -- /bin/bash
Then launch the mongo shell and hack away:
$ mongo
> db2 = db.getSiblingDB('coolDB')
coolDB
> db2.getCollectionNames()
[ "coolThings" ]
> db2.coolThings.count()
666
> db2.coolThings.remove({})
WriteResult({ "nRemoved" : 666 })
> db2.coolThings.count()
0
source:
- [ mongo]
extending gpg keys
22-06-20
%Don't let those keys expire. 🚨
Time to edit some keys:
gpg --edit-key t@travisshears.com
Secret key is available.
sec rsa2048/D4C2E4DFAB8BABF8
created: 2018-07-18 expires: 2020-07-17 usage: SC
ssb rsa2048/25C629D0FECC25B9
created: 2018-07-18 expires: 2020-07-17 usage: E
ssb rsa4096/97F7C2B46E6C5D11
created: 2019-09-28 expires: 2023-09-28 usage: E
- select key to change
key 1
expire
- duration:
1y
- repeat until every key is updated
resulting output should be something like:
sec rsa2048/D4C2E4DFAB8BABF8
created: 2018-07-18 expires: 2021-06-22 usage: SC
ssb rsa2048/25C629D0FECC25B9
created: 2018-07-18 expires: 2021-06-22 usage: E
ssb rsa4096/97F7C2B46E6C5D11
created: 2019-09-28 expires: 2021-06-22 usage: E
lastly save
and done
source:
- [ gpg]
moving gpg keys
20-06-20
%New laptop? Got to move over those GPG keys.
$ cd /tmp && mkdir gpg_export
$ gpg --output gpg_export/main_pub.gpg --armor --export t@travisshears.com
$ gpg --output gpg_export/main_sec.gpg --armor --export-secret-key t@travisshears.com
$ tar cvfz gpg_export.tar.gz ./gpg_export
$ gpg --symmetric ./gpg_export.tar.gz
Then move the encrypted tar to the new computer, with airdrop for example.
To import the keys.
$ gpg --decrypt gpg_export.tar.gz.gpg > gpg_export.tar.gz
$ tar xvfz gpg_export.tar
$ gpg --import gpg_export/main_sec.gpg
$ gpg --import gpg_export/main_pub.gpg
Source:
- [ gpg]
k8s deployment.yaml env vscode snippet
20-06-20
%Most of my personal projects are deployed via kubernetes. I write a lot of deployment.yaml files. In order to keep them clean and checked in to version control I keep sensitive env variables in a config maps. Problem is adding values env values to deployment.yaml files is pretty painful. This makes it a little less.
placed in yaml.json 😀 what a file name!
{
"env var from configmap": {
"prefix": "env",
"body": [
"- name: $1",
" valueFrom:",
" configMapKeyRef:",
" key: $1",
" name: configmapname"
],
"description": "env varable from config map, remember to replace configmapname with your configmap name"
}
}
- [ vscodekubernetes]