Golang for PHP Developers: Dependency management

The PHP ecosystem is quite spoiled with Composer for dependency management. Composer is very straightforward and easy to use. For the Golang ecosystem Glide is what Composer is for PHP; Glide is the easiest and (probably) most used dependency management tool for Golang.

This article compares Glide and Composer by four important Composer commands: init, install, require, and update. It does not cover installing Glide.

composer init for Glide

Glide also knows the “init” subcommand to initialise dependency management in your Golang project. This will set up the initial glide.yaml for you after asking you some questions about the package you’re setting up.

To create a glide.yaml for your project:

$ glide init

Glide will then create the glide.yaml file and it will also try to populate it. If you initialise glide on a codebase that already has dependencies mentioned in the .go source files.

composer install for Glide

Now that you know how to initialise glide in the project, it is time to install the dependencies. Composer and Glide are very similar in this aspect. To install dependencies simply call:

$ glide install

Glide will then install dependencies in the vendor/ folder, just as Composer does.

composer require for Glide

Glide’s “get” subcommand is the equivalent of Composer’s “require”. It also works much the same. Take the following two commands as an example:

$ composer require rtuin/somepackage

# Add a dev dependency
$ composer require --dev rtuin/somepackage

# Or, with version specified
$ composer require rtuin/somepackage:^1.0

Glide’s counterpart, note the # instead of : to denote a version:

$ glide get github.com/rtuin/somepackage

# Add a dev dependency
$ glide get --test github.com/rtuin/somepackage

# Or, with version specified
$ glide get github.com/rtuin/somepackage#^1.0

Glide respects the same way for denoting version numbers as Composer does.

composer update for Glide

Composer “update and Glide “update” have exactly the same results. It will find new dependency versions based on the packages listed in the glide.yaml file and checks if these versions need to be installed based on the version range constraints you configured.

If any packages need to be updated Glide will, just like Composer, download this package to the vendor/ folder and write the new version to the glide.lock file. In the end resulting in reproducible installations.

Glide’s composer.json

Glide uses the YAML format instead of json, but other than that the essence of the glide.yaml file is similar to the composer.json file.

Below you will see a composer.json file and a glide.yaml file to give you an idea of how similar they are.

There are three key differences:

  • The package definition in glide.yaml does not contain a version number. The package version is defined by the version control system you put your Glide-enabled package in. This can, for example, be a hash reference, revision number, or (semantic version) tag names.
  • There is no “dev” import, but “test” imports. (But you can use them in the same way.)
  • The autoload directive is not present in Glide. This is because Golang has it’s own fixed way of loading dependencies and you can’t influence them using Glide. Package loading is predictable in Golang and needs no configuration value in the dependency manager.
// composer.json
{
  "name": "rtuin/somepackage",
  "description": "This is some package description",
  "homepage": "https://github.com/rtuin/somepackage",
  "license": "MIT",
  "version": "1.0.0",
  "authors": [
    {
      "name": "Richard Tuin",
      "email": "git@rtuin.nl",
      "homepage": "http://www.rtuin.nl"
    }
  ],
  "autoload": {
    "psr-4": {"Rtuin\\Somepackage": "src/"}
  },
  "require": {
      "rtuin/someotherpackage": "^4.1",
  },
  "require-dev": {
    "rtuin/somedevdependency": "^5.3"
  }
}
# glide.yaml
package: github.com/rtuin/somepackage
homepage: https://github.com/rtuin/somepackage
license: MIT
owners:
- name: Richard Tuin
  email: git@rtuin.nl
  homepage: http://www.rtuin.nl
import:
- package: github.com/rtuin/someotherpackage
  version: ^4.1
testImport:
- package: github.com/rtuin/somedevdependency
  version: ^5.3

Conclusion

As it turns out Golang’s Glide is very similar to PHP’s Composer. Glide is not the only popular dependency manager for Golang but it is definitely the easiest to use and it is very similar to Composer.

Validate server configuration with Serverspec

The other day I had to validate if a bunch of servers was configured according to specification. Now normally I would log in to every server and inspect each detail of the configuration by hand, but in this case that would be too time-consuming. Also, if the configuration would change later I would do the same thing again. And again. You may know the drill.

Suddenly realised that I work in the automation business, I figured I might as well automate this validation process. Why not. After a quick search one tool named Serverspec came to my attention. Serverspec is a Ruby-based (RSpec) command-line tool that validates server configuration for you. You write the server specifications in RSpec files which can get messy, but are at least easier to read than the shell commands you would write as an alternative.

