Skip to content

Commit

Permalink
feature(cloudcmd) add --no-config-dialog: add ability to hide config …
Browse files Browse the repository at this point in the history
…dialog (#65)
  • Loading branch information
coderaiser committed Nov 9, 2016
1 parent 0f79fc3 commit b4c5b5a
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 32 deletions.
2 changes: 2 additions & 0 deletions HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ Cloud Commander supports command line parameters:
| `--progress` | show progress of file operations
| `--open` | open web browser when server started
| `--one-panel-mode` | set one panel mode
`--config-dialog` | enable config dialog
| `--no-server` | do not start server
| `--no-auth` | disable authorization
| `--no-online` | load scripts from local server
| `--no-open` | do not open web browser when server started
| `--no-minify` | disable minification
| `--no-progress` | do not show progress of file operations
| `--no-one-panel-mode` | unset one panel mode
| `--no-config-dialog` | disable config dialog

If no parameters given Cloud Commander reads information from `~/.cloudcmd.json` and use
port from it (`8000` default). if port variables `PORT` or `VCAP_APP_PORT` isn't exist.
Expand Down
10 changes: 10 additions & 0 deletions bin/cloudcmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var Info = require('../package'),
'open',
'minify',
'progress',
'config-dialog',
'one-panel-mode'
],
default: {
Expand All @@ -47,6 +48,7 @@ var Info = require('../package'),
prefix : config('prefix') || '',
progress : config('progress'),

'config-dialog': defaultTrue(config('configDialog')),
'one-panel-mode': config('onePanelMode'),
},
alias: {
Expand Down Expand Up @@ -85,6 +87,7 @@ if (args.version) {
config('prefix', args.prefix);
config('root', args.root);
config('onePanelMode', args['one-panel-mode']);
config('configDialog', args['config-dialog']);

readConfig(args.config);

Expand All @@ -107,6 +110,13 @@ if (args.version) {
});
}

function defaultTrue(value) {
if (typeof value === 'undefined')
return true;

return value;
}

function validateRoot(root) {
var validate = require('../lib/server/validate');
validate.root(root, console.log);
Expand Down
3 changes: 2 additions & 1 deletion json/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"root": "/",
"prefix": "",
"progress": true,
"onePanelMode": false
"onePanelMode": false,
"configDialog": true
}
4 changes: 3 additions & 1 deletion json/help.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"--minify ": "enable minification",
"--progress ": "show progress of file operations",
"--one-panel-mode ": "set one panel mode",
"--config-dialog ": "enable config dialog",
"--open ": "open web browser when server started",
"--no-server ": "do not start server",
"--no-auth ": "disable authorization",
"--no-online ": "load scripts from local server",
"--no-open ": "do not open web browser when server started",
"--no-minify ": "disable minification",
"--no-progress ": "do not show progress of file operations",
"--no-one-panel-mode ": "unset one panel mode"
"--no-one-panel-mode ": "unset one panel mode",
"--no-config-dialog ": "disable config dialog"
}
11 changes: 10 additions & 1 deletion lib/client/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,16 @@ var CloudCmd, Util, DOM, io;
}
}

init();
DOM.Files.get('config', function(error, config) {
if (error)
return Dialog.alert(TITLE, error);

if (!config.configDialog)
return;

init();
});
}

})(CloudCmd, Util, DOM);

