George Opritescu

Developer from somewhere

irb config

A nice tool, for fast tests irb-config

Read More

handling uninitialized constant ActiveRecord::RecordNotFound

I kept getting “uninitialized constant ActiveRecord::RecordNotFound” in a Rack app. The fix was to add this at the beginning of the file:

require "active_record/errors"
Read More

formatting JSON from the commandline using Python

Say you’re doing a CURL request that returns some JSON. If you want to pretty-print it, here’s an easy way to do so:

curl http://some_host/action.json | python -m json.tool
Read More

find out where a rake task is defined

If you want to find out where a particular task is defined:

rake -W task_name
Read More

zsh script to repeatedly check if something changed

#!/bin/zsh
command_to_run=$1
expect_change_from=$2
notification_to_show=$3
default_sleep_time=$4

if [[ -z $command_to_run ]] || [[ -z $expect_change_from ]] || [[ -z $notification_to_show ]]; then
  echo "Command to run , expected_change or notification are blank"
  exit 1
fi

if [[ -z $default_sleep_time ]]; then
  default_sleep_time=30
fi

echo "Running $command_to_run ; expecting output different than $expect_change_from ; every $default_sleep_time"

while true; do
  res=$(eval ${command_to_run})
  if [[ "$res" == $expect_change_from ]]; then
    echo "Value didn't change"
    sleep $default_sleep_time
  else
    notify-send -u critical $notification_to_show
    break
  fi
done

I wanted to be notified when CDN Sumo finished provisioning, so I wrote the script above. It can be ran the following way:

zsh q.sh “heroku config:get CDN_SUMO_URL -a my_app” “my_app.herokuapp.com” “CDN_Sumo finished provisioning”

Read More
zsh

git read tree to move files around

I wanted to copy a directory with all of it’s files from a branch to the current one. This seems to have done the trick:

git read-tree --prefix=lib/zombie -u devel:lib/zombie

The prefix switch specifies where to place it, and the -u represents what I want to copy.

Read More
git

creating a bootable windows usb 7 stick from ubuntu

Simple way to create a bootable windows 7 usb stick:

 
$ sudo apt-get update 
$ sudo apt-get install gparted 
  • insert usb stick
  • format it as ntfs using gparted
  • in case it’s not set, go to Manage flags, and check boot
$ sudo mount -o loop your.iso /some/mount/point 
  • insert usb stick again so your ubuntu can remount it
 
$ cp -rvf /some/mount/point/* /media/wherever/ubuntu/mounted_it 
  • wait
  • reboot, and select your usb stick as your first boot device
  • done!
Read More

proxying with EventMachine

[EM-Proxy] (https://github.com/igrigorik/em-proxy) is awesome. Check the repo for examples of what it can do. I’ve also played a bit with it, and I made a very basic proxy that won’t forward requests if the User-Agent is curl. Sample code, basic as it is, can be found [here] (https://github.com/International/rev_proxy) .

Read More

thin with unix sockets

To start thin with a unix socket, run this:

thin start --socket /tmp/some.sock
Read More

zsh complete param

Been struggling to have autocompletion based on the param. This is the function:

load_pg () {
    pg_restore --verbose --clean --no-acl --no-owner -h localhost -U $1 -d $2 $3
}

and here’s the solution I found ( full code provided ) :

_load_pg() {
  _arguments "1: :->user" "2: :->db" "3: :->file"

  case $state in
    user)
      compadd $USER
    ;;
    db)
      compadd $(cat config/database.yml | grep -i database | awk '{print $2}')
    ;;  
    file)
      compadd $(ls *.dump*)
    ;;
  esac
}

load_pg() {
    pg_restore --verbose --clean --no-acl --no-owner -h localhost -U $1 -d $2 $3
}

compdef _load_pg load_pg
Read More