Introduction

I was looking for some tools for writing CLI. Then i found an awesome way of writing cli in Java Script and nodejs as npm module. And this is easy as it writing a simple js app in node app. In my previous post i wrote my Bash Profile, this will definitely be a good addition in my Bash Profile.

I created Git Cli module, that you can use to search git repositories, based on keywords, owner and language of repository. Let’s jump into the code and have a look how it works.

packge.json

{
  "name": "gitcli",
  "version": "1.0.0",
  "description": "Command Line tool for searching git repositories.",
  "author": "Muhammad Arslan",
  "engines": {
    "node": ">=0.10"
  },
  "dependencies": {
    "commander": "~2.2.0",
    "request": "~2.36.0",
    "chalk": "~0.4.0"
  },
  "bin": {
    "gitcli": "gitcli.js"
  }
}

gitcli.js

#!/usr/bin/env node

//For command line interface commands and arguments.
var program = require('commander');
//For http requests.
var request = require('request');
//For String styling and colors.
var chalk = require('chalk');
//Include json file.
var pJson = require('./package.json');

program
	.version(pJson.version)
	.usage('[options] <keywords>')
	.option('-o, --owner [name]', 'Filter by the repositories owner')
	.option('-l, --language [language]', 'Filter by the repositories language')
	.option('-f, --full', 'Full output without any styling, in json')
	.parse(process.argv);

if(!program.args.length) {
	program.help();

} else {
    //Arguments as keywords.
	var keywords = program.args;
	//Github api end point for searching keyworkds.
    var url = 'https://api.github.com/search/repositories?sort=stars&order=desc&q='+keywords;

    //Add owner if it exists.
	if(program.owner) {
		url = url + '+user:' + program.owner;
	}

    //Add language if it exists.
	if(program.language) {
		url = url + '+language:' + program.language;
	}

    //Http request to github with url that we already formed.
	request({
		method: 'GET',
		headers: {
			'User-Agent': 'muhammadarslan'
		},
		url: url
	}, function(error, response, body) {

		if (!error && response.statusCode == 200) {
			var body = JSON.parse(body);
			if(program.full) {
                //Print plain json if you don't want to format.
				console.log(body);
			} else {
                //Loop through all the results and print it on console.
				for(var i = 0; i < body.items.length; i++) {
					console.log(chalk.cyan.bold('Name: ' + body.items[i].name));
					console.log(chalk.magenta.bold('Owner: ' + body.items[i].owner.login));
					console.log(chalk.grey('Desc: ' + body.items[i].description + 'n'));
					console.log(chalk.grey('Clone url: ' + body.items[i].clone_url + 'n'));
				}
				process.exit(0);
			}
			process.exit(0);
		} else if (error) {
			console.log(chalk.red('Error: ' + error));
			process.exit(1);
		}
	});
}

Run locally

You just need to have node installed in your machine.

Download the code and enter it into folder. And execute following command.

npm install

This will download all the dependencies specified in package.json. i.e. commander, request and chalk.

And if you want to install this locally to use gitcli command, execute following command.

Note you need make gitcli.js file executable. you need to execute: chmod 755 gitcli.js
npm install -g

After this you can able to execute command from your shell. Following command will print search result of git-cli repo on github.

gitcli git-cli -o muhammadarslan

Install from npm-registery

This is also published on npm-registry. You can also install it by executing following command.

npm install gitcli

Enjoy!