Skip to content

Commit

Permalink
use new parsing structure and classes and bump version to 0.1 to publ…
Browse files Browse the repository at this point in the history
…ish to npm
  • Loading branch information
spankr committed Mar 11, 2017
1 parent 1e6b801 commit 3acc8c4
Show file tree
Hide file tree
Showing 18 changed files with 372 additions and 262 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
*.log

88 changes: 3 additions & 85 deletions lib/cli/commands/config.js
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'))
}
62 changes: 50 additions & 12 deletions lib/cli/commands/configCmds/output.js
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
}
29 changes: 13 additions & 16 deletions lib/cli/commands/configCmds/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const chalk = require('chalk')
const filterOptions = require('app/cli/helpers/filterOptions')
const nginxUtil = require('app/nginxUtil')
const NginrxError = require('app/errors/nginrxError')

exports.command = 'test',
exports.describe = 'Test current nginx configuration',
function checker(argv) {
let extraOptions = filterOptions(argv, [])
if(extraOptions.length > 0) {
throw new Error('Unknown option: '+ extraOptions[0])
}

return true
}

exports.command = 'test'
exports.describe = 'Test current nginx configuration'

exports.builder = (yargs) => {
return yargs
Expand All @@ -13,21 +21,10 @@ exports.builder = (yargs) => {
}

exports.handler = () => {
throw new NginrxError('Command not defined yet')
let result = nginxUtil.testConfig()
if(result.success) {
console.log(chalk.green('>>> Nginx config ok'))
console.log(chalk.green('Nginx config ok'))
} else {
console.log(chalk.red('>>> result.error'))
console.log(chalk.red('Nginx config error: ' +result.error))
}
}


function checker(argv) {
let extraOptions = filterOptions(argv, [])
if(extraOptions.length > 0) {
throw new Error('Unknown option: '+ extraOptions[0])
}

return true
}
45 changes: 35 additions & 10 deletions lib/cli/commands/parse.js
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())
}
}
22 changes: 17 additions & 5 deletions lib/cli/commands/sites.js
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 = () => {}
34 changes: 34 additions & 0 deletions lib/cli/commands/sitesCmds/status.js
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')
}
}
Loading

0 comments on commit 3acc8c4

Please sign in to comment.