#!/usr/bin/env node

var portastic = require('../'); var commander = require('commander'); var package = require('../package.json'); commander.version(package.version);

// Test if a port is closed commander

.command('test <port>')
.alias('t')
.description('Test if a port is closed or open')
.action(function(port) {
  portastic.test(port)
    .then(function(result) {
      console.log('Port %s is %s', port, result ? 'open' : 'closed');
    });
});

// Find available ports commander

.command('find <min> <max>')
.alias('f')
.description('Find ports that are available to use')
.option('-r, --retrieve', 'How many ports to retrieve')
.action(function(min, max, options) {
  portastic.find({
    min: min,
    max: max,
    retrieve: options.retrieve
  })
    .then(function(ports) {
      console.log('Ports available to use: %s', ports.join(', '));
    });
});

// Filter a list of ports commander

.command('filter <ports...>')
.alias('i')
.description('Find ports that are open whithin a list of ports')
.action(function(ports) {
  portastic.filter(ports)
    .then(function() {
      console.log('Ports available to use: %s', ports.join(', '));
    });
});

// Monitor ports commander

.command('monitor <ports...>')
.alias('m')
.description('Monitor a list of ports and logs to the terminal when port state had changed')
.action(function(ports) {
  var monitor = new portastic.Monitor(ports);
  monitor.on('open', function(port) {
    console.log('Port %s is open', port);
  });

  monitor.on('close', function(port) {
    console.log('Port %s is closed', port);
  });
});

commander.parse(process.argv);