forked from DrPaulBrewer/zip-bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.js
More file actions
executable file
·69 lines (57 loc) · 1.94 KB
/
bin.js
File metadata and controls
executable file
·69 lines (57 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
/* Copyright 2017 Paul Brewer, Economic and Financial Technology Consulting LLC */
/* This file is open source software. The MIT License applies to this software. */
/* jshint node:true,esnext:true,eqeqeq:true,undef:true,lastsemic:true */
const fs = require('fs');
const program = require('commander');
// for storage API 2.x
const {Storage} = require('@google-cloud/storage');
const zipBucketFactory = require("./index.js");
const assert = require('assert');
const z = {};
let projectId, credentials, useJSON, keep;
function setAPIKey(keyFilename){
projectId = JSON.parse(fs.readFileSync(keyFilename)).project_id;
credentials = { projectId, keyFilename };
}
function setJSON(){
useJSON = true;
}
function setProgress(){
z.progress = 1;
}
function setKeep(path){
z.keep = path;
}
function gsParse(path, bucketProperty, pathProperty){
const match = /gs\:\/\/([^\/]+)\/*(.*)/.exec(path);
if ((!match) || (match.length<2)) return false;
z[bucketProperty] = match[1];
z[pathProperty] = match[2] || '';
return true;
}
function output(status){
if (useJSON)
console.log(JSON.stringify(status,null,2));
else
explain(status);
}
(program
.version('0.1.0')
.arguments('<fromBucketPath> [toBucketPath]')
.option('--key <keyfile>', 'keyfile to access Google Cloud Storage API', setAPIKey)
.option('--keep <keep>', 'path in local filesystem to keep a copy of the .zip file', setKeep)
.option('--progress', 'show progress messages', setProgress)
.option('--json','output parameters and manifest in json', setJSON)
.action(function(fromBucketPath, toBucketPath){
if (gsParse(fromBucketPath,'fromBucket','fromPath')){
gsParse(toBucketPath, 'toBucket', 'toPath');
const storage = Storage(credentials);
const zipBucket = zipBucketFactory(storage);
zipBucket(z).then(
(status)=>{if (useJSON) console.log(JSON.stringify(status,null,2));}, (e)=>(console.log(e))
);
}
})
.parse(process.argv)
);