Thursday, January 16, 2014

Handling JSON in bash using underscore

Now on days, software components expose some sort of API using a REST interface. In addition, the typical response comes in JSON format. While this very convenient for a browser to interpret, using JSON in a bash script is not as simple. I came across this project on github called underscore and I must admit I liked it. I highly recommend reading it's short README file. I figured I'd write some about my issues installing it.

Underscore is a node.js solution (no surprise there). I have been a fan of node.js for sometime now, in particular for how you can get things done in my bash scripts. Installing underscore on Linux was not a problem at all (I tried my install on an Ubuntu VM). Once you have all the dependencies installed, there was no problem.

Installing it on a MAC OSX was a different issue. I detected a problem with the msgpack feature. In MAC OSX, brew did most of the work to install node, but doing something:

    npm install -g underscore-cli

I got the following:

> msgpack@0.2.1 install /usr/local/lib/node_modules/underscore-cli/node_modules/msgpack
> node-gyp rebuild

gyp http GET
gyp http 200
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

gyp: Error 1 running xcodebuild
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/Cellar/node/0.10.24/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:337:16)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Darwin 13.0.2
gyp ERR! command "node" "/usr/local/Cellar/node/0.10.24/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/local/lib/node_modules/underscore-cli/node_modules/msgpack
gyp ERR! node -v v0.10.24
gyp ERR! node-gyp -v v0.12.1
gyp ERR! not ok
npm WARN optional dep failed, continuing msgpack@0.2.1
/usr/local/bin/underscore -> /usr/local/lib/node_modules/underscore-cli/bin/underscore
underscore-cli@0.2.17 /usr/local/lib/node_modules/underscore-cli
├── commander@2.1.0
├── underscore@1.5.2
├── coffee-script@1.6.3
├── underscore.string@2.3.3
└── JSONSelect@0.4.0

My fix: Install XCode. Once installed (free 2GB download), this problem disappeared.

No comments: