UNPKG

2.4 kBPlain TextView Raw
1import { getBucket, Bucket } from 'cloud-bucket';
2import { loadYaml } from './utils';
3
4export type PathInfo = {
5 store: string,
6 path?: string, // path or glob
7}
8
9
10// --------- Public list/copy/del/download/upload --------- //
11export async function list(pathInfo: PathInfo) {
12 const bucket = await getBucketFromPathInfo(pathInfo);
13 const result = await bucket.list(pathInfo.path);
14 return result;
15}
16
17export async function copy(from: PathInfo, to: PathInfo) {
18 const files = await list(from);
19 const destBucket = await getBucketFromPathInfo(to);
20 // FIXME: Need to be implemented
21}
22
23export async function download(from: PathInfo, destDir: string) {
24 if (from.path == null) {
25 throw new Error(`Cannot download - undefined remote path: ${from.store}:${from.path}`);
26 }
27 const bucket = await getBucketFromPathInfo(from);
28 return bucket.download(from.path, destDir);
29}
30
31export async function upload(localFile: string, dest: PathInfo) {
32 if (dest.path == null) {
33 throw new Error(`Cannot download - undefined remote path: ${dest.store}:${dest.path}`);
34 }
35 const bucket = await getBucketFromPathInfo(dest);
36 return bucket.upload(localFile, dest.path);
37}
38// --------- /Public list/cp/del/download/upload ------, --- //
39
40// --------- public PathInfo --------- //
41
42
43const formatInfo = `'storename:path' or just 'storename'`;
44
45export function parsePathInfo(storeAndPathStr?: string) {
46 if (storeAndPathStr == null || storeAndPathStr.length == 0) {
47 throw new Error(`gs need to have a path like ${formatInfo}`);
48 }
49 const storeAndPath = storeAndPathStr.split(':');
50 if (storeAndPath.length > 2) {
51 throw new Error(`Path ${storeAndPathStr} is invalid, needs to be of format like ${formatInfo}`)
52 }
53
54 const [store, path] = storeAndPath;
55
56 return { store, path };
57
58}
59// --------- /public PathInfo --------- //
60
61
62// --------- Private Utils --------- //
63
64async function getBucketFromPathInfo(pathInfo: PathInfo): Promise<Bucket> {
65 const bucketCfgs = await loadRawConfig();
66
67 // TODO: Needs to recognize the config from gcp vs aws (for now, assume gcp)
68 const rawCfg = bucketCfgs[pathInfo.store];
69 return getBucket(rawCfg);
70
71}
72
73
74async function loadRawConfig() {
75 let vdevBuckets: any = await loadYaml('vdev-buckets.yaml');
76 const confs = vdevBuckets.buckets;
77 // add the .name to each bucket info
78 for (let name in confs) {
79 confs[name].name = name;
80 }
81 return confs;
82}
83// --------- /Private Utils --------- //
\No newline at end of file