Skip to content
This repository
Browse code

[api] Support fetching a snapshot

  • Loading branch information...
commit 0ea3827a6548b8d6ad09342827245f4159578892 1 parent 2d9cf58
authored June 28, 2012

Showing 1 changed file with 58 additions and 1 deletion. Show diff stats Hide diff stats

  1. 59  lib/jitsu/commands/snapshots.js
59  lib/jitsu/commands/snapshots.js
@@ -5,7 +5,8 @@
5 5
  *
6 6
  */
7 7
  
8  
-var semver = require('semver'),
  8
+var fs = require('fs'),
  9
+    semver = require('semver'),
9 10
     dateformat = require('dateformat'),
10 11
     jitsu = require('../../jitsu');
11 12
 
@@ -22,6 +23,8 @@ snapshots.usage = [
22 23
   'jitsu snapshots list <app-name>',
23 24
   'jitsu snapshots activate',
24 25
   'jitsu snapshots activate <app-name>',
  26
+  'jitsu snapshots fetch',
  27
+  'jitsu snapshots fetch <app-name>',
25 28
   'jitsu snapshots destroy',
26 29
   'jitsu snapshots destroy <app-name>',
27 30
   '',
@@ -227,6 +230,60 @@ snapshots.activate.usage = [
227 230
   'jitsu snapshots activate <app-name>'
228 231
 ];
229 232
 
  233
+snapshots.fetch = function (name, callback) {
  234
+  if (typeof name === 'function') {
  235
+    name = null;
  236
+    callback = name;
  237
+  }
  238
+
  239
+  function fetch(appName, snapshot) {
  240
+    jitsu.snapshots.fetch(appName, snapshot.id)
  241
+      .on('error', callback)
  242
+      .on('response', function (res) {
  243
+        if (res.statusCode === 404) {
  244
+          return callback(new Error('No such snapshot'));
  245
+        }
  246
+        else if (res.statusCode !== 200) {
  247
+          return callback(new Error('Unknown status code: ' + res.statusCode));
  248
+        }
  249
+
  250
+        var filename = appName.replace('/', '-') + '-' + snapshot.id + '.tgz';
  251
+        res.pipe(fs.createWriteStream(filename)).on('close', function () {
  252
+          jitsu.log.info('Snapshot ' + snapshot.id + ' saved to file ' + filename + '.');
  253
+        });
  254
+      });
  255
+  }
  256
+
  257
+  snapshots.list(name, function (err, snapshots, appName) {
  258
+    if (err) {
  259
+      return callback(err);
  260
+    }
  261
+
  262
+    jitsu.prompt.get(['snapshot'], function (err, result) {
  263
+      if (err) {
  264
+        return callback(err);
  265
+      }
  266
+
  267
+      var snapshot = snapshots.filter(function (snap) {
  268
+        return snap.id === result.snapshot;
  269
+      })[0];
  270
+
  271
+      if (!snapshot) {
  272
+        return callback(new Error('Cannot find snapshot with name: ' + result['snapshot'].magenta), true);
  273
+      }
  274
+
  275
+      fetch(appName, snapshot);
  276
+    });
  277
+  });
  278
+};
  279
+
  280
+snapshots.fetch.usage = [
  281
+  'Fetches a snapshot for the application in the current directory',
  282
+  '',
  283
+  'jitsu snapshots fetch',
  284
+  'jitsu snapshots fetch <app-name>'
  285
+];
  286
+
230 287
 //
231 288
 // ### function destroy (appName, callback)
232 289
 // #### @count {string} **optional** Number of snapshots to destroy

0 notes on commit 0ea3827

Please sign in to comment.
Something went wrong with that request. Please try again.