#!/usr/bin/env node

'use strict';

const http = require('http');
const program = require('commander');
const log = console.log;

const getStats = function () {

    const options = {
        host: 'onhub.here',
        path: '/api/v1/status'
    };

    const callback = function (response) {
        var str = '';

        response.on('data', function (chunk) {
            str += chunk;
        });

        response.on('end', function () {
            const stats = JSON.parse(str);

            const getFormattedTime = function (seconds) {
                var minutes = Math.floor(seconds / 60);
                seconds = seconds % 60;
                var hours = Math.floor(minutes / 60);
                minutes = minutes % 60;
                var days = Math.floor(hours / 24);
                hours = hours % 24;
                return days + ' days, ' +
                    hours + ' hours, ' +
                    minutes + ' minutes, ' +
                    seconds + ' seconds';
            }

            if (program.swversion) { log('Software Version:', stats.software.softwareVersion); }
            if (program.uptime) { log('Total Uptime:', getFormattedTime(stats.system.uptime)); }
            if (program.gip) { log('Gateway IP:', stats.wan.gatewayIpAddress); }
            if (program.ip) { log('IP Address:', stats.wan.localIpAddress); }
            if (program.dns) { log('DNS Servers:', stats.wan.nameServers); }
            if (program.all) {
                log('Software Version:', stats.software.softwareVersion);
                log('Total Uptime:', getFormattedTime(stats.system.uptime));
                log('Gateway IP:', stats.wan.gatewayIpAddress);
                log('IP Address:', stats.wan.localIpAddress);
                log('DNS Servers:', stats.wan.nameServers);
            }
        });
    }

    const request = http.request(options, callback);

    request.on('error', function () {
        console.log('Your Google Wifi is offline.');
    });

    request.end();
}

program
    .option('-v, --swversion', 'Software Version')
    .option('-u, --uptime', 'Total Uptime')
    .option('-g, --gip', 'Gateway IP')
    .option('-i, --ip', 'IP Address')
    .option('-d, --dns', 'DNS Servers')
    .option('-a, --all', 'Display all stats')
    .parse(process.argv);

// If no option passed, show help
if (!process.argv.slice(2).length) {
    program.help();
}

getStats();
@@ -93,24 +93,104 @@
 ommander');%0A
+const chalk = require('chalk');%0Aconst blue = chalk.blue;%0Aconst red = chalk.red;%0A
 const log = 
@@ -1118,24 +1118,29 @@
 sion) %7B log(
+blue(
 'Software Ve
@@ -1179,16 +1179,17 @@
 Version)
+)
 ; %7D%0A    
@@ -1218,24 +1218,29 @@
 time) %7B log(
+blue(
 'Total Uptim
@@ -1282,16 +1282,17 @@
 uptime))
+)
 ; %7D%0A    
@@ -1318,24 +1318,29 @@
 .gip) %7B log(
+blue(
 'Gateway IP:
@@ -1361,32 +1361,33 @@
 atewayIpAddress)
+)
 ; %7D%0A            
@@ -1404,24 +1404,29 @@
 m.ip) %7B log(
+blue(
 'IP Address:
@@ -1453,16 +1453,17 @@
 Address)
+)
 ; %7D%0A    
@@ -1489,24 +1489,29 @@
 .dns) %7B log(
+blue(
 'DNS Servers
@@ -1536,16 +1536,17 @@
 Servers)
+)
 ; %7D%0A    
@@ -1592,16 +1592,21 @@
     log(
+blue(
 'Softwar
@@ -1649,16 +1649,17 @@
 Version)
+)
 ;%0A      
@@ -1672,16 +1672,21 @@
     log(
+blue(
 'Total U
@@ -1732,16 +1732,17 @@
 uptime))
+)
 ;%0A      
@@ -1755,16 +1755,21 @@
     log(
+blue(
 'Gateway
@@ -1794,32 +1794,33 @@
 atewayIpAddress)
+)
 ;%0A              
@@ -1825,16 +1825,21 @@
     log(
+blue(
 'IP Addr
@@ -1870,16 +1870,17 @@
 Address)
+)
 ;%0A      
@@ -1893,16 +1893,21 @@
     log(
+blue(
 'DNS Ser
@@ -1936,16 +1936,17 @@
 Servers)
+)
 ;%0A      
@@ -2072,27 +2072,23 @@
         
-console.log
+log(red
 ('Your G
@@ -2111,16 +2111,17 @@
 fline.')
+)
 ;%0A    %7D)

Use chalk colors