{"size": 2389, "ext": "js", "max_stars_repo_path": "redis-install.js", "max_stars_repo_name": "taoyuan/linux-setups", "max_stars_repo_stars_event_min_datetime": "2020-07-23T19:20:04.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-23T19:20:04.000Z", "max_issues_repo_path": "redis-install.js", "max_issues_repo_name": "taoyuan/linux-setups", "max_issues_repo_issues_event_min_datetime": "2019-09-08T10:15:22.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-08T10:15:22.000Z", "max_forks_repo_path": "redis-install.js", "max_forks_repo_name": "taoyuan/linux-setups", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 24.8854166667, "max_line_length": 87, "alphanum_fraction": 0.4914190038}
#! /usr/bin/env node

"use strict";

var sh = require('shelljs');
var async = require('async');
var prompt = require('prompt');
var prop = require('properties-parser');

var opts = { silent: true },
    cmds, ver;

if (sh.which('redis-server')) {
    ver = sh.exec('redis-server -v', {silent: true}).output;
    sh.echo(ver, 'has been installed.');
}

// TODO prompt for bind hosts

async.series([install, setup], function () {
});

function install(callback) {
    if (ver) {
        prompt.start();
        prompt.get({ properties: {
                continue: {
                    description: 'Continue to Install? [Y/n]'.yellow,
                    default: 'n',
                    required: true
                }
            }},
            function (err, result) {
                if (result.continue == 'y' || result.continue == 'Y') {
                    doInstall(opts);
                }
                callback && callback(err);
            }
        );
    } else {
        doInstall(opts);
        callback && callback(null);
    }
}

function setup(callback) {
    if (ver) {
        prompt.start();
        prompt.get({ properties: {
                continue: {
                    description: 'Continue to Setup? [Y/n]'.yellow,
                    default: 'n',
                    required: true
                }
            }},
            function (err, result) {
                if (result.continue == 'y' || result.continue == 'Y') {
                    doSetup(opts);
                }
                callback && callback(err);
            }
        );
    } else {
        doSetup(opts);
        callback && callback(null);
    }
}


// install redis
function doInstall(opts) {
    sh.echo('Install redis');
    cmds = [
        'sudo apt-get -yq update',
        'sudo apt-get -yq install python-software-properties',
        'sudo add-apt-repository -y ppa:rwky/redis',
        'sudo apt-get -yq update',
        'sudo apt-get -yq install redis-server'
    ];

    cmds.forEach(function (cmd) {
        sh.echo(cmd);
        sh.exec(cmd, opts);
    });
}

// setup redis
function doSetup(opts) {
    sh.echo('Setup redis');
    var editor = prop.createEditor('/etc/redis/conf.d/local.conf', { separator: ' ' });
    editor.set('bind', '127.0.0.1');
    editor.save();
    sh.echo('Restart redis server');
    sh.exec('sudo service redis-server restart');
}