The big pro is that you can run the validation again and again. You can save the server specifications as plain-text files meaning you can manage them in version control easily.

There are a couple of things that were “out of my comfort zone”. Or cons, as you may call them. For example Serverspec is built with validating one server specification per run. Running the tests against multiple servers requires some extra work. Not hard to do, but documentation on this is lacking. Apart from this the documentation is alright!

Automatically installing all Ansible role dependencies

I think it was a couple of months ago now that I first started to fiddle around with Ansible. Being used to work with Puppet as a system provision tool before, Ansible seemed to make this task I disliked so bad before very easy. Almost fun I guess.

One thing that I found missing though, was a way to automatically install all the role dependencies. This still seems to be a task you have to do manually. Or anyway, I can’t find a way how to do it with the current Ansible version. So, being a programmer and all, I wrote a script for it.

The script itself is very simple:

  1. Loops through every role installed in roles/
  2. For every role, it fetches dependencies from meta/main.yml
  3. Installs the dependencies via Ansible galaxy

Personally I haven’t found a use case for other features, but if you do, please let me know.

The script source is available here: GitHub link
Available for download here: Direct download

Happy coding!

Charles is my superhero!

As a true geek I am a big fan of several superheroes. Actually, Iron Man is by far my favourite one. Or actually, the coolest part about Iron Man is J.A.R.V.I.S. if you ask me. The Just A Rather Very Intelligent System, is a clever system that assists Tony Stark in virtually everything that he considers “everyday tasks”.

Unfortonately I do not embody a superhero, but I do have several tools that assist me with my everyday tasks, and one of these tools is Charles Proxy.
Like my IDE helps me with being productive in writing, testing and troubleshooting code, Charles Web Debugging Proxy helps me with most of the things that are related to HTTP. And as a web developer, you know, that’s a lot. Here are some of the most significant contributions of Charles in my everyday work.

Communication insight

Specifically when developing APIs, a lot of communication is happening “under water”. A website does a request to an API, or a mobile device calls some webservices, and you want to know what’s going on. As with most proxies, Charles can show the HTTP-client requests. What’s connecting when, to where, and with what headers, cookies, request/response bodies etc.

Request tampering

Screen Shot 2013-07-25 at 20.34.59By setting a breakpoint on a specific request, you can modify the entire HTTP request the client is sending to the server. Change headers, cookies, request bodies, query parameters. In short, everything that you need to troubleshoot most common HTTP-related issues. Activate this in the context menu of a specific request -> Breakpoints, and the next time your client requests this URL, charles will break on the request.

Give colleagues and devices access to your dev environment

Another very convenient feature is that if you set your proxy server as a system-wide proxy on let’s say, your mobile device or colleagues’ machine, that they can see everything web-accessible on the machine you’re running Charles on.
So in practice, it means that you can check out your application that’s running on let’s say, a host-only connected virtual machine. This is something that we use a lot here in the office. Pretty neat, huh?

But there’s more…

Actually, the list above is just the tip of the iceberg of Charles’ features. There are a lot more features in the application, like: Bandwidth throttling, Request filtering, Recording/Replaying and saving sessions, DNS spoofing, SSL proxying, and there’s still more.

So, why a blog about Charles’ features while I could also visit it’s website?

Well, if you’ve read this far, I’ve got your attention. And that’s exactly what I was after. My goal with this post is to show that there’s a tool that can help you with things that can be very cumbersome without a proxy. And more! Charles is just great, and every web developer should at least know about it’s existence.

Of course, Charles is not the only Proxy program out there, and probably there are tools that have more or less the same features. If you know such a tool, could you let me know in the comments?
Oh, and Charles runs on virtually every desktop platform!

New writing tool: Getting in the zone

Usually when I am writing, I get distracted a lot. And I mean a lot. And this is one of the reasons that i don’t write as much as I want to.

However tonight I found this tool called OmmWriter. And it is specifically designed to zone yourself out from all distractions and write. Just write. It helps me a lot to do productive writing.

ommwriter-logo-main

When starting the application, which runs in full-screen mode only, it suggests that it works best with your headphones on. And this is where the zoning out begins. After the splash screen the application plays a soothing ambient song in the background. And it helps me focus on my screen.
Music normally distracts my thought from the task at hand, but this music does not. Excellent!

