Friday, November 27, 2015

Terminal.app doesn't save history in El Capitan and you are using RVM

The cause is described here https://github.com/rvm/rvm/issues/3540#issue-113491283
The fix is here https://github.com/rvm/rvm/issues/3540#issuecomment-152824133

I copied the fix here just in case it's removed in the future

Another option is to create .bash_logout with a single line:

shell_session_update

Wednesday, November 4, 2015

Setup Debian/Ubuntu apt package cache server with Ansible

Install elnappoo.apt-cacher-ng with Ansible Galaxy and then follow the instruction in the README tab or README in the github repository. Refer to the test.yml file as an example. If you cache server doesn't have ufw running, don't forget to set apt_cacher_ng_setup_ufw: False

Thursday, October 29, 2015

How to get caller function in Clojure

I posted this question on Clojurians slack channel.
What's clojure equivalent of ruby's `caller` method? http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-caller
To put it another way, if a function is call from multiple places, at runtime, is there a way to print out caller for debugging purpose?
Thanks to Alex Miller. He answered
the Thread class has methods to inspect the stack
(Thread/dumpStack) will just dump that thread's stack to stderr
or (Thread/getStackTrace) gives you an array of StackTraceElement's that you can traverse and do whatever
Basically, we can just use Java's Thread class. To be more precise, getStackTrace is an instance method so we have to call it this way.
(.getStackTrace (Thread/currentThread))
We'll get back Java's array of StackTraceElement class.

You can copy and just use a function I wrote here https://gist.github.com/visibletrap/bdf16dd5596c9a72ec73

Tuesday, October 13, 2015

Convert FLV to MP4 with FFmpeg

Only for no AV recording
$ ffmpeg -i input.flv -c copy -copyts output.mp4
Tested on: OS X 10.11
Credit: http://superuser.com/a/860127

Thursday, September 24, 2015

How to clear terminal screen in Clojure

Print some ANSI characters
(print (str (char 27) "[2J")) ; clear screen
(print (str (char 27) "[;H")) ; move cursor to the top left corner of the screen
Credit: http://codereview.stackexchange.com/questions/17603/critique-my-clojure-game-of-life-code

Find out which process is using a particular port

I was never be able to remember options for netstat command. This option for lsof seems to be easier. Note that it requires a root access.
lsof -i :80
Change 80 to any port number you like.

Creadit: http://unix.stackexchange.com/a/106572

Tuesday, August 25, 2015

On OS X, Java Control Panel and Terminal show different Java versions

That's because Java Control Panel shows version of JRE for normal Mac user, but command line on Terminal shows version of JDK. To update JDK version, need to download the full package from Oracle website.
Older JDK version won't be replace automatically to allow us to switch back if we want. So we have to manually remove it by following the instruction here http://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html#A1096903

Credit: http://stackoverflow.com/a/28254333

Monday, July 13, 2015

[PostgreSQL] List index usage

Update: This query is much cooler Index size/usage statistics

--

In psql console, connect to the database you want to check its indexes usage. Then execute this query.
# SELECT * FROM pg_stat_user_indexes ORDER BY idx_scan DESC;
If your database is manage by Rails's schema, you could filter out auto generated public key indexes like so
# SELECT * FROM pg_stat_user_indexes WHERE indexrelname NOT LIKE '%_pkey' ORDER BY idx_scan DESC; 

Tuesday, June 16, 2015

Sort file name by number of changes in git history

git log --pretty=format: --name-only | sort | uniq -c | sort -rg | less
Literally copied from the answer of "Finding most changed files in git"

Tuesday, June 9, 2015

[PostgreSQL] Using some indexes from Composite index

If we had a composite index on three fields: we would be able to use it for queries on the first field, for queries on the first two fields and for queries involving all three fields, but not on any other field combination.
Copied directly from How to stop worrying and love your Postgres indexes 

Monday, May 25, 2015

[PostgreSQL] List running queries

In psql, run this command
select * from pg_stat_activity where datname = YOUR_DB_NAME and state = 'active'
 You might also what to order the result by query_start or state_change

Saturday, April 4, 2015

Start lein repl with test profile

When you try to run lein repl with test profile, you'll get warning/error message similar to this.
$ lein with-profile test repl
Warning: no nREPL dependency detected.
Be sure to include org.clojure/tools.nrepl in :dependencies of your profile.
... 
Error loading clojure.tools.nrepl.server: Could not locate clojure/tools/nrepl/server__init.class or clojure/tools/nrepl/server.clj on classpath: 
Error loading complete.core: Could not locate complete/core__init.class or complete/core.clj on classpath: 
Exception in thread "main" java.lang.ClassNotFoundException: clojure.tools.nrepl.server, compiling:(/private/var/folders/7d/98yf91rn0yz6wbh56h5q7rkh0000gn/T/form-init2792809345387087761.clj:1:1340)
The reason is because unlike default profile, tools.nrepl isn't added as dependency for test profile. You can verify this by comparing the output between lein pprint (or lein with-profile default pprint) and lein with-profile test pprint.

To fix this error, you can use the feature that lein provided combining profile like so;
$ lein with-profile default,test repl
With the command above, lein should start the repl without any errors.

Thursday, March 12, 2015

[VIM] Move screen before cursor reaching top or bottom

It's interesting that I didn't realize I had been living without this feature for years as a vim user. Thanks to @tenderlove and @searls for publishing their vim setup session video. In the beginning of this video, @tenderlove adds set scrolloff=2 to vimrc. scrolloff is a good config that everyone who's using vim should have in their vimrc.

What it does is to tell vim to move screen before cursor reaching 2 top or bottom lines (2 is an arbitrary number). Give it a try. I bet you'll like it.

I went observe on JetBrains' IDE, they are configured to behave the same with setting scrolloff=1 on vim.

Sunday, January 11, 2015

Sample config for rabbitmq_auth_mechanism_ssl that works

This is a sample rabbitmq.config for enabling both client-side certificate and ssl authentication.

[
    {rabbit, [{auth_mechanisms, ['EXTERNAL']},
              {ssl_listeners, [5671]},
              {ssl_cert_login_from, common_name},
              {ssl_options, [{cacertfile,"/path/to/ca-cert"},
                             {certfile,"/path/to/client-cert"},
                             {keyfile,"/path/to/client-key"},
                             {verify,verify_peer},
                             {fail_if_no_peer_cert,true}                           
              ]}  
    ]}  
].

Notes:
  1. Make sure that you enabled rabbitmq_auth_mechanism_ssl plugin with 
    rabbitmq-plugins enable rabbitmq_auth_mechanism_ssl. It will be target for the EXTERNAL auth mechanim.
  2.  In your process of creating client certificate, set your rabbitmq client username as CN. i.e. CN=client_username. You don't need to provide client's login name anymore when creating rabbitmq connection in your client code.
Tested with RabbitMQ version 3.4.2

Wednesday, January 7, 2015

[Docker] Look inside running container

docker exec -t -i <container-name> /bin/bash

For looking inside an image, use

docker run -t -i <image-name> /bin/bash

Config number of file descriptors on Linux

Check file descriptor limit for a process. Vary depending on owner of the process $ cat /proc/{process-id}/limits Check current number of...