UNPKG

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