Angry Docker Purge

If you receive No Space left error on Docker this the brute force solution:

It would stop and delete ALL containers, images and volumes.

docker stop $(docker ps -aq); docker rm $(docker ps -aq); docker volume rm $(docker volume ls -qf dangling=true);docker system prune -af


4
Babar

Run same command on different input/inputs

If you have to run the same command repetitively over different input/inputs.

Example input:

c595270372e3
c04094382ae1
498a6505a1f7
4ac0e1872789
170a7f2ec51a
930743cb4e19


Command:

pbpaste | xargs -I id docker image rm id


Note: pbpaste is clipboard paste command for Mac, would be different for operating systems.

1
Babar

Check for IKEA item availability

A bit of a hack, but we want to be notified if an item becomes available at IKEA.

This is just a shell script that should work on any *NIX that has curl installed.

This is how their API response looks like (replace th with your country code): https://www.ikea.com/th/en/iows/catalog/availability/S99067497

There are only two stores in Thailand, both of which are close enough to the Oozou office, we only care that it's available in one of those stores.

#!/usr/bin/env bash

url="https://www.ikea.com/th/en/iows/catalog/availability/$1"

# This is the string that we want to see changed
string="0"

while true; do
  # Get the API response and return as string
  text=$(curl -s "$url")

  # Get a count of how many times the substring matches
  count=`grep -o "$string" <<< "$text" | wc -l`

  # If less than 2, then it's available!
  if (( $count < 2 )); then
    echo "AVAILABLE, yay.";
    # Notify us via Slack
    curl -X POST --data-urlencode "payload={\"channel\": \"…\", \"username\": \"ikea\", \"text\": \"Item $1 is now available at IKEA.\", \"icon_emoji\": \":shooping_bags:\"}" https://hooks.slack.com/services/…
    break
  else
    # If not available, sleep for 30s
    echo "$1 not in stock"
    sleep 30
  fi
done



Pass in your item ID to start:

$ ./check.sh S99067497


5
Const

Create Element with attributes in one line

We normally use this kind of code to create a HTML element with JS:

const a = document.createElement('a')
a.setAttribute('class', 'foo bar')
a.setAttribute('id', 'id')
a.setAttribute('innerHTML', 'Hello World!')
a.setAttribute('href', 'https://www.google.com/')


Or some similar ways. But setting each attribute one by one is tedious!

We can do this in a single line if we want:

Object.assign(document.createElement('a'), {
  id: 'id',
  class: 'whatever class',
  href: 'https://www.google.com/',
  innerHTML: 'This is a link'
});


One more way to mess up with your JS codebase!

21
Babar

Change Ruby version for single command with rbenv

You can switch Ruby version temporarily with rbenv local

But it'd need to be switched back after you're done. Specially for a single command it's kind of pain. In those cases, you can use RBENV_VERSION env variable.

RBENV_VERSION=2.6.3 ruby -v


This might be useful in a shell script or so.

1
Babar

Go through a file Git history (GUI)

$ gitk --follow file


Ali

Reset Postgres table sequence after DELETE

When deleting recent records from a table, you can reset it's sequence counter if auto-increment field continuity is important to you. Consider actions on the following table, users
 
select max(id) from users;

+-----+ | max | +-----+ | 896 | +-----+ 1 rows in set (0.03 sec)

Then, some delete action against most recent rows: 
delete from users where created_at > timestamp 'yesterday';
96 rows in set (0.15 sec) 

The next auto-increment value for id would be 897 on the next insert. Use the following command to reduce the sequence value for the users table by 96.
select setval('users_id_seq',(select max(id) from users));

+--------+
| setval |
+--------+
| 800    |
+--------+
1 rows in set (0.04 sec)
829
Jeff

Kill unresponsive SSH session

Occasionally an SSH session may become unresponsive. If I put my laptop to sleep this happens fairly often. Rather than killing the tab you can use SSH Escape Characters to kill the session

$ ~.


To view all escape characters check man ssh and search for ESCAPE CHARACTERS $ /ESCAPE CHARACTERS

Joe

PostgreSQL UPDATE Join

This query shows how to use the PostgreSQL UPDATE join syntax to update data in a table based on values in another table. for example;

updating subscription_missions.giftbox_type with rewards.gift_box_type through mission_rewards table (tested on 300k records, crazy level fast)

  UPDATE
        subscription_missions sm
  SET
        giftbox_type = r.gift_box_type
  FROM
        mission_rewards mr,
        rewards r
  WHERE
        sm.giftbox_type IS NULL
        AND sm.id = mr.subscription_mission_id
        AND mr.reward_id = r.id;



1
Ali

Kill rails that got stuck

I'd like to kill rails running w/specific port. (My laptop running several rails servers. )

kill -9 $(lsof -i :3000|grep ruby|head -1|awk '{print $2}')

Alyson