Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[api] Support fetching a snapshot

  • Loading branch information...
commit 0ea3827a6548b8d6ad09342827245f4159578892 1 parent 2d9cf58
Maciej Małecki mmalecki authored
Showing with 58 additions and 1 deletion.
  1. +58 −1 lib/jitsu/commands/snapshots.js
59 lib/jitsu/commands/snapshots.js
View
@@ -5,7 +5,8 @@
*
*/
-var semver = require('semver'),
+var fs = require('fs'),
+ semver = require('semver'),
dateformat = require('dateformat'),
jitsu = require('../../jitsu');
@@ -22,6 +23,8 @@ snapshots.usage = [
'jitsu snapshots list <app-name>',
'jitsu snapshots activate',
'jitsu snapshots activate <app-name>',
+ 'jitsu snapshots fetch',
+ 'jitsu snapshots fetch <app-name>',
'jitsu snapshots destroy',
'jitsu snapshots destroy <app-name>',
'',
@@ -227,6 +230,60 @@ snapshots.activate.usage = [
'jitsu snapshots activate <app-name>'
];
+snapshots.fetch = function (name, callback) {
+ if (typeof name === 'function') {
+ name = null;
+ callback = name;
+ }
+
+ function fetch(appName, snapshot) {
+ jitsu.snapshots.fetch(appName, snapshot.id)
+ .on('error', callback)
+ .on('response', function (res) {
+ if (res.statusCode === 404) {
+ return callback(new Error('No such snapshot'));
+ }
+ else if (res.statusCode !== 200) {
+ return callback(new Error('Unknown status code: ' + res.statusCode));
+ }
+
+ var filename = appName.replace('/', '-') + '-' + snapshot.id + '.tgz';
+ res.pipe(fs.createWriteStream(filename)).on('close', function () {
+ jitsu.log.info('Snapshot ' + snapshot.id + ' saved to file ' + filename + '.');
+ });
+ });
+ }
+
+ snapshots.list(name, function (err, snapshots, appName) {
+ if (err) {
+ return callback(err);
+ }
+
+ jitsu.prompt.get(['snapshot'], function (err, result) {
+ if (err) {
+ return callback(err);
+ }
+
+ var snapshot = snapshots.filter(function (snap) {
+ return snap.id === result.snapshot;
+ })[0];
+
+ if (!snapshot) {
+ return callback(new Error('Cannot find snapshot with name: ' + result['snapshot'].magenta), true);
+ }
+
+ fetch(appName, snapshot);
+ });
+ });
+};
+
+snapshots.fetch.usage = [
+ 'Fetches a snapshot for the application in the current directory',
+ '',
+ 'jitsu snapshots fetch',
+ 'jitsu snapshots fetch <app-name>'
+];
+
//
// ### function destroy (appName, callback)
// #### @count {string} **optional** Number of snapshots to destroy
Please sign in to comment.
Something went wrong with that request. Please try again.