Skip to content

Commit

Permalink
#620: Refactor update.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Jan 17, 2018
1 parent eae4891 commit 49692c9
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 135 deletions.
93 changes: 55 additions & 38 deletions bin/lando.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,50 @@

// Modules
var _ = require('lodash');
var bootstrap = require('./../lib/bootstrap.js');
var cli = require('./../lib/cli');
var Log = require('./../lib/logger');
var Metrics = require('./../lib/metrics');
var os = require('os');
var path = require('path');

// Grab the bootstrap func
var bootstrap = require('./../lib/bootstrap.js');
var Promise = require('./../lib/promise');
var sudoBlock = require('sudo-block');
var userConfRoot = path.join(os.homedir(), '.lando');

// Allow envvars to override a few core things
var userConfRoot = path.join(os.homedir(), '.lando');
var ENVPREFIX = process.env.LANDO_CORE_ENVPREFIX || 'LANDO_';
var LOGLEVELCONSOLE = process.env.LANDO_CORE_LOGLEVELCONSOLE || 'warn';
var USERCONFROOT = process.env.LANDO_CORE_USERCONFROOT || userConfRoot;

// If we have CLI verbosity args let's use those instead
if (cli.largv.verbose) {
LOGLEVELCONSOLE = cli.largv.verbose + 1;
}
if (cli.largv.verbose) { LOGLEVELCONSOLE = cli.largv.verbose + 1; }

// Initialize Lando with some start up config
bootstrap({
// Define the start up options
var options = {
configSources: [path.join(USERCONFROOT, 'config.yml')],
envPrefix: ENVPREFIX,
logLevelConsole: LOGLEVELCONSOLE,
logDir: path.join(USERCONFROOT, 'logs'),
mode: 'cli',
pluginDirs: [USERCONFROOT],
userConfRoot: USERCONFROOT
})
};

