blew
Version:
A terminal tool to http://blew.io :3
246 lines (207 loc) • 5.36 kB
JavaScript
var fs = require('fs');
var path = require('path');
var http = require('http');
var request = require('request');
var program = require('commander');
var package = require('../package.json');
var config = require('./config.json');
/**
* Check if OS is Supported
*
*/
function _isSupportedOS() {
var isWindows = /^win/.test(process.platform);
var isMac = /^darwin/.test(process.platform);
var isLinux = /^linux/.test(process.platform);
return (isLinux || isMac);
}
/**
* Return the User Path /home/user for linux, ...
*
*/
function _getUserPath() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
/**
* Get the App Path, usually '~/.blew'
*
*/
function _getAppPath() {
return _getUserPath() + '/.' + package.name;
}
/**
* Get the Auth File Path, usually '~/.blew/auth'
*
*/
function _getAuthFilePath() {
return _getUserPath() + '/.' + package.name + '/key.io';
}
/**
* Check if App folder exists, and create a new one if need to.
*
*/
function _checkAppFolderStatus(){
var exists = fs.existsSync(_getAppPath());
if (!exists) {
try {
fs.mkdirSync(_getAppPath());
}
catch (e) {
console.log(e);
return false;
}
}
return true;
}
/**
* Check the if AuthFile is present and if is valid
*
*/
function _checkAuthFileStatus() {
var exists = fs.existsSync(_getAuthFilePath());
if (!exists) {
try {
_setAuthKey(' ');
return true;
}
catch (e) {
return false;
}
}
return true;
}
// ** Commands **/
/**
* Invalid Command
*
*/
function invalidCmd(env) {
console.log("Please enter a valid command. If you need help type '%s -h'", package.name);
}
/**
* Auth Command
*
*/
function authCmd(env){
var key = env.key ;
request.post(
{ method: 'GET',
url: config.api.paths.check,
form: { 'key': key }
},
function (err, res, resBody) {
resBody = JSON.parse(resBody);
if (resBody.isValid == true) {
_setAuthKey(key);
console.error('Your key was successfully authenticated!');
return true;
}
else {
if (resBody.error == true) {
return console.error('Error: ', resBody.message);
}
return console.error('Error: ', err);
}
});
}
function _setAuthKey(key) {
return fs.writeFileSync(_getAuthFilePath(), key || null);
}
function _getAuthKey() {
return fs.readFileSync(_getAuthFilePath(), "utf8");
}
function newCmd(env) {
if (env.file != null) {
var filePath = path.resolve(env.file);
fs.exists(filePath, function(exists) {
if (exists) {
fs.stat(filePath, function(error, stats) {
if (stats.isFile()) {
var data = fs.readFileSync(filePath, "utf8");
request.post(
{ method: 'POST',
url: config.api.paths.newPaste,
form:
{
'key': _getAuthKey(),
'name': (env.name === undefined) ? path.basename(env.file) : env.name,
'language': env.language || null,
'private': (env.private === true) ? 'true' : 'false',
'expiresAt': env.expiresAt || null,
'content': data
}
},
function (err, res, resBody) {
resBody = JSON.parse(resBody);
if (err) {
return console.error('Error :', err);
}
else if (resBody.error == true) {
return console.error('Error: ', resBody.message);
}
else {
return console.log("Uploaded successfully! \n Access Link: %s", resBody.link);
}
});
}
else {
return console.log("'" + env.file + "' it is not a valid file! \n");
}
});
}
else {
return console.log("'" + env.file + "' does not exists! \n");
}
});
}
else {
return console.log('You must specify a file! \n');
}
}
function _go() {
program.version(package.version)
program.command('auth')
.alias('a')
.description("Setup and authenticated the default user.")
.option("-k, --key <key>", "Blew.io authentication key.")
.action(function(env){
authCmd(env);
});
program.command('new')
.alias('n')
.description("Send a new file to the server.")
.option("-f, --file <file>", "The file path.")
.option("-n, --name <name>", "The name for your paste, default is the file name.")
.option("-l, --language <language>", "Your code language. Like 'C', 'C++', 'JS'")
.option("-e, --expiresAt <expiresAt>", "Expiration value, ... '2w, 1d, 3h, 3h20m ...'")
.option("-P, --private", "Your file will be private.")
.action(function(env){
newCmd(env);
});
program.command("*")
.action(function(env){
invalidCmd();
});
program.parse(process.argv);
var NO_COMMAND_SPECIFIED = program.args.length === 0;
if (NO_COMMAND_SPECIFIED) {
invalidCmd();
}
}
function main() {
if (!_isSupportedOS()) {
console.log("Sorry, %s doesn't support your operating system.", package.name);
return 0;
}
if (!_checkAppFolderStatus()) {
console.log("Impossible to create the folder '~/." + package.name + "', please check the users folder permissions and try again.");
return 0;
}
if (!_checkAuthFileStatus()) {
console.log("Impossible to create the authentication file, please check the users folder permissions and try again.");
return 0;
}
return _go();
}
main();