#!/usr/bin/env node
var needle = require('./../lib/needle');

var exit = function(code, str) {
  console.log(str) || process.exit(code);
}

if (process.argv[2] == '-v' || process.argv[2] == '--version')
  exit(0, needle.version);
else if (process.argv[3] == null)
  exit(1, 'Usage: needle [get|head|post|put|delete] url [query]');

var method   = process.argv[2].toLowerCase(),
    url      = process.argv[3],
    options  = { compressed: true, parse: true, follow: true, timeout: 5000 };

var callback = function(err, resp) {
  if (err) return exit(1, "Error: " + err.message);

  if (process.argv.indexOf('-i') != -1)
    console.log(resp.headers) || console.log('');

  console.log(resp.body);
};

if (method == 'post' || method == 'put')
  needle[method](url, process.argv[4], options, callback);
else
  needle[method](url, options, callback);
