-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use new parsing structure and classes and bump version to 0.1 to publ…
…ish to npm
- Loading branch information
spankr
committed
Mar 11, 2017
1 parent
1e6b801
commit 3acc8c4
Showing
18 changed files
with
372 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.DS_Store | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,11 @@ | ||
const chalk = require('chalk') | ||
const filterOptions = require('app/cli/helpers/filterOptions') | ||
const nginxUtil = require('app/nginxUtil') | ||
|
||
exports.command = 'config <action> [options]', | ||
exports.describe = 'Server basic configuration related commands', | ||
exports.command = 'config <action> [options]' | ||
exports.describe = 'Server basic configuration related commands' | ||
|
||
exports.builder = (yargs) => { | ||
return yargs | ||
.usage('Usage: nginrx config <action> [options]') | ||
.commandDir('configCmds') | ||
.check(checker) | ||
.demandCommand(1, 1) | ||
} | ||
|
||
exports.handler = () => {} | ||
|
||
let cmds = { | ||
test: { | ||
name: 'test', | ||
description: 'Test current nginx configuration', | ||
builder: testBuilder, | ||
handler: testHandler, | ||
}, | ||
output: { | ||
name: 'output', | ||
description: 'Output current nginx configuration in json', | ||
builder: outputBuilder, | ||
handler: outputHandler, | ||
}, | ||
} | ||
|
||
|
||
function testBuilder(yargs) { | ||
return yargs | ||
.usage('Usage: $0 config test') | ||
.check(testChecker) | ||
} | ||
|
||
function outputBuilder(yargs) { | ||
return yargs | ||
.usage('Usage: $0 config output [options]') | ||
.option('f', { alias: 'file', describe: 'Output data to a file' }) | ||
.check(outputChecker) | ||
} | ||
|
||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, []) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
if(argv._.length === 1) { | ||
throw new Error('No command provided') | ||
} | ||
if(Object.keys(cmds).indexOf(argv._[1] < 0)) { | ||
throw new Error('Unknown command: '+ argv._[1]) | ||
} | ||
return true | ||
} | ||
|
||
function testChecker(argv) { | ||
let extraOptions = filterOptions(argv, []) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
if(argv._.length > 2) { | ||
throw new Error('Unknown command: '+ argv._[2]) | ||
} | ||
return true | ||
} | ||
|
||
function outputChecker(argv) { | ||
let extraOptions = filterOptions(argv, ['file']) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
if(argv._.length > 2) { | ||
throw new Error('Unknown command: '+ argv._[2]) | ||
} | ||
return true | ||
} | ||
|
||
function testHandler() { | ||
let result = nginxUtil.testConfig() | ||
if(result.success) { | ||
console.log(chalk.green('>>> Nginx config ok')) | ||
} else { | ||
console.log(chalk.red('>>> result.error')) | ||
} | ||
} | ||
|
||
function outputHandler() { | ||
console.log(chalk.blue('>>> Handled output command')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,63 @@ | ||
const chalk = require('chalk') | ||
const fs = require('fs') | ||
const util = require('util') | ||
const nginrx = require('app') | ||
const NginrxError = require('app/errors/nginrxError') | ||
const filterOptions = require('app/cli/helpers/filterOptions') | ||
|
||
exports.command = 'output [options]', | ||
exports.describe = 'Output current nginx configuration in json', | ||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, ['file', 'f', 'output', 'o']) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
return true | ||
} | ||
|
||
exports.command = 'output [options]' | ||
exports.describe = 'Output current nginx configuration in json' | ||
|
||
exports.builder = (yargs) => { | ||
return yargs | ||
.option('f', { alias: 'file', describe: 'Output data to a file' }) | ||
.option('f', { | ||
alias: 'file', | ||
describe: 'Write output data to a file', | ||
}) | ||
.option('o', { | ||
alias: 'output', | ||
describe: 'Data output format', | ||
choices: ['string', 'json', 'object'], | ||
default : 'object', | ||
}) | ||
.check(checker) | ||
.demandCommand(0, 0) | ||
} | ||
|
||
exports.handler = () => { console.log(chalk.blue('>>> Handled output command')) } | ||
exports.handler = (argv) => { | ||
let config | ||
try { | ||
config = nginrx.getNginxConfig() | ||
} catch(e) { | ||
throw new NginrxError('Error in getting config', e.message) | ||
} | ||
|
||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, ['file']) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
let result = config | ||
if(argv.output === 'json') { | ||
result = config.toJson() | ||
} else if(argv.output === 'string') { | ||
result = config.toString() | ||
} | ||
if(argv._.length > 2) { | ||
throw new Error('Unknown command: '+ argv._[2]) | ||
|
||
if(!argv.file) { | ||
if(argv.output === 'object') { | ||
console.log(util.inspect(result, null, null)) | ||
} else { | ||
console.log(result) | ||
} | ||
} else { | ||
try { | ||
fs.writeFileSync(argv.file, result) | ||
} catch(e) { | ||
throw new NginrxError('Error in writing file', e.message) | ||
} | ||
console.log('Config successfully written') | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,56 @@ | ||
const fs = require('fs') | ||
const util = require('util') | ||
const filterOptions = require('app/cli/helpers/filterOptions') | ||
const parser = require('app/parser') | ||
const NginrxError = require('app/errors/nginrxError') | ||
|
||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, ['f', 'file', 'o', 'output']) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.command = 'parse [string] [options]' | ||
exports.describe = 'Parse the input Nginx config' | ||
exports.describe = 'Parse the input Nginx config and output to console' | ||
|
||
exports.builder = (yargs) => { | ||
return yargs | ||
.option('f', { alias: 'file', describe: 'Parse this file' }) | ||
.usage('Usage: nginrx parse [string] [options]') | ||
.option('f', { | ||
alias: 'file', | ||
describe: 'Parse this file', | ||
}) | ||
.option('o', { | ||
alias: 'output', | ||
describe: 'Data output format', | ||
choices: ['string', 'json', 'object'], | ||
default : 'object', | ||
}) | ||
.check(checker) | ||
} | ||
|
||
exports.handler = (argv) => { | ||
if(!argv.string && !argv.file) { | ||
throw new NginrxError('Need at-least a string or a file to parse') | ||
} | ||
|
||
let string | ||
if(argv.string) { | ||
string = argv.string.toString() | ||
} else if(argv.file) { | ||
string = fs.readFileSync(argv.file, 'utf-8') | ||
} | ||
console.log(require('util').inspect(parser.parse(string), null, null)) | ||
} | ||
|
||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, ['f', 'file']) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
const config = parser.parse(string) | ||
|
||
return true | ||
if(argv.output === 'json') { | ||
console.log(config.toJson()) | ||
} else if(argv.output === 'object') { | ||
console.log(util.inspect(config, null, null)) | ||
} else { | ||
console.log(config.toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
const NginrxError = require('app/errors/nginrxError') | ||
const filterOptions = require('app/cli/helpers/filterOptions') | ||
|
||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, []) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
return true | ||
} | ||
|
||
exports.command = 'sites <action> [options]' | ||
exports.describe = 'Sites/Server blocks related commands' | ||
|
||
exports.builder = {} | ||
|
||
exports.handler = () => { | ||
throw new NginrxError('Sites command not defined yet') | ||
exports.builder = (yargs) => { | ||
return yargs | ||
.usage('Usage: nginrx sites <action> [options]') | ||
.commandDir('sitesCmds') | ||
.check(checker) | ||
.demandCommand(1, 1) | ||
} | ||
|
||
exports.handler = () => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const filterOptions = require('app/cli/helpers/filterOptions') | ||
const nginrx = require('app') | ||
|
||
function checker(argv) { | ||
let extraOptions = filterOptions(argv, []) | ||
if(extraOptions.length > 0) { | ||
throw new Error('Unknown option: '+ extraOptions[0]) | ||
} | ||
|
||
return true | ||
} | ||
|
||
exports.command = 'status' | ||
exports.describe = 'Status of active nginx sites' | ||
|
||
exports.builder = (yargs) => { | ||
return yargs | ||
.check(checker) | ||
.demandCommand(0, 0) | ||
} | ||
|
||
exports.handler = () => { | ||
const config = nginrx.getConfig() | ||
const sites = config.find('block', 'server') | ||
|
||
if(sites.length > 0) { | ||
console.log(sites.length, 'active nginx sites') | ||
sites.forEach((site) => { | ||
console.log(site.server_name) | ||
}) | ||
} else { | ||
console.log('No active nginx site') | ||
} | ||
} |
Oops, something went wrong.