site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

whats-in-my-rc-2022.md (8432B)


      1 ---
      2 title: What's in My RC 2022
      3 description: A tour of my RC file in 2022.
      4 ---
      5 
      6 # What's in My RC 2022
      7 
      8 2022-12-19
      9 
     10 Welcome to a tour of my `.zshrc` file! I thought it would be fun to take a dive into the different tools I use to elevate my developer game.
     11 
     12 If you want to see my whole config, I keep all my setup files on GitHub: [https://github.com/unitehenry/config](https://github.com/unitehenry/config)
     13 
     14 Also check out last years: [What's in my RC 2021](/blog/whats-in-my-rc-2021)
     15 
     16 # The Variables
     17 
     18 ```bash
     19 # EDITOR
     20 export EDITOR="vi";
     21 export VISUAL="vi";
     22 
     23 # iCloud Directory
     24 export DOCS="/Users/$(whoami)/Library/Mobile Documents/com~apple~CloudDocs";
     25 
     26 # Work Directory
     27 export WORK="/Users/$(whoami)/Projects/lula";
     28 
     29 # Vault Address
     30 export VAULT_ADDR=https://vault.stallions.dev/
     31 ```
     32 
     33 *   Continuing to use vim, so just exporting my preferred editor variables
     34 *   Reference my iCloud directory which I use to keep all my personal files
     35 *   Adding an environment variable to help reference [where my company is storing secrets](https://www.vaultproject.io/)
     36 
     37 # Credentials
     38 
     39 ```bash
     40 # Credentials Fetcher
     41 function op-create() {
     42   op item template get Login > /tmp/login.json;
     43   if [ -n "$3" ]
     44   then
     45     echo $(cat /tmp/login.json | jq -r -c "(.fields[] | select(.id | contains(\"username\"))) .value = \"$2\"") > /tmp/login.json;
     46     echo $(cat /tmp/login.json | jq -r -c "(.fields[] | select(.id | contains(\"password\"))) .value = \"$3\"") > /tmp/login.json;
     47     op item create --template /tmp/login.json --title $1;
     48   else
     49     echo $(cat /tmp/login.json | jq -r -c "(.fields[] | select(.id | contains(\"username\"))) .value = \"$2\"") > /tmp/login.json;
     50     op item create --template /tmp/login.json --title $1 --generate-password;
     51   fi
     52   rm /tmp/login.json;
     53 }
     54 
     55 function op-list() {
     56   op item list --format=json | jq -c -r '.[].title';
     57 }
     58 
     59 function op-username() {
     60  op item get $@ --format=json | jq -c -r '.fields[] | select(.id | contains("username")) | .value';
     61 }
     62 
     63 function op-password() {
     64   op item get $@ --format=json | jq -c -r '.fields[] | select(.id | contains("password")) | .value';
     65 }
     66 
     67 function op-delete() {
     68   op item delete $@;
     69 }
     70 ```
     71 
     72 I used to be anti hosted password managers, but I was recommended using 1password and their [CLI client](https://developer.1password.com/docs/cli) to manage my passwords.
     73 
     74 # Code Formatter
     75 
     76 ```bash
     77 ## Code Formatter
     78 function format-file() {
     79   export FILENAME="$(basename $@)";
     80   export EXTENSION="${FILENAME##*.}";
     81 
     82   if [ $EXTENSION = 'py' ]
     83   then
     84     yapf --in-place $@;
     85     return 0;
     86   fi
     87 
     88   if [ $EXTENSION = 'php' ]
     89   then
     90     php-cs-fixer fix $@;
     91     rm .php_cs.cache;
     92     return 0;
     93   fi
     94 
     95   if [ $EXTENSION = 'sql' ]
     96   then
     97     npx sql-formatter-cli --file $@ --out $@;
     98     return 0;
     99   fi
    100 
    101   npx prettier --write --single-quote $@;
    102 
    103   unset FILENAME; unset EXTENSION;
    104 }
    105 ```
    106 
    107 Using the same code formatter, but added something to handle `sql` files now!
    108 
    109 # Spell Check
    110 
    111 ```bash
    112 ## Spellcheck
    113 function spellcheck-file() {
    114   npx spellchecker-cli --files $@;
    115 }
    116 ```
    117 
    118 Handy spellchecker utility through `npx` is always useful.
    119 
    120 # What the Commit
    121 
    122 ```bash
    123 ## What the Commit
    124 function wtf() { git commit -am "$(curl http://whatthecommit.com/index.txt)"; }
    125 ```
    126 
    127 For when I don't know what to type in for a commit message.
    128 
    129 # Cheat Sheet
    130 
    131 ```bash
    132 Cheat Sheet
    133 ## Cheat
    134 function cheat(){ curl https://cheat.sh/"$@"; }
    135 ```
    136 
    137 When I don't remember how to use a certain utility, this [cheatsheet API](https://cheat.sh/) is always handy.
    138 
    139 # Git
    140 
    141 ```bash
    142 ## Commit Count
    143 function commit-count() {
    144   if [ -n "$1" ]
    145   then
    146     git rev-list --count $1;
    147   else
    148     echo "commit-count <branch-name>";
    149   fi
    150 }
    151 
    152 Not as useful, but interesting. I use this to get the number of commits on my current working branch.
    153 ```
    154 
    155 # Document Generation
    156 
    157 ```bash
    158 ## Generate Markdown
    159 function generate-doc() { 
    160   cp -rf . /tmp;
    161   if [ -n "$2" ]
    162   then
    163     pandoc -s $1 -c $2 -o "/tmp/$1.html";
    164   else
    165     pandoc -s $1 -o "/tmp/$1.html"; 
    166   fi
    167   open "/tmp/$1.html";
    168 }
    169 
    170 ## Generate Slide
    171 function generate-slide() {
    172   # https://revealjs.com/config/
    173   pandoc -t revealjs \
    174     -V controls="false" \
    175     -V progress="false" \
    176     -V navigationMode="linear" \
    177     -V transition="none" \
    178     -s $1 -o "/tmp/$1.html";
    179   cp -rf . /tmp;
    180   open "/tmp/$1.html";
    181 }
    182 
    183 function mmdc() {
    184   npx @mermaid-js/mermaid-cli $@;
    185 }
    186 ```
    187 
    188 Continuing to leverage [pandoc](https://pandoc.org/) whenever I need to create some quick slides or a document.
    189 
    190 # Homebrew Install
    191 
    192 ```bash
    193 ## Homebrew Install Script
    194 function install-homebrew() { /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"; }
    195 ```
    196 
    197 Handy script to install [homebrew](https://brew.sh) on a new machine.
    198 
    199 # Vundle Install
    200 
    201 ```bash
    202 ## Vundle Install Script
    203 function install-vundle() {
    204   git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim;
    205   sudo gem install vundle-cli;
    206   if ! grep -Fxq 'set rtp+=~/.vim/bundle/Vundle.vim' ~/.vimrc
    207   then
    208     echo "\nset nocompatible\nfiletype off\nset rtp+=~/.vim/bundle/Vundle.vim\ncall vundle#begin()\n\nPlugin 'VundleVim/Vundle.vim'\n\ncall vundle#end()\nfiletype plugin indent on" >> ~/.vimrc;
    209   fi
    210 }
    211 ```
    212 
    213 I use [vundle](https://github.com/VundleVim/Vundle.vim) to manage my vim packages. But this [handy cli](https://github.com/baopham/vundle-cli) makes installing/removing those packages much easier.
    214 
    215 # Docker
    216 
    217 ```bash
    218 function clear-docker() {
    219   docker system prune -a -f --volumes
    220 }
    221 
    222 function kill-docker() {
    223   killall Docker && open /Applications/Docker.app;
    224 }
    225 ```
    226 
    227 For when I just want to wipe docker of all images, containers, and volumes.
    228 
    229 Also something to help restart the docker daemon.
    230 
    231 # Google Cloud Platform
    232 
    233 ```bash
    234 function gcloud-adc() {
    235   gcloud auth login --update-adc
    236 }
    237 
    238 function use-gcloud-project () {
    239   gcloud config set project "$1";
    240 }
    241 
    242 function impersonate() {
    243     if [ -z "$1" ]; then
    244         echo "Must provide a service account to impersonate."
    245         return 1
    246     fi
    247     gcloud config set auth/impersonate_service_account "$1"
    248 }
    249 
    250 function unimpersonate() {
    251     gcloud config unset auth/impersonate_service_account
    252 }
    253 
    254 function connect() {
    255     # Set the context
    256     CONTEXT=${1}
    257 
    258     export PGUSER=$(vault kv get -field=username secrets/${CONTEXT}/postgres-terraform)
    259     export PGPASSWORD=$(vault kv get -field=password "secrets/${CONTEXT}/postgres-terraform")
    260     psql -h 127.0.0.1 -p 54320  -d "${CONTEXT}"
    261 }
    262 ```
    263 
    264 *   Logging into [Google ADC](https://cloud.google.com/docs/authentication/provide-credentials-adc)
    265 *   Setting GCP project to work with
    266 *   [Service account impersonation](https://cloud.google.com/iam/docs/impersonating-service-accounts)
    267 *   Connect to [cloud sql](https://cloud.google.com/sql)
    268 
    269 # Vault
    270 
    271 ```bash
    272 function vs() {
    273     # Verify inputs
    274     environment="$1"
    275     if [ "$environment" != "common" ] && [ "$environment" != "staging" ] && [ "$environment" != "production" ]; then
    276         echo "\"$environment\" is not a valid environment."
    277         return 1
    278     fi
    279 
    280     # Setup
    281     [ ! -d ~/.vault-tokens ] && mkdir ~/.vault-tokens
    282 
    283     # Move the current environment's token to the correct location
    284     if [ -f ~/.vault-tokens/current-environment ] && [ -f ~/.vault-token ]; then
    285         current="$(cat ~/.vault-tokens/current-environment)"
    286         cp ~/.vault-token ~/.vault-tokens/${current}
    287     fi
    288 
    289     # Set the new current environment
    290     echo "${environment}" > ~/.vault-tokens/current-environment
    291 
    292     # Set the correct vault address
    293     if [ "$environment" = "common" ]; then
    294         export VAULT_ADDR="https://vault.stallions.dev"
    295     else
    296         export VAULT_ADDR="https://vault.${environment}.stallions.dev"
    297     fi
    298 
    299     # Get the token from the current environment if it exist
    300     if [ -f ~/.vault-tokens/${environment} ] ; then
    301         cp ~/.vault-tokens/${environment} ~/.vault-token
    302     fi
    303 
    304     # Prompt login if the token is not valid
    305     if ! vault token lookup > /dev/null; then
    306         vault login --method oidc
    307     fi
    308 }
    309 ```
    310 
    311 This script helps me switch [vault](https://www.vaultproject.io/) contexts for pulling secrets in different environments like staging and production.
    312 
    313 # Generate ID
    314 
    315 ```bash
    316 function uuid() {
    317     uuidgen | tr '[:upper:]' '[:lower:]'
    318 }
    319 
    320 function vin() {
    321   echo "$(curl -sS https://randomvin.com/getvin.php\?type\=fake | tr -d '[:space:]')"
    322 }
    323 ```
    324 
    325 I use this to create new uuids and new [vin numbers](https://en.wikipedia.org/wiki/Vehicle_identification_number).