UNPKG

2.62 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parsePathInfo = exports.upload = exports.download = exports.copy = exports.list = void 0;
4const cloud_bucket_1 = require("cloud-bucket");
5const utils_1 = require("./utils");
6// --------- Public list/copy/del/download/upload --------- //
7async function list(pathInfo) {
8 const bucket = await getBucketFromPathInfo(pathInfo);
9 const result = await bucket.listFiles(pathInfo.path);
10 return result;
11}
12exports.list = list;
13async function copy(from, to) {
14 const files = await list(from);
15 const destBucket = await getBucketFromPathInfo(to);
16 // FIXME: Need to be implemented
17}
18exports.copy = copy;
19async 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}
26exports.download = download;
27async 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}
34exports.upload = upload;
35// --------- /Public list/cp/del/download/upload ------, --- //
36// --------- public PathInfo --------- //
37const formatInfo = `'storename:path' or just 'storename'`;
38function 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}
49exports.parsePathInfo = parsePathInfo;
50// --------- /public PathInfo --------- //
51// --------- Private Utils --------- //
52async function getBucketFromPathInfo(pathInfo) {
53 const bucketCfgs = await loadRawConfig();
54 // TODO: Needs to recognize the config from gcp vs aws (for now, assume gcp)
55 const rawCfg = bucketCfgs[pathInfo.store];
56 return cloud_bucket_1.getBucket(rawCfg);
57}
58async function loadRawConfig() {
59 let vdevBuckets = await utils_1.loadYaml('vdev-buckets.yaml');
60 const confs = vdevBuckets.buckets;
61 // add the .name to each bucket info
62 for (let name in confs) {
63 confs[name].name = name;
64 }
65 return confs;
66}
67// --------- /Private Utils --------- //