/*
* Initializes the CLI.
*
* This will either print the CLI usage to the console or route the command and
* options given by the user to the correct place.
*/
.then(function(lando) {
// Kick off our bootstrap
bootstrap(options)

// Yargonaut must come before yargs
var sudoBlock = require('sudo-block');
var yargonaut = require('yargonaut');
yargonaut.style('green').errorsStyle('red');

// Get yargs
var yargs = require('yargs');
// Initialize the CLI
.then(function(lando) {

// Log
lando.log.info('Initializing cli');

// Get global tasks
var tasks = _.sortBy(lando.tasks.tasks, 'name');

// Get cmd
// Get CLI things
var cmd = '$0';
var tasks = _.sortBy(lando.tasks.tasks, 'name');
var yargonaut = require('yargonaut');
yargonaut.style('green').errorsStyle('red');
var yargs = require('yargs');

// If we are packaged lets get something else for the cmd path
if (_.has(process, 'pkg')) {
Expand Down Expand Up @@ -96,15 +85,36 @@ bootstrap({
sudoBlock(lando.node.chalk.red('Lando should never be run as root!'));
})

// Print our result
// Check for updates and inform user if we have some
.then(function() {

// Create epilogue for our global options
var epilogue = [
lando.node.chalk.green('Global Options:\n'),
' --help, -h Show help\n',
' --verbose, -v, -vv, -vvv, -vvvv Change verbosity of output'
];
// Determine whether we need to refresh
return Promise.resolve(lando.updates.fetch(lando.cache.get('updates')))

// Fetch and cache if needed
.then(function(fetch) {
if (fetch) {
lando.log.verbose('Checking for updates...');
return lando.updates.refresh(lando.config.version)
.then(function(latest) {
lando.cache.set('updates', latest, {persist: true});
});
}
})

// Determin whether we should print a message or not
.then(function() {
var current = lando.config.version;
var latest = lando.cache.get('updates');
if (lando.updates.updateAvailable(current, latest.version)) {
console.log(lando.cli.updateMessage(latest.url));
}
});

})

// Print the CLI
.then(function() {

// Loop through the tasks and add them to the CLI
_.forEach(tasks, function(task) {
Expand All @@ -118,6 +128,13 @@ bootstrap({
process.exit(0);
}

// Create epilogue for our global options
var epilogue = [
lando.node.chalk.green('Global Options:\n'),
' --help, -h Show help\n',
' --verbose, -v, -vv, -vvv, -vvvv Change verbosity of output'
];

// Finish up the yargs
var argv = yargs
.strict(true)
Expand All @@ -126,15 +143,15 @@ bootstrap({
.wrap(yargs.terminalWidth() * 0.75)
.argv;

// Log the CLI
// Log the CLI args
lando.log.debug('CLI args', argv);

});

})

// Handle uncaught errors
// @TODO: Replace the error.js to include some of the below
// @TODO: Replace the error.js to include some (all?) of the below
.catch(function(error) {

// Log and report our errors
Expand All @@ -149,7 +166,7 @@ bootstrap({
data: {
//mode: 'cli',
//devMode: false,
version: 'pirog5000',
version: 'refactoring',
//os: {
// type: os.type(),
// platform: os.platform(),
Expand Down
30 changes: 30 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,36 @@ exports.tunnelHeader = function() {

};

/**
* Returns a mesage indicating the availability of an update
*
* @since 3.0.0
* @param {String} url - The URL with the link to the update
* @returns {String} An update message we can print to the CLI
* @example
*
* // Print the header to the console
* console.log(lando.cli.tunnelHeader());
*/
exports.updateMessage = function(url) {

// Collect the lines
var lines = [];

// Paint a picture
lines.push('');
lines.push(chalk.yellow('There is an update available!!!'));
lines.push(chalk.yellow('Install it to get the latest and greatest'));
lines.push('');
lines.push('Updating helps us provide the best support.');
lines.push(chalk.green(url));
lines.push('');

// Return
return lines.join(os.EOL);

};

/**
* Utility function to help construct CLI displayable tables
*
Expand Down
2 changes: 1 addition & 1 deletion lib/lando.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module.exports = _.memoize(function(config) {
* @memberof lando
* @see {@link update.md}
*/
//update: require('./update'),
updates: require('./updates'),

/**
* The user module.
Expand Down
96 changes: 0 additions & 96 deletions lib/update.js

This file was deleted.

82 changes: 82 additions & 0 deletions lib/updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Contains update helpers.
*
* @since 3.0.0
* @fires pre-bootstrap
* @module update
*/

'use strict';

// Modules
var _ = require('./node')._;
var GitHubApi = require('github');
var Promise = require('./promise');
var semver = require('./node').semver;

/**
* Compares two versions
*
* @since 3.0.0
*/
exports.updateAvailable = function(version1, version2) {
return semver.lt(version1, version2);
};

/**
* Determines whether we need to fetch updatest or not
*
* @since 3.0.0
*/
exports.fetch = function(data) {

// Return true immediately if update is undefined
if (!data) {
return true;
}

// Else return based on the expiration
return !(data.expires >= Math.floor(Date.now()));

};

/**
* Get latest version info from github
*
* @since 3.0.0
*/
exports.refresh = function(version) {

// GitHub object
var github = new GitHubApi({Promise: Promise});

// GitHub repo config
var landoRepoConfig = {
owner: 'lando',
repo: 'lando',
page: 1,
'per_page': 1
};

// This i promise you
return github.repos.getReleases(landoRepoConfig)

// Extract and return the metadata
.then(function(data) {
return {
version: _.trimStart(_.get(data, 'data.[0].tag_name', version), 'v'),
url: _.get(data, 'data.[0].html_url', ''),
expires: Math.floor(Date.now()) + 3600000
};
})

// Dont let an error here kill things
.catch(function() {
return {
version: _.trimStart(version, 'v'),
url: '',
expires: Math.floor(Date.now()) + 3600000
};
});

};

0 comments on commit 49692c9

Please sign in to comment.