Skip to content

Commit

Permalink
added methods for manipulating installed refs
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Dec 1, 2014
1 parent ef4e9df commit 9286eaf
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 25 deletions.
80 changes: 58 additions & 22 deletions lib/Apps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Autowire = require('wantsit').Autowire,
util = require('util'),
Actions = require('./Actions')
Actions = require('./Actions'),
Table = require('./Table')

var Apps = function() {
Actions.call(this)
Expand All @@ -12,19 +13,14 @@ util.inherits(Apps, Actions)

Apps.prototype.deployApplication = function(name, url, options) {
this._do(options, function(boss) {
var opts = {
user: options.user || this._user.name
}

boss.deployApplication(name, url, opts, function(info) {
console.info(info)
}, function(err) {
console.error(err)
}, function(error) {
if(error) throw error
var user = options.user || this._user.name

boss.disconnect()
})
boss.deployApplication(name, url, user, console.info, console.error,
function(error) {
if(error) throw error

boss.disconnect()
})
}.bind(this))
}

Expand All @@ -33,10 +29,15 @@ Apps.prototype.listApplications = function(options) {
boss.listApplications(function(error, deployedApplications) {
if(error) throw error

var table = new Table('No applications have been deployed')
table.addHeader(['Name', 'User', 'URL'])

deployedApplications.forEach(function(app) {
console.info(app.name)
table.addRow([app.name, app.user, app.url])
})

table.print(console.info)

boss.disconnect()
})
}.bind(this))
Expand All @@ -56,17 +57,52 @@ Apps.prototype.runApplication = function(name, ref, options) {
ref = ref || 'master'

this._do(options, function(boss) {
boss.switchApplicationRef(name, ref, function(info) {
console.info(info)
}, function(err) {
console.error(err)
}, function(error, applicationInfo) {
boss.switchApplicationRef(name, ref, console.info, console.error,
function(error, applicationInfo) {
if(error) throw error

options.name = name

this._processes.start(applicationInfo.path, options)
}.bind(this))
}.bind(this))
}

Apps.prototype.listRefs = function(name, options) {
this._do(options, function(boss) {
boss.listApplicationRefs(name, function(error, refs) {
if(error) throw error

var table = new Table(name + ' has no refs')
table.addHeader(['Name', 'Commit'])

refs.forEach(function(ref) {
table.addRow([ref.name, ref.commit])
})

table.print(console.info)

boss.disconnect()
})
}.bind(this))
}

Apps.prototype.updateRefs = function(name, options) {
this._do(options, function(boss) {
boss.updateApplicationRefs(name, console.info, console.error, function(error, refs) {
if(error) throw error

options.name = name
var table = new Table(name + ' has no refs')
table.addHeader(['Name', 'Commit'])

refs.forEach(function(ref) {
table.addRow([ref.name, ref.commit])
})

this._processes.start(applicationInfo.path, options)
}.bind(this))
table.print(console.info)

boss.disconnect()
})
}.bind(this))
}

Expand Down
14 changes: 13 additions & 1 deletion lib/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ CLI.prototype.afterPropertiesSet = function() {

this._commander
.command('startapp <name> [ref]')
.description('Start a deployed application')
.description('Start a deployed application at master/HEAD or the passed ref')
.option('-u, --user <user>', 'The user to start a process as')
.option('-g, --group <group>', 'The group to start a process as')
.option('-i, --instances <instances>', 'How many instances of the process to start', parseInt)
Expand All @@ -192,6 +192,18 @@ CLI.prototype.afterPropertiesSet = function() {
.option('-v, --verbose', 'Prints detailed internal logging output')
.action(this._apps.runApplication.bind(this._apps))

this._commander
.command('lsrefs <name>')
.description('Lists app refs available to be started')
.option('-v, --verbose', 'Prints detailed internal logging output')
.action(this._apps.listRefs.bind(this._apps))

this._commander
.command('updaterefs <name>')
.description('Updates app refs available to be started')
.option('-v, --verbose', 'Prints detailed internal logging output')
.action(this._apps.updateRefs.bind(this._apps))

this._commander
.command('*')
.description('')
Expand Down
87 changes: 85 additions & 2 deletions test/lib/AppsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ describe('Apps', function() {
boss.listApplications = sinon.stub()
boss.listApplications.callsArgWith(0, undefined, applications)

var invocations = 1

console.info = function(data) {
expect(data).to.equal('foo')
if(invocations == 2) {
// first invocation is table header..
expect(data).to.contain('foo')

done()
done()
}

invocations++
}

apps.listApplications(options)
Expand Down Expand Up @@ -226,4 +233,80 @@ describe('Apps', function() {

expect(boss.switchApplicationRef.withArgs(name, 'master', sinon.match.func, sinon.match.func, sinon.match.func).called).to.be.true
})

it('should list application refs', function(done) {
var app = 'foo'
var options = {}
var refs = [{
name: 'bar'
}]
boss.listApplicationRefs = sinon.stub()
boss.listApplicationRefs.withArgs(app, sinon.match.func).callsArgWith(1, undefined, refs)

var invocations = 1

console.info = function(data) {
if(invocations == 2) {
// first invocation is table header..
expect(data).to.contain('bar')

done()
}

invocations++
}

apps.listRefs(app, options)
})

it('should fail to list application refs', function() {
var app = 'foo'
var options = {}
boss.listApplicationRefs = sinon.stub()
boss.listApplicationRefs.withArgs(app, sinon.match.func).callsArgWith(1, new Error('urk!'))

try {
apps.listRefs(app, options)
} catch(e) {
if(e.message != 'urk!') throw e
}
})

it('should update application refs', function(done) {
var app = 'foo'
var options = {}
var refs = [{
name: 'bar'
}]
boss.updateApplicationRefs = sinon.stub()
boss.updateApplicationRefs.withArgs(app, sinon.match.func, sinon.match.func, sinon.match.func).callsArgWith(3, undefined, refs)

var invocations = 1

console.info = function(data) {
if(invocations == 2) {
// first invocation is table header..
expect(data).to.contain('bar')

done()
}

invocations++
}

apps.updateRefs(app, options)
})

it('should fail to update application refs', function() {
var app = 'foo'
var options = {}
boss.updateApplicationRefs = sinon.stub()
boss.updateApplicationRefs.withArgs(app, sinon.match.func, sinon.match.func, sinon.match.func).callsArgWith(3, new Error('urk!'))

try {
apps.updateRefs(app, options)
} catch(e) {
if(e.message != 'urk!') throw e
}
})
})

0 comments on commit 9286eaf

Please sign in to comment.