22 changes: 18 additions & 4 deletions lib/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ module.exports = manage;
module.exports.save = save;
module.exports.middle = middle;
module.exports.listen = function(socket, authCheck) {
if (!socket)
throw Error('socket could not be empty!');
check(socket, authCheck);

if (authCheck && typeof authCheck !== 'function')
throw Error('authCheck should be function!');
if (!manage('configDialog'))
return middle;

listen(socket, authCheck);

Expand Down Expand Up @@ -115,6 +114,8 @@ function connection(socket) {
}

function middle(req, res, next) {
var noConfigDialog = !manage('configDialog');

if (req.url !== apiURL + '/config') {
next();
} else {
Expand All @@ -124,6 +125,11 @@ function middle(req, res, next) {
break;

case 'PATCH':
if (noConfigDialog)
return res
.status(404)
.send('Config is disabled');

patch(req, res, next);
break;

Expand Down Expand Up @@ -190,3 +196,11 @@ function cryptoPass(json) {
json.password = criton(json.password, algo);
}

function check(socket, authCheck) {
if (!socket)
throw Error('socket could not be empty!');

if (authCheck && typeof authCheck !== 'function')
throw Error('authCheck should be function!');
}

35 changes: 22 additions & 13 deletions lib/server/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ var DIR = __dirname + '/../../',
}).join(':');

module.exports = function(req, res, next) {
check(req, res, next);

readFiles(function() {
route(req, res, next);
});
Expand All @@ -62,6 +64,7 @@ function indexProcessing(options) {
right = '',
keysPanel = '<div id="js-keyspanel" class="{{ className }}',
isOnePanel = config('onePanelMode'),
isConfig = config('configDialog'),
data = options.data,
panel = options.panel;

Expand All @@ -82,6 +85,10 @@ function indexProcessing(options) {
.replace('icon-move', 'icon-move none')
.replace('icon-copy', 'icon-copy none');

if (!isConfig)
data = data
.replace('icon-config', 'icon-config none');

left = rendy(Template.panel, {
side : 'left',
content : panel,
Expand All @@ -96,10 +103,10 @@ function indexProcessing(options) {
});

data = rendy(data, {
title : CloudFunc.getTitle(),
fm : left + right,
prefix : prefix(),
css : CSS_URL
title: CloudFunc.getTitle(),
fm: left + right,
prefix: prefix(),
css: CSS_URL
});

return data;
Expand Down Expand Up @@ -148,15 +155,6 @@ function readFiles(callback) {
function route(request, response, callback) {
var name, p, isAuth, isFS, fullPath;

if (!request)
throw Error('request could not be empty!');

if (!response)
throw Error('response could not be empty!');

if (typeof callback !== 'function')
throw Error('callback should be function!');

name = ponse.getPathName(request);

isAuth = RegExp('^(/auth|/auth/github)$').test(name);
Expand Down Expand Up @@ -233,3 +231,14 @@ function buildIndex(json, callback) {
minify(PATH_INDEX, 'name', callback);
});
}

function check(req, res, next) {
if (!req)
throw Error('req could not be empty!');

if (!res)
throw Error('res could not be empty!');

if (typeof next !== 'function')
throw Error('next should be function!');
}
2 changes: 2 additions & 0 deletions man/cloudcmd.1
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ programs in browser from any computer, mobile or tablet device.
--progress show progress of file operations
--open open web browser when server started
--one-panel-mode set one panel mode
--config-dialog enable config dialog
--no-auth disable authorization
--no-server do not start server
--no-online load scripts from local server
--no-open do not open web browser when server started
--no-minify disable minification
--no-progress do not show progress of file operations
--no-one-panel-mode unset one panel mode
--no-config-dialog disable config dialog

.SH RESOURCES AND DOCUMENTATION

Expand Down
13 changes: 9 additions & 4 deletions test/before.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const writejson = require('writejson');
const readjson = require('readjson');

const cloudcmd = require('..');
const {assign} = Object;

const pathConfig = os.homedir() + '/.cloudcmd.json';
const currentConfig = readjson.sync.try(pathConfig);
Expand All @@ -23,14 +24,18 @@ module.exports = (config, fn = config) => {
};

app.use(cloudcmd({
config: {
auth: false,
root: __dirname
}
config: assign(defaultConfig(), config)
}));

server.listen(() => {
fn(server.address().port, after);
});
};

function defaultConfig() {
return {
auth: false,
root: __dirname
};
}

61 changes: 53 additions & 8 deletions test/rest/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,39 @@ test('cloudcmd: rest: config: get', (t) => {
});
});

test('cloudcmd: rest: config: put', (t) => {
before((port, after) => {
patch(`http://localhost:${port}/api/v1/config`, {
json: {
auth: false
}
})
test('cloudcmd: rest: config: patch', (t) => {
const configDialog = true;

before({configDialog}, (port, after) => {
const json = {
auth: false,
};

patch(`http://localhost:${port}/api/v1/config`, json)
.then(warp(_pullout, 'string'))
.then((result) => {
t.equal(result, 'config: ok("auth")', 'should patch config');
t.end();
after();
})
.catch((error) => {
console.log(error);
});
});
});

test('cloudcmd: rest: config: patch: no configDialog', (t) => {
const configDialog = false;

before({configDialog}, (port, after) => {
const json = {
ip: null
};

patch(`http://localhost:${port}/api/v1/config`, json)
.then(warp(_pullout, 'string'))
.then((result) => {
t.equal(result, 'config: ok("json")', 'should patch config');
t.equal(result, 'Config is disabled', 'should return error');
t.end();
after();
})
Expand All @@ -54,3 +77,25 @@ test('cloudcmd: rest: config: put', (t) => {
});
});

test('cloudcmd: rest: config: patch: no configDialog: statusCode', (t) => {
const configDialog = false;

before({configDialog}, (port, after) => {
const json = {
ip: null
};

patch(`http://localhost:${port}/api/v1/config`, json)
.then((result) => {
result.on('response', (response) => {
t.equal(response.statusCode, 404);
t.end();
after();
});
})
.catch((error) => {
console.log(error);
});
});
});

0 comments on commit b4c5b5a

Please sign in to comment.