Browse files

add manifest downloader

  • Loading branch information...
1 parent 15217af commit b984e9867562ab2918ebdbf2ec969b7692c15b73 @ddollar committed May 30, 2012
Showing with 47 additions and 0 deletions.
  1. +4 −0 bin/download_manifest
  2. +43 −0 lib/download_manifest.coffee
View
4 bin/download_manifest
@@ -0,0 +1,4 @@
+#!/usr/bin/env node
+
+require('coffee-script');
+require('../lib/download_manifest').execute(process.argv);
View
43 lib/download_manifest.coffee
@@ -0,0 +1,43 @@
+async = require("async")
+fs = require("fs")
+http = require("http")
+https = require("https")
+mkdirp = require("mkdirp")
+os = require("os")
+path = require("path")
+program = require("commander")
+url = require("url")
+util = require("util")
+
+program
+ .version(require("#{__dirname}/../package.json").version)
+ .usage('[options] <slug url>')
+ .option('-a, --auth <password>', 'admin password')
+ .option('-c, --concurrency <num>', 'number of workers', os.cpus().length)
+ .option('-e, --env <url>', 'environment file')
+ .option('-p, --port <port>', 'port on which to listen', 3000)
+
+datastore_fetchers = (manifest, dir) ->
+ fetchers = {}
+ for name, file_manifest of manifest
+ do (name, file_manifest) =>
+ fetchers[file_manifest.hash] = (async_cb) =>
+ filename = "#{dir}/#{name}"
+ mkdirp path.dirname(filename), =>
+ file = fs.createWriteStream filename
+ options = url.parse("#{process.env.ANVIL_HOST}/file/#{file_manifest["hash"]}")
+ client = if options.protocol is "https:" then https else http
+ client.get options, (get) ->
+ get.on "data", (chunk) -> file.write chunk
+ get.on "end", ->
+ file.end()
+ fs.chmod filename, file_manifest.mode, (err) ->
+ async_cb null, true
+
+module.exports.execute = (args) ->
+ program.parse(args)
+ fs.readFile program.args[0], (err, data) ->
+ manifest = JSON.parse(data)
+ mkdirp program.args[1]
+ async.parallel datastore_fetchers(manifest, program.args[1]), (err, results) ->
+ console.log "complete"

0 comments on commit b984e98

Please sign in to comment.