'use strict';

var _ = require('lodash');
var fs = require('fs');
var path = require('path');

function arrayToQueryString(args) {
  var searchString = '?q=' + args._.join('+');
  if(args.user !== 'undefined') {
    searchString = searchString.concat('+user:' + args.user);
  }
  return searchString;
}

function performQuery(config, request, args) {
  var url = config.API_URI + config.SEARCH_URI + arrayToQueryString(args);
  request.get(
    {
      url: url,
      headers: {
        'User-Agent': 'request',
      },
    }, function(error, response, body) {
      var result = JSON.parse(body);
      /* jshint camelcase: false */
      console.log('Found %s repositories that match your search query',
                  result.total_count || 0);
      _.forEach(result.items, function(repo) {
        console.log(repo.full_name);
      });
      /* jshint camelcase: true */
    });
}

function help() {
  console.log(fs.readFileSync(
    path.resolve(__dirname, '../templates/help-search.txt'), 'utf8'));
}

module.exports = function(config, request, args) {
  if (args.help) {
    help();
  }  else {
    performQuery(config, request, args);
  } 
};

@@ -175,16 +175,17 @@
 ');%0A  if
+ 
 (args.us
@@ -1147,13 +1147,12 @@
 gs);%0A  %7D
- 
 %0A%7D;%0A

Fix code style bugs.