1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parsePathInfo = exports.upload = exports.download = exports.copy = exports.list = void 0;
|
4 | const cloud_bucket_1 = require("cloud-bucket");
|
5 | const utils_1 = require("./utils");
|
6 |
|
7 | async function list(pathInfo) {
|
8 | const bucket = await getBucketFromPathInfo(pathInfo);
|
9 | const result = await bucket.listFiles(pathInfo.path);
|
10 | return result;
|
11 | }
|
12 | exports.list = list;
|
13 | async function copy(from, to) {
|
14 | const files = await list(from);
|
15 | const destBucket = await getBucketFromPathInfo(to);
|
16 |
|
17 | }
|
18 | exports.copy = copy;
|
19 | async function download(from, destDir) {
|
20 | if (from.path == null) {
|
21 | throw new Error(`Cannot download - undefined remote path: ${from.store}:${from.path}`);
|
22 | }
|
23 | const bucket = await getBucketFromPathInfo(from);
|
24 | return bucket.download(from.path, destDir);
|
25 | }
|
26 | exports.download = download;
|
27 | async function upload(localFile, dest) {
|
28 | if (dest.path == null) {
|
29 | throw new Error(`Cannot download - undefined remote path: ${dest.store}:${dest.path}`);
|
30 | }
|
31 | const bucket = await getBucketFromPathInfo(dest);
|
32 | return bucket.upload(localFile, dest.path);
|
33 | }
|
34 | exports.upload = upload;
|
35 |
|
36 |
|
37 | const formatInfo = `'storename:path' or just 'storename'`;
|
38 | function parsePathInfo(storeAndPathStr) {
|
39 | if (storeAndPathStr == null || storeAndPathStr.length == 0) {
|
40 | throw new Error(`gs need to have a path like ${formatInfo}`);
|
41 | }
|
42 | const storeAndPath = storeAndPathStr.split(':');
|
43 | if (storeAndPath.length > 2) {
|
44 | throw new Error(`Path ${storeAndPathStr} is invalid, needs to be of format like ${formatInfo}`);
|
45 | }
|
46 | const [store, path] = storeAndPath;
|
47 | return { store, path };
|
48 | }
|
49 | exports.parsePathInfo = parsePathInfo;
|
50 |
|
51 |
|
52 | async function getBucketFromPathInfo(pathInfo) {
|
53 | const bucketCfgs = await loadRawConfig();
|
54 |
|
55 | const rawCfg = bucketCfgs[pathInfo.store];
|
56 | return cloud_bucket_1.getBucket(rawCfg);
|
57 | }
|
58 | async function loadRawConfig() {
|
59 | let vdevBuckets = await utils_1.loadYaml('vdev-buckets.yaml');
|
60 | const confs = vdevBuckets.buckets;
|
61 |
|
62 | for (let name in confs) {
|
63 | confs[name].name = name;
|
64 | }
|
65 | return confs;
|
66 | }
|
67 |
|