Skip to content

Commit 6e8f8c8

Browse files
committed
examples: add Share CLI example (PAN-8682)
1 parent a2d6017 commit 6e8f8c8

File tree

4 files changed

+168
-180
lines changed

4 files changed

+168
-180
lines changed

dev/run_all_examples.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
#!/usr/bin/env bash
22

33
set -e
4+
skip_items=("/node_modules/", "cli.mjs")
45

56
# Root directory
67
root_directory=$(pwd)
78

89
# Find all *.mjs files and run them with node
910
find . -type f -name '*.mjs' | while read -r file; do
11+
# Exclusions.
12+
skip=false
13+
for item in "${skip_items[@]}"; do
14+
if [[ $file == *"$item"* ]]; then
15+
skip=true
16+
break
17+
fi
18+
done
19+
if [ "$skip" = true ]; then
20+
continue
21+
fi
22+
1023
echo -e "\n\n--------------------------------------------------------------\nRunning: $file"
1124
node "$file"
1225
echo -e "\nFinish: $file\n--------------------------------------------------------------\n"

examples/share/cli.mjs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env node
2+
3+
// @ts-check
4+
5+
import fs from "node:fs/promises";
6+
import path from "node:path";
7+
import process from "node:process";
8+
9+
import { defineCommand, runMain } from "citty";
10+
import { PangeaConfig, Share, ShareService } from "pangea-node-sdk";
11+
12+
const main = defineCommand({
13+
meta: {
14+
name: "share",
15+
version: "0.0.0",
16+
description:
17+
"An example command line utility that creates an email+code, SMS+code, " +
18+
"or password-secured download/upload/editor share-link for a given " +
19+
"file or for each file in a given directory.",
20+
},
21+
args: {
22+
input: {
23+
type: "string",
24+
required: true,
25+
alias: "i",
26+
description: "Local path to upload.",
27+
},
28+
dest: {
29+
type: "string",
30+
default: "/",
31+
description: "Destination path in Share.",
32+
},
33+
email: {
34+
type: "string",
35+
description: "Email address to protect the share link with.",
36+
},
37+
phone: {
38+
type: "string",
39+
description: "Phone number to protect the share link with.",
40+
},
41+
password: {
42+
type: "string",
43+
description: "Password to protect the share link with.",
44+
},
45+
link_type: {
46+
type: "string",
47+
default: Share.LinkType.DOWNLOAD,
48+
description: "Type of link.",
49+
},
50+
},
51+
async run({ args }) {
52+
if (!args.email && !args.phone && !args.password) {
53+
throw new Error(
54+
"At least one of --email, --phone, or --password must be provided."
55+
);
56+
}
57+
58+
/** @type {Set<Share.Authenticator>} */
59+
const authenticators = new Set();
60+
if (args.password) {
61+
authenticators.add({
62+
auth_type: Share.AuthenticatorType.PASSWORD,
63+
auth_context: args.password,
64+
});
65+
}
66+
if (args.email) {
67+
authenticators.add({
68+
auth_type: Share.AuthenticatorType.EMAIL_OTP,
69+
auth_context: args.email,
70+
});
71+
}
72+
if (args.phone) {
73+
authenticators.add({
74+
auth_type: Share.AuthenticatorType.SMS_OTP,
75+
auth_context: args.phone,
76+
});
77+
}
78+
79+
const share = new ShareService(
80+
// @ts-expect-error
81+
process.env.PANGEA_SHARE_TOKEN,
82+
new PangeaConfig({ domain: process.env.PANGEA_DOMAIN })
83+
);
84+
85+
// Upload files.
86+
const files = (await fs.lstat(args.input)).isDirectory()
87+
? (await fs.readdir(args.input)).map((x) => path.resolve(args.input, x))
88+
: [args.input];
89+
const objectIds = new Set();
90+
for (const file of files) {
91+
const uploadResponse = await share.put(
92+
{
93+
path: `${args.dest}/${path.basename(file)}`,
94+
},
95+
{ file, name: "file" }
96+
);
97+
objectIds.add(uploadResponse.result.object.id);
98+
}
99+
100+
// Create share link.
101+
const linkResponse = await share.shareLinkCreate({
102+
links: [
103+
{
104+
targets: Array.from(objectIds),
105+
// @ts-expect-error
106+
link_type: args.link_type,
107+
authenticators: Array.from(authenticators),
108+
},
109+
],
110+
});
111+
console.log(linkResponse.result.share_link_objects[0].link);
112+
},
113+
});
114+
115+
runMain(main);

examples/share/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
22
"name": "share_examples",
3-
"version": "2.0.0",
4-
"description": "Pangea Share example",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8-
},
3+
"version": "0.0.0",
4+
"description": "Pangea Secure Share examples",
5+
"private": true,
96
"author": "Pangea",
107
"license": "MIT",
118
"dependencies": {
9+
"citty": "^0.1.6",
1210
"pangea-node-sdk": "file:../../packages/pangea-node-sdk"
1311
}
1412
}

0 commit comments

Comments
 (0)