As soon as you start typing you hear the sound of your keyboard. A little something that the application adds. I think it works motivating to hear the ticks of the keyboard. It motivates to stick to the rhythm of typing.

Proof that this works is that last night I’ve written a 900 word blogpost in under an hour. Possibly one of the longest blogposts that I’ve ever written. Now that’s some productive writing!

If you are interested in this application, you can download the application for free at the OmmWriter website. It is available for Mac, Windows, and iPad.

See, another 200+ word blogpost done in under 10 minutes. Way to go!

Javascript minification in capistrano/webistrano

Photo: http://www.sxc.hu/photo/347930

Recently i had to automate the minification of specific javascripts. Since we don’t have a real build tool, i thought it was best to do this during deployment.
For (automatic) deployments our tool of choice is Webistrano, which is based on Capistrano, which is written in Ruby.

Capistrano has this concept of recipes, which are small scripts that you write to do specific tasks during deployment.
So, one of these tasks could be the minification of some javascript.

But for the minification and deployment of that, i was bound to some constraints:

  • We’re using the yuicompressor (java)
  • Deploy target machines do not have java installed
  • So we have to do the minification locally

After doing some initial research it was quite easy to do, and i came up with the following code:

The approach can be considered somewhat unusual, as the project’s files are first uploaded to the server. And after that the javascript is minified and uploaded seperately. This is mainly because I couldn’t find another hook within capistrano that fits my need.
Of course, you can rewrite this script to work with any other local command too.

Note/disclaimer: This script uses the copy_cache directive. I’m not a capistrano expert, but this variable might not be available to all deployment strategies. This script might not work for you if copied 1:1. But i suppose you get the idea.

Productivity ABC: A

Automation

Gears

© 2011 foxbit – sxc.hu

Automation can be very profitable when it comes to boosting productivity. Not only because you don’t have to do an action manually, but there are other productivity gains.

Eliminating the possibility of human error is one of them. Automation is especially useful for repetitive tasks. Tasks which are more often than not boring as waiting for grass to grow.
Waiting, or being bored decreases focus and causes the need for distraction. And losing focus on the task at hand is the worst thing that can happen to your productivity.

There are a lot of tools that can help you with automating different type of tasks. As a software developer i usually make small scripts for tasks i need to automate. Therefore i don’t use many automation tools. However, if you have suggestions for productivity tools, please leave a comment!
Some of my favourite automation languages: PHP & shell.

Attention span

Oh look, a butterfly!

Distraction: “Oh look, a butterfly!” – © 2012 irum – sxc.hu

Try to find out what your average attention span is, and always take a short break after that period.
It doesn’t have to be a long break. Just grab a coffee, stretch, do a small (but different) task, or anything like that. You need this to re-focus on the task. If you start on the original task again, work while your attention span lasts, and then take a break again to refocus after that.

It may sound that you will lose a lot of time because of all the breaks, but this is not true. (if your breaks are short enough, hehe).
Working longer on a task longer than your full attention lasts will result in working less efficient, and what you do is possibly more prone to errors. So taking a break every now and then will most likely boost your productivity.

Finding out how long your attention span lasts can be quite a challenge. It might be good to know that the maximum attention span lies somewhere between 3 minutes (young children) and 20 minutes (adults).
The easiest way to find your attention span is to start a stopwatch, and start working on the task you want to complete. Stop the stopwatch and remember what time is on the stopwatch. Reset and start the stopwatch and continue on the task. Do this a couple of times and you’ve found your average attention span.

For more information on attention span visit Wikipedia.

Is success the key to happiness, or the other way around?

Yesterday I was watching this TED talk from Shawn Achor and this question hasn’t left my mind since. The speaker talks about that being happy makes you work and learn more effective, and claims that therefore you can be more successful. I think this statement, or rather, fact is very valid and is worth to stop and think about for a minute.

Shawn mentions that chasing happiness by working towards successes that we want to have is a never ending one, as we constantly set new targets and raise the bar. Or, quoting Shawn “If happiness is on the opposite side of success, your brain never gets there.”

Standing still by what we have achieved so far makes one happy, and let’s you do and focus on what you like and love. Which, according do Gary Vaynerchuck (you should read his book Crush It! and The Thank You Economy) is the key to success and personal development. Which, in turn, raises your level of success and if you stick to this approach, your level of happiness.

