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 a Mongo Collection -- 29.06.2020 %

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.2020 %

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
  1. select key to change key 1
  2. expire
  3. duration: 1y
  4. 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.2020 %

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 I trasferend the encrypted tar via airdrop.

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.2020 %

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"
	}
}

Force push with --lease for safety -- 10.06.2020 %

Just found out, via tweet, from the insightful joshdholtz that there is a safer alternative to git push --force

Time to update my aliases:

alias gs="gst"
-alias gpf="gp --force"
+alias gpf="gp --force-with-lease"
alias gdc="gd --cached"

I use zsh shell’s git plugin that is why you see “gst” for git status and “gp” for git push. Highly recommend it

Source:

\- [ git ]

Open Notion Links -- 07.06.2020 %

Recently I’ve started using Notion app for note taking. There came times when I wanted to write about a specific directory, but then came the problem of how to link the dic to notion page. How about a tiny bash script? OSX’s built in open command is capable of opening links so I copied the link from notion and put in a docs.sh script. Boom! It opens the notion page but in the browser not the app 😟.

After some digging I found this repo for a chrome extension that opens notion links in the app. Taking a look at the source code it simply adds a /native in the url to open in the app. Updated my script and now it works 😀.

#!/usr/bin/env bash

open https://www.notion.so/native/Custom-Brew-Tap-e6f4faef5130442f94c9b06575806fc0
exit 0

If I continue to do this perhaps I’ll write a small tool to create the script files for me.

\- [ bash, notion ]

Twtxt Config Alias -- 30.05.2020 %

Recently I started using twtxt. I installed it with brew then setup it up via the quick setup command. The problem is when you put a config file location other then the default when you call the command you get a “✗ Config file not found or not readable. You may want to run twtxt quickstart." error. This is super annoying so I aliased the command to always include the location of my config file.

  alias gpf="gp --force"
+ alias tw="twtxt -c ~/.twtxt/config"

Now posting is as easy as:

$ tw tweet "authoring a snippet on how to configure alias twtxt"
\- [ zsh, twtxt ]

JSX Comments -- 26.05.2020 %

The other day at work we have an html comment in jsx slip on to stage. Made me relize I didn’t know how to leave comments in jsx myself.

So as a reminder DON’T do this:

<div>
    <!-- comment here -->
    <h2>Hello world</h2>
</div>

Instead do this:

<div>
    {/* comment here  */}
    <h2>Hello world</h2>
</div>

source:

https://wesbos.com/react-jsx-comments

\- [ react, jsx ]

Creating K8s registry secrets -- 29.03.2020 %

Hosting side projects in kubernetes and using gitlab container registry? This is the command I run to create the needed secret for the cluster to pull the image:

$ kubectl create secret docker-registry cool-project-gitlab \
    --docker-server=registry.gitlab.com \
    --docker-username=gitlab+deploy-token-666666 \
    --docker-password=xxxxxxxxxxxxxxxxxxxx \
    --docker-email=xxxxxxxxx@xmail.com

Then in the deployment.yml use the gitlab registry image and newly created image secret:

containers:
    image: registry.gitlab.com/btbtravis/cool-project:0.0.1
    imagePullPolicy: IfNotPresent
    name: cool-project-api
imagePullSecrets:
- name: cool-projects-gitlab
\- [ kubernetes ]

Vim Open File Under Cursor -- 06.03.2020 %

Half my time in the editor I’m not coding, I’m just browsing the file system trying to understand how things are connected. Trying figure out what needs to change in order to complete my task. This module imports this module which imports this module… There are many ways to navigate the file system and read files. Since I primarily code in VIM there are two main ways I navigate.

One is using the file explorer from plugin, NerdTree. It is great for getting a general overview of folder structure of a project and moving, deleting, or creating new files. Also really good for finding sibling files using the :NerdTreeFind command which I have remapped to . Where is lacks however is opening a nested import for example. When you want to jump to a different file / module directly.

This is where the goto file comes in. Using the following commands in conjunction with moving up and down the jump list, navigation is easy. Here is an example where I use the goto command and a few of its variations.

asciicast

demo using vim's goto and jumplist commands

The most helpful ones to learn are

  • <g><f> goto file, same window
  • <c-w><f> goto file, new split
  • <c-w><g><f> goto file, new tab

Also don’t forget to use the jump list

  • <c-o> or in my case remapped to <Leader><i> to jump backwards

Resources:

\- [ vim ]