Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup multiple args #186

Merged
merged 6 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/import_map.g.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"@hongminhee/aitertools": "jsr:@hongminhee/aitertools@^0.6.0",
"@hugoalh/http-header-link": "jsr:@hugoalh/http-header-link@^1.0.2",
"@logtape/logtape": "jsr:@logtape/logtape@^0.8.0",
"@opentelemetry/api": "npm:@opentelemetry/api@^1.9.0",
"@opentelemetry/semantic-conventions": "npm:@opentelemetry/semantic-conventions@^1.27.0",
rudeh1253 marked this conversation as resolved.
Show resolved Hide resolved
"@phensley/language-tag": "npm:@phensley/language-tag@^1.9.0",
"@std/assert": "jsr:@std/assert@^0.226.0",
"@std/async": "jsr:@std/async@^1.0.5",
Expand Down
91 changes: 59 additions & 32 deletions cli/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";
import { printJson } from "./utils.ts";

export const command = new Command()
.arguments("<url:string>")
.arguments("<...urls:string>")
.description(
"Lookup an Activity Streams object by URL or the actor handle. " +
"The argument can be either a URL or an actor handle " +
Expand All @@ -32,7 +32,12 @@ export const command = new Command()
conflicts: ["raw", "compact"],
})
.option("-u, --user-agent <string>", "The custom User-Agent header value.")
.action(async (options, url: string) => {
.option(
"-s, --separator <separator:string>",
"Speicfy the separator between adjacent output object.",
{ default: "~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~" },
)
.action(async (options, ...urls: string[]) => {
const spinner = ora({
text: "Looking up the object...",
discardStdin: false,
Expand Down Expand Up @@ -88,38 +93,60 @@ export const command = new Command()
privateKey: key.privateKey,
});
}
try {
spinner.text = "Looking up the object...";
const object = await lookupObject(
url,
{
documentLoader: authLoader ?? documentLoader,
contextLoader,
userAgent: options.userAgent,
},
);
spinner.succeed();
if (object == null) {
console.error("Failed to fetch the object.");
if (authLoader == null) {
console.error(
"It may be a private object. Try with -a/--authorized-fetch.",
spinner.text = "Initializing succeeded";
spinner.succeed();

let success = true;
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
const spinnerForEachLookup = ora({
text: "Looking up an object...",
discardStdin: false,
}).start();
try {
const object = await lookupObject(
url,
{
documentLoader: authLoader ?? documentLoader,
contextLoader,
userAgent: options.userAgent,
},
);

if (object == null) {
console.error("Failed to fetch the object.");
if (authLoader == null) {
console.error(
"It may be a private object. Try with -a/--authorized-fetch.",
);
}
spinnerForEachLookup.text = `Failed to lookup: ${url}`;
spinnerForEachLookup.fail();
success = false;
continue;
}
spinnerForEachLookup.succeed();
if (options.raw) {
printJson(await object.toJsonLd({ contextLoader }));
} else if (options.compact) {
printJson(
await object.toJsonLd({ format: "compact", contextLoader }),
);
} else if (options.expand) {
printJson(await object.toJsonLd({ format: "expand", contextLoader }));
} else {
console.log(object);
}
Deno.exit(1);
}
if (options.raw) {
printJson(await object.toJsonLd({ contextLoader }));
} else if (options.compact) {
printJson(await object.toJsonLd({ format: "compact", contextLoader }));
} else if (options.expand) {
printJson(await object.toJsonLd({ format: "expand", contextLoader }));
} else {
console.log(object);
if (i < urls.length - 1) {
console.error(options.separator);
}
rudeh1253 marked this conversation as resolved.
Show resolved Hide resolved
} catch (_) {
spinnerForEachLookup.fail();
success = false;
}
rudeh1253 marked this conversation as resolved.
Show resolved Hide resolved
} catch (_) {
spinner.fail();
} finally {
await server?.close();
}
await server?.close();
if (!success) {
Deno.exit(1);
}
});
39 changes: 39 additions & 0 deletions cli/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cli/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"repository": {
"type": "git",
"url": "git+https://github.com/dahlia/fedify.git"
},
"dependencies": {
"@fedify/cli": "file:"
rudeh1253 marked this conversation as resolved.
Show resolved Hide resolved
}
}