1 | import { Bucket, getBucket } from 'cloud-bucket';
|
2 | import { loadYaml } from './utils';
|
3 |
|
4 | export type PathInfo = {
|
5 | store: string,
|
6 | path?: string,
|
7 | }
|
8 |
|
9 |
|
10 |
|
11 | export async function list(pathInfo: PathInfo) {
|
12 | const bucket = await getBucketFromPathInfo(pathInfo);
|
13 | const result = await bucket.listFiles(pathInfo.path);
|
14 | return result;
|
15 | }
|
16 |
|
17 | export async function copy(from: PathInfo, to: PathInfo) {
|
18 | const files = await list(from);
|
19 | const destBucket = await getBucketFromPathInfo(to);
|
20 |
|
21 | }
|
22 |
|
23 | export 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 |
|
31 | export 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 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const formatInfo = `'storename:path' or just 'storename'`;
|
44 |
|
45 | export 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 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | async function getBucketFromPathInfo(pathInfo: PathInfo): Promise<Bucket> {
|
65 | const bucketCfgs = await loadRawConfig();
|
66 |
|
67 |
|
68 | const rawCfg = bucketCfgs[pathInfo.store];
|
69 | return getBucket(rawCfg);
|
70 |
|
71 | }
|
72 |
|
73 |
|
74 | async function loadRawConfig() {
|
75 | let vdevBuckets: any = await loadYaml('vdev-buckets.yaml');
|
76 | const confs = vdevBuckets.buckets;
|
77 |
|
78 | for (let name in confs) {
|
79 | confs[name].name = name;
|
80 | }
|
81 | return confs;
|
82 | }
|
83 |
|
\ | No newline at end of file |