-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,466 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
AUTO_DRIVE_API_KEY=51c03eb7985c49359eb457a6b7e97b21 | ||
AUTO_DRIVE_URL=http://localhost:3000 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Examples | ||
|
||
This folder contains examples in how to use and interact with Auto-Drive backend. Though it's posible to interact directly with the API REST it's recommended to use `@autonomys/auto-drive` npm package. | ||
|
||
For using any of these examples, you'd need to have launched auto-drive backend. Read more about this in the root README of this repo. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { downloadFile } from "@autonomys/auto-drive"; | ||
import { Argument, Command } from "commander"; | ||
import { api } from "../services/api.js"; | ||
import { createWriteStream } from "fs"; | ||
|
||
export const downloadCommand = new Command("download") | ||
.addArgument(new Argument("cid", "The CID of the file to download")) | ||
.option("-p, --password <password>", "The password to use for the file") | ||
.option("-o, --output <output>", "The path to save the file to") | ||
.action(async (cid, options) => { | ||
const iterable = await downloadFile(api, cid, options.password); | ||
if (options.output) { | ||
await saveIterableInFile(iterable, options.output); | ||
} else { | ||
await logIterable(iterable); | ||
} | ||
}); | ||
|
||
const saveIterableInFile = async ( | ||
iterable: AsyncIterable<Uint8Array>, | ||
output: string | ||
) => { | ||
const writable = createWriteStream(output); | ||
for await (const chunk of iterable) { | ||
writable.write(chunk); | ||
} | ||
writable.close(); | ||
}; | ||
|
||
const logIterable = async (iterable: AsyncIterable<Uint8Array>) => { | ||
let buffer = Buffer.alloc(0); | ||
for await (const chunk of iterable) { | ||
buffer = Buffer.concat([buffer, chunk]); | ||
} | ||
console.log(buffer.toString("utf-8")); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Command, Option } from "commander"; | ||
import { api } from "../services/api.js"; | ||
import { getRoots, Scope } from "@autonomys/auto-drive"; | ||
|
||
export const listCommand = new Command("ls") | ||
.addOption( | ||
new Option( | ||
"-s, --scope <scope>", | ||
"The scope to list the roots for (user or global)" | ||
) | ||
.choices(["user", "global"]) | ||
.default("user") | ||
) | ||
.option("-l, --limit <limit>", "The limit of roots to list", "100") | ||
.option("-o, --offset <offset>", "The offset of roots to list", "0") | ||
.action(async (options) => { | ||
const roots = await getRoots(api, { | ||
scope: options.scope as Scope, | ||
limit: options.limit, | ||
offset: options.offset, | ||
}); | ||
|
||
roots.rows.forEach((root) => { | ||
console.log(`${root.headCid}: ${root.name}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Argument, Command } from "commander"; | ||
import { uploadFileFromFilepath } from "@autonomys/auto-drive"; | ||
import { api } from "../services/api.js"; | ||
|
||
export const publishCommand = new Command("publish") | ||
.addArgument(new Argument("path", "The path to the file to publish")) | ||
.option("-p, --password <password>", "The password to use for the file") | ||
.option("--no-compression", "Whether to not compress the file", false) | ||
.action(async (path, options) => { | ||
await uploadFileFromFilepath(api, path, { | ||
password: options.password, | ||
compression: options.compression, | ||
}) | ||
.promise.then((e) => { | ||
console.log(`Published file with CID: ${e.cid}`); | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { program } from "commander"; | ||
import { publishCommand } from "./commands/publish.js"; | ||
import { downloadCommand } from "./commands/download.js"; | ||
import { listCommand } from "./commands/list.js"; | ||
|
||
program | ||
.version("0.0.1") | ||
.description("Auto-Drive CLI") | ||
.addCommand(publishCommand) | ||
.addCommand(downloadCommand) | ||
.addCommand(listCommand); | ||
|
||
program.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createAutoDriveApi } from "@autonomys/auto-drive"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config({ | ||
path: ".env.test", | ||
}); | ||
|
||
const url = process.env.AUTO_DRIVE_URL; | ||
const apiKey = process.env.AUTO_DRIVE_API_KEY; | ||
|
||
if (!url || !apiKey) { | ||
throw new Error("AUTO_DRIVE_URL and AUTO_DRIVE_API_KEY must be set"); | ||
} | ||
|
||
export const api = createAutoDriveApi({ | ||
apiKey, | ||
url, | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2018", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true | ||
} | ||
} | ||
"compilerOptions": { | ||
"target": "es2018", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"outDir": "./dist" | ||
}, | ||
"include": ["./**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
Oops, something went wrong.