Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

writeTemplate to write the wrapper script + tests

  • Loading branch information...
commit d86443e7bd7ce00af9fcaf63c61c10ec44375bc0 1 parent ae1e057
@niallo authored
View
3  .gitignore
@@ -6,10 +6,11 @@ lib-cov
*.out
*.pid
*.gz
+*.swp
pids
logs
results
node_modules
-npm-debug.log
+npm-debug.log
View
23 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.
View
2  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
View
64 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,
+}
View
29 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": "*"
+ }
+}
View
70 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()
+ })
+
+ })
+
+ })
+
+
+
+
+})
+
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.