Sunday, February 28, 2016

How to figure out static dependencies of a library for example libxml-2.0

Alright, this is fairly common question when you are trying to generate a static binary or have come across situations when you want to find out all the dependencies of a library.

Example:
The problem: How the hell do I figure out all the static dependencies of libxml-2 ?

The solution: Its easy, use pkg-config !!

Here is how to list all the static dependencies of libxml-2

Run the below command:

pkg-config --static --libs libxml-2.0

and this returns a list

-lxml2 -lpthread -lz -lm

Saturday, February 27, 2016

All permutation of a string using Go (Golang)

I have been playing around a bit with Golang and I thought the best way to learn about a prog. language is to write some code.

The problem: Write all permutations of a given string

The solution:

Run code here : http://play.golang.org/p/h4Kx6IS_lE

package main
import "fmt"
/*
Return all permutations of a string
Example,
Find all perm of abc:
We will find all perms of the given string based on all perms of its substring
To find all perms of abc, we need to find all perms of bc, and then add a to those perms
To find all perms of bc, we need to find all perms of c, and add b to those perms
To find all perms of c, well we know that is only c
Now we can find all perms of bc, insert c at every available space in b
bc --> bc, cb
Now we need to add a to all perms of bc
abc, bac, bca (insert a in bc) -- acb, cab, cba (insert a in cb)
*/
func main() {
fmt.Printf("All perms are - %v", getPerms("abc"))
}
func getPerms(str string) []string {
// base case, for one char, all perms are [char]
if len(str) == 1 {
return []string{str}
}
current := str[0:1] // current char
remStr := str[1:] // remaining string
perms := getPerms(remStr) // get perms for remaining string
allPerms := make([]string, 0) // array to hold all perms of the string based on perms of substring
// for every perm in the perms of substring
for _, perm := range perms {
// add current char at every possible position
for i := 0; i <= len(perm); i++ {
newPerm := insertAt(i, current, perm)
allPerms = append(allPerms, newPerm)
}
}
return allPerms
}
// Insert a char in a word
func insertAt(i int, char string, perm string) string {
start := perm[0:i]
end := perm[i:len(perm)]
return start + char + end
}
view raw permutations.go hosted with ❤ by GitHub



Saturday, January 16, 2016

Dockerizing Sinatra app with reloading (Docker + Sinatra + foreman + rerun)

Lately I have been trying to learn more about Docker, so I picked up a task to dockerize a Sinatra app.

Here is an excellent training material which includes 3 hours of video about docker:
https://training.docker.com/self-paced-training

Here is another introduction to docker by Codeship:
http://blog.codeship.com/using-docker-for-rails-development/

Assumptions:
  1. You have a Sinatra app up and running using foreman.
  2. You have docker machine installed. We will be using docker compose to dockerize

Lets get started:

Step 1: Create a file named Dockerfile in the root of your project with the following content

FROM ruby:2.2.0
RUN mkdir -p /var/app
COPY Gemfile* /var/app/
WORKDIR /var/app
ENV environment=development
RUN bundle install

Step 2: Create a file named docker-compose.yml in the root of your project with the following content

+web:
+ build: .
+ ports:
+ - '9292:9292'
+ volumes:
+ - '.:/var/app'
+ tty: true
+ command: ["rerun", "--force-polling", "foreman", "start"]


Step 3: Lets get reloading Sinatra app on changes working (ONLY if you want to reload your Sinatra app on changes using rerun gem)

At the moment to get rerun to work properly with Docker you have to supply --force-polling option
as mentioned here
https://github.com/alexch/rerun#vagrant-and-virtualbox

The catch is that the supporting code is only in master(as of now) and is not pushed to the latest gem.
So chances are if you install the latest version of rerun gem, you might not get the force-polling option 

use this in your gemfile development section to load the master branch of the gem
gem 'rerun', :git => 'https://github.com/alexch/rerun.git'
gem 'foreman'

Step 4: And thats all the config thats needed. Run
    a) docker-compose build
    b) docker-compose up