adamfallon.com

The big software cheatsheet

1 Software Cheatsheet

1.1 Git

1.1.1 Re-apply a .gitignore

git rm -r --cached .
git add .

1.2 *Nix

1.2.1 Create a self signed cert

openssl genrsa -aes256 -passout pass:gsahdg -out server.pass.key 4096
openssl rsa -passin pass:gsahdg -in server.pass.key -out server.key
rm server.pass.key
openssl req -new -key server.key -out server.csr
openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt

1.2.2 Enable ufw on docker hosts

sudo vim /etc/default/docker
DOCKER_OPTS="--iptables=false"
sudo reboot now

1.2.3 Reinstall packages in linux

# Create a backup of what packages are currently installed:
dpkg --get-selections > ~/dotfiles/apt_packages.txt
git commit -am "Updated apt packages"
git push
# Then (on another system) restore installations from that list:
dpkg --clear-selections
sudo dpkg --set-selections < list.txt

1.2.4 Create a Desktop file

# Create a file under ~/.local/share/applications/
# Example file here:
[Desktop Entry]
Name=Deus Ex: Game of the Year Edition
Comment=Play this game on Steam
Exec=steam steam://rungameid/6910
Icon=steam_icon_6910
Terminal=false
Type=Application
Categories=Game;

1.2.5 Find process using port

sudo lsof -i :5432

1.2.6 Disk usage + Sort

du -sh -- * | sort -h

1.2.7 Use specfic version of apt source

sudo apt -t experimental my-cool-dep

1.2.8 Add a cronjob

# list cronjobs
crontab -l
# edit cronjob
crontab -e
# add one and quit, example here
*/5 * * * * /usr/bin/offlineimap >/dev/null 2>&1
# remove cronjob
crontab -r

1.2.9 Put together multipart rar

unrar x -e first-par-01.rar

1.2.10 See all fonts

fc-list

1.3 macOS

1.3.1 Delete a disk from the command line

diskutil list
diskutil eraseDisk ExFAT USB64 /dev/disk4

1.4 SourceGraph

export SRC_HOST=http://localhost:7080
export SRC_ACCESS_TOKEN=some_token

docker run --publish 7080:7080 --publish 127.0.0.1:3370:3370 \
       --publish 127.0.0.1:443:7443 \
       --rm \
       --volume ~/.sourcegraph/config:/etc/sourcegraph \
       --volume ~/.sourcegraph/data:/var/opt/sourcegraph \
       --volume ~/dotfiles/self-signed-cert:/etc/nginx \
       sourcegraph/server:3.31.0 

1.5 Docker

1.5.1 General

# Remove all images
docker system prune -a --volumes
# Connect to a running system
docker exec -it <container-id> bash
# Disconnect

1.5.2 Create postgres docker-compose file

version: "3"
services:
    database:
        image: "postgres"
        env_file:
            - database.env
        volumes:
            - database-data:/var/lib/postgresql/data

volumes:
    database-data:
POSTGRES_USER=bookmark_admin
POSTGRES_PASSWORD=changeme
POSTGRES_DB=bookmarks

1.5.3 Copy folder from host to container

docker cp <FILE> <CONTAINER_ID>:<CONTAINER_FILE_LOCATION>
docker cp ~/code/iOS-Trainline 02eb85ead1b4:/code

1.6 Jupyter

python3 -m pip install virtualenv
python3 -m virtualenv jupyterenv
source jupyterenv/bin/activate
pip install jupyter
jupyter notebook

1.7 Postgres

1.7.1 Find users

SELECT usename AS role_name,
CASE 
WHEN usesuper AND usecreatedb THEN 
CAST('superuser, create database' AS pg_catalog.text)
WHEN usesuper THEN 
CAST('superuser' AS pg_catalog.text)
WHEN usecreatedb THEN 
CAST('create database' AS pg_catalog.text)
ELSE 
CAST('' AS pg_catalog.text)
END role_attributes
FROM pg_catalog.pg_user
ORDER BY role_name desc;

1.7.2 Create table

create table users (
user_id 					serial primary key,
email 						text not null unique,
password_digest 	text not null
);

CREATE TABLE bookmark (
bookmark_id     text CONSTRAINT firstkey PRIMARY KEY,
title						text not null,
date						date,
user_id					integer,					

foreign key (user_id) references users(user_id)
);

1.7.3 Insert into table

insert into users (email, password_digest) values ('[email protected]', 'changeme');
insert into bookmark (bookmark_id, title, url, date, user_id) values (1, 'what I am up to now - martin fowler', 'https://news.ycombinator.com/item?id=28325452', NOW(), 1)

1.7.4 Update a table

alter table bookmark
add column url text;

1.8 Emacs Lisp

1.8.1 Run code after async shell command

You can specify the output buffer for async-shell-command. The shell runs as a process of the output buffer. You can get that process with get-buffer-process. Define your own process sentinel for the shell and set it with set-process-sentinel. It is wise to run shell-command-sentinel from your sentinel since that is the sentinel actually set by async-shell-command.

Note that the output buffer may not be associated with any other process when you call async-shell-command. Otherwise an other buffer could be used as process buffer for the shell command (see the documentation of the variable async-shell-command-buffer).

There follows an example:

(defun do-something (process signal)
  (when (memq (process-status process) '(exit signal))
    (message "Do something!")
    (shell-command-sentinel process signal)))

(let* ((output-buffer (generate-new-buffer "*Async shell command*"))
       (proc (progn
               (async-shell-command "sleep 10; echo Finished" output-buffer)
               (get-buffer-process output-buffer))))
  (if (process-live-p proc)
      (set-process-sentinel proc #'do-something)
    (message "No process running.")))


1.9 Jq

Given some json, turn result into an array, create a new json with fields headers, body and create that from all the fields that contain a header with value matching "graph.facebook.com"

cat facebook.json | jq '[.[] | {headers: .request.header.headers, body: .request.body} | select(.headers[].value | match("graph.facebook.com")) ]'

2 Links

Author: Adam Fallon

Created: 2022-01-05 Wed 11:20

Validate