Skip to content

Commit

Permalink
writeTemplate to write the wrapper script + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Niall O'Higgins committed Aug 26, 2012
1 parent ae1e057 commit d86443e
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -6,10 +6,11 @@ lib-cov
*.out
*.pid
*.gz
*.swp

pids
logs
results

node_modules
npm-debug.log
npm-debug.log
23 changes: 23 additions & 0 deletions LICENSE
@@ -0,0 +1,23 @@
Copyright 2012 Niall O'Higgins
All rights reserved.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
Gitane
======

Node.JS Git wrapper with support for SSH public keys
Easy Node.JS Git wrapper with support for SSH keys
64 changes: 64 additions & 0 deletions index.js
@@ -0,0 +1,64 @@
var crypto = require('crypto')
var exec = require('child_process').exec
var fs = require('fs')
var path = require('path')
var os = require('os')
var Step = require('step')

var GIT_SSH_TEMPLATE = '#!/bin/sh\n' +
'exec ssh -i $key -o StrictHostKeyChecking=no "$$@"\n'

//
// Write the Git script template to enable use of the SSH private key
//
// *privKey* SSH private key.
// *file* (optional) filename of script.
// *cb* callback function of signature function(err, filename).
//
function writeTemplate(privKey, file, cb) {
// No file name - generate a random one under the system temp dir
if (!file) {
var randomStr = crypto.randomBytes(4).toString('hex')
var name = "_gitane" + randomStr + ".sh"
file = path.join(os.tmpDir(), name)
}

var data = GIT_SSH_TEMPLATE.replace('$key', privKey)
Step(
function() {
fs.writeFile(file, data, this)
},
function(err) {
if (err) {
return cb(err, null)
}
// make executable
fs.chmod(file, '0755', this)

},
function(err) {
if (err) {
return cb(err, null)
}

return cb(null, file)
}
)
}

//
// Run a `git` process
//
// *baseDir* current working dir from which to execute git
// *privKey* SSH private key to use
// *cmd* command to run
//
function run(baseDir, privKey, cmd) {


}

module.exports = {
run:run,
writeTemplate:writeTemplate,
}
29 changes: 29 additions & 0 deletions package.json
@@ -0,0 +1,29 @@
{
"author": "Niall O'Higgins <niallo@beyondfog.com> (http://niallohiggins.com)",
"name": "Gitane",
"description": "Easy Node.JS Git wrapper with support for SSH keys",
"version": "0.0.0",
"homepage": "https://github.com/niallo/Gitane",
"repository": {
"type": "git",
"url": "git://github.com/niallo/Gitane.git"
},
"main": "index.js",
"scripts": {
"test": "mocha -R tap"
},
"dependencies": {
"step": "~0.0.5"
},
"devDependencies": {
"mocha": "~1.3.0",
"chai": "~1.1.1"
},
"scripts": {
"test": "mocha -R tap"
},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
70 changes: 70 additions & 0 deletions test/test.js
@@ -0,0 +1,70 @@
var expect = require('chai').expect
var fs = require('fs')
var gitane = require('../index')

describe('gitane', function() {

describe('#writeTemplate', function() {

it('should create a random file if none specified', function(done) {

gitane.writeTemplate('testkey', null, function(err, file) {
expect(err).to.be.null
expect(file).to.be.a('string')
expect(file).to.match(/_gitane/)
var data = fs.readFileSync(file)
expect(data).to.match(/exec ssh -i testkey/)

fs.unlinkSync(file)

done()
})

})

it('should use passed-in file if specified', function(done) {
var filename = "_testfile"

gitane.writeTemplate('testkey', filename, function(err, file) {
expect(file).to.eql(filename)
expect(err).to.be.null
var data = fs.readFileSync(file)
expect(data).to.match(/exec ssh -i testkey/)

fs.unlinkSync(file)

done()
})

})

it('should create an executable file', function(done) {
var filename = "_testfile"

gitane.writeTemplate('testkey', filename, function(err, file) {
expect(file).to.eql(filename)
expect(err).to.be.null

var stats = fs.statSync(file)

// Note we must convert to octal ourselves.
expect(stats.mode.toString(8)).to.eql('100755')

fs.unlinkSync(file)

done()
})

})

})




})





0 comments on commit d86443e

Please sign in to comment.