Tuesday, August 15, 2017

My Emacs Initialisation File

I've been writing Clojure codes and using Frederick Giasson's Emacs init file for almost a month. I find his init file gravitates towards simplicity and pragmatism. I've used other people's init file but end up disliking how my Emacs behaves. I would almost certainly blame the unnecessary packages for this.

I've copied Frederick's init file, excluded monokai-theme and turned off a few other settings. Below is the GitHub link: https://github.com/lyeung/dot-emacs


Sunday, March 26, 2017

SSH X11 Forwarding and Mac OS X Sierra

I want to run remote Firefox locally on my computer. I have an SSH server running on Ubuntu 15 and an SSH client on Mac OS X Sierra. I've done this before, I would've thought it's as easy as enabling "-X", right? No so, this time. It looks like luck isn't on my side.

I have to enable debugging by using "-vvv" from SSH client to understand the problem:

ssh -vvv -X user@remote.com

The log contained:

debug1: No xauth program.

Warning: untrusted X11 forwarding setup failed: xauth key data not generated

Obviously, I need the extra "-Y" to enable trusted X11 forwarding:

ssh -vvv -XY user@remote.com

This time, it contained:

debug1: No xauth program.

Warning: No xauth data; using fake authentication data for X11 forwarding.

The problem must be related to xauth and I can't find this anywhere on Sierra. Googling the web, the link below came to my attention:
https://origin-discussions-us.apple.com/thread/7685786?start=0&tstart=0

So, I the solution here is to install XQuartz as this comes with the xauth binary.

After the installation, Sierra SSH configuration files /etc/ssh/sshd_config and etc/ssh/ssh_config both contains:


XAuthLocation /opt/X11/bin/xauth

A new directory X11 exists under "/opt" and xauth exists under "/opt/X11/bin".

Another attempt with the following command shows Firefox running remote on Ubuntu and displayed locally:

ssh -XY user@remote.com firefox

To improve local connection speed, you could request compression by adding "-C" when executing SSH:

ssh -XYC user@remote.com firefox


Tuesday, February 7, 2017

My First Macro

I'm writing my first test code to cover my HugSQL.  I'm still on my training wheels, so I can't be more fancier here:

-- :name get-user-type :? :*
-- :doc get user by type
select id, name from users where type = :type order by id


Now comes the mythical test code. Let's suppose "db/add-user!" exists, performs database inserts into the users table and returns 1 as return value.

(deftest test-user
  (jdbc/with-db-transaction [t-conn *db*]
    (jdbc/db-set-rollback-only! t-conn)
    (is (= 1 (db/add-user!
              t-conn
              {:username      "u123"
               :type          "admin"})))
    (is (= {:username      "u123"
            :type          "admin"}
           (db/get-user-type t-conn {:type "admin"}))))))

But alas! This is an epic failure!

(expected: {:username "u123",
            :type "admin"}
  actual: ({:id 1,
            :username "u123",
            :type "admin"})

It appears that ":id" value is an auto-generated primary key and this equality test is not going to work.


Perhaps, I could define a function to remove the id before comparing? This might work as the id is a surrogated key automatically populated by the database.

(defn match-record
  [expected record]
  (= expected (dissoc record :id)))


Or perhaps I could raise the bar higher by writing a macro? After all, I've heard Clojure macro is a powerful language extension.

(defmacro match-record
  [expected record]
  `(= ~expected (dissoc ~record :id)))


This is really exciting - my first ever Clojure macro!


So, rewriting my test code gives me,

(deftest test-user
  (jdbc/with-db-transaction [t-conn *db*]
    (jdbc/db-set-rollback-only! t-conn)
    (is (= 1 (db/add-user!
              t-conn
              {:username      "u123"
               :type          "admin"})))
    (let [result (db/get-user-type t-conn {:type "admin"})]
        (is (match-record {:username   "u123"
                           :type       "admin"}
                          (first result))))))



I know my macro is sub-optimal here and there are better ways of doing this. But hey! I'm certain my dear mother will be proud of me!