Commit 9ce5a38b authored by pgd's avatar pgd
Browse files

implement accepting multiple lookup arguments

Previously, for lookup cli command, it is possible to set only a single
argument for looking up. Now, it is possible to set multiple arguments.

Related to: #173
parent 6be10f1d
Loading
Loading
Loading
Loading
+48 −32
Original line number Diff line number Diff line
@@ -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 " +
@@ -32,7 +32,7 @@ export const command = new Command()
    conflicts: ["raw", "compact"],
  })
  .option("-u, --user-agent <string>", "The custom User-Agent header value.")
  .action(async (options, url: string) => {
  .action(async (options, ...urls: string[]) => {
    const spinner = ora({
      text: "Looking up the object...",
      discardStdin: false,
@@ -88,8 +88,16 @@ export const command = new Command()
        privateKey: key.privateKey,
      });
    }
    spinner.text = "Initializing succeeded";
    spinner.succeed();

    for (let i = 0; i < urls.length; i++) {
      const url = urls[i];
      const spinnerForEachLookup = ora({
        text: "Looking up an object...",
        discardStdin: false,
      }).start();
      try {
      spinner.text = "Looking up the object...";
        const object = await lookupObject(
          url,
          {
@@ -98,7 +106,7 @@ export const command = new Command()
            userAgent: options.userAgent,
          },
        );
      spinner.succeed();

        if (object == null) {
          console.error("Failed to fetch the object.");
          if (authLoader == null) {
@@ -106,20 +114,28 @@ export const command = new Command()
              "It may be a private object.  Try with -a/--authorized-fetch.",
            );
          }
        Deno.exit(1);
          spinnerForEachLookup.text = `Failed to lookup: ${url}`;
          spinnerForEachLookup.fail();
          continue;
        }
        spinnerForEachLookup.succeed();
        if (options.raw) {
          printJson(await object.toJsonLd({ contextLoader }));
        } else if (options.compact) {
        printJson(await object.toJsonLd({ format: "compact", contextLoader }));
          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.log("===================");
        }
      } catch (_) {
      spinner.fail();
    } finally {
      await server?.close();
        spinnerForEachLookup.fail();
      }
    }
    await server?.close();
  });