Think about it. What is your attitude to happiness and success?

I found this all very interesting and worth thinking of. If you are reading this and are looking for more in-depth info I can recommend you to view Shawn’s presentation.

Anticipating for change with the Strategy-pattern

We developers get to deal with change nearly every day. As your project progresses, stories or requirements are changed or added. And we’ve all been to the point where our code gets brittle or unmaintainable after too many changes and/or additions.

Let’s start with a metaphor. Let’s say your keyboard is broken and you need a new one. Luckily the keyboard is connected via a USB-connector to your computer, so you can replace it with another keyboard with ease.
But what if there wasn’t a common interface (USB) to connect your keyboard to a computer, for example if it was directly connected to the computer?
Indeed, you would either get an entire new computer or you’d be soldering your new keyboard onto your computer. None of which are ideal solutions to the problem.

This metaphor applies to programming in many ways, but arguably the most important principle that comes from this example is that you should program to an interface where possible.

In a lot of cases programming to an interface encourages the interchangeability of certain parts of your application. Thus it enables you to write flexible code, and anticipate for changes in domain logic.

If you’ve ever written or worked with an online shop you might have come a cross the problem that different countries have different tax rates. Each country has their own tax rate, which of course can change independently. When implementing an online shop, this problem cries out for a flexible solution, and you need to think of a durable strategy to solve this problem.

Diagram 1 - Different implementations of TaxInterface

A good way to do this is to look for commonalities of the different calculations. It is not hard to see that every tax calculation has nothing more than an in- and output number. As long as the tax is calculated correctly, the calculation itself is not really important to the system.
These commonalities are the things you define in the interface, in this case we’ll call the method calculateTax. By programming each country-specific implementation to the interface (TaxInterface) the Order does not need to know (or worry) about the outcome of TaxInterface::calculateTax(). The only thing that it needs to do is instantiate the approperiate implementation, which is out of the scope of this post.

See Diagram 1 for a clarification.

This solution is defined as the “Strategy pattern”, and it is about anticipating on (potential) change of features. In the example it is fairly easy to add a tax implementation for another country, while you have to make only small changes to your existing code.

I hope this example demonstrated the reason behind programming to an interface and and the purpose of the Strategy-pattern in a fairly simple manner.

Why i think the web as a platform is the future

Lately i have found myself thinking about why applications built on web standards are becoming more and more common. What is it that draws us all to this amazing place called the web? And why are more and more companies investing in applications based on web standards?
Personally i am really enthusiastic about the direction the “applicationscape” is heading and i  think web applications have the potential to go a (really) long way.

But seriously, why?

I think the answer lies in how people consume information. It seems we have this (subconcious?) urge to find and use information as effective as possible. And the technology of today has brought us to the point that we want this information anywhere, and we want it NOW.

There are a lot of ways to consume information in modern (internet) times: PC’s, laptops, tablets, smartphones and what not. Each of them having their own vendors and platforms but they also have their own distinctive level of speed, mobility and have different input mechanisms. Each device is used different.
However, important to note is that all these devices serve the same purpose: to serve information.

There is an important thing that, nowadays, all of these devices have in common, and that is that they have a web browser. So all of these devices have access to the web.
That is what makes the web so interesting as a platform. The reach is enormous.
Online, offline, anywhere and any time, on any device. The world wide web is a shared platform across devices already. Regardless of operating system.

An important downside of developing applications for multiple platforms is that it is time consuming. Another downside is that, for various reasons, the different applications you end up with do not always contain the same functionality. Now i can see sometimes this is because the project is on a time constraint, but an often heard reason is that to offer users of different devices the best user experience.
Now there is a big difference between offering a different user experience based on certain context parameters and designing an different application for each platform possibly limiting users access to certain functionality of your original product.

Now this is where i think the web as a platform has it’s potential and can offer more for both end-users and developers. Of course, web applications do not offer the same functionality as native applications at this time, but i think this is a matter of time. The open web standards have become really mature at present time, and i think it is time that we start embracing the web as a platform.

And what about you? I would love to hear your thoughts about the web as a platform in the comments and get some discussion going around this…

On September 17th i am speaking at the PFcongres (in dutch) about the web as a platform and what technologies you can use today to develop for it as such. If you are interested in this topic, or even if you are not (the other speakers on schedule cover amazing subjects!!) PFcongres is the place to be in Utrecht this saturday!