diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-06-13 17:43:10 +0800 | 
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-06-13 18:03:32 +0800 | 
| commit | 9049027f96c7fd0b67c5761a92357fd3ceeb07c0 (patch) | |
| tree | 12bffae8c625a58a8f9174c0cce3262f25919a8d /deno | |
| parent | 4083cac63ee12deb6c32664031209de367a64b29 (diff) | |
| download | crupest-9049027f96c7fd0b67c5761a92357fd3ceeb07c0.tar.gz crupest-9049027f96c7fd0b67c5761a92357fd3ceeb07c0.tar.bz2 crupest-9049027f96c7fd0b67c5761a92357fd3ceeb07c0.zip | |
deno: use yargs in mail-relay.
Diffstat (limited to 'deno')
| -rw-r--r-- | deno/deno.json | 1 | ||||
| -rw-r--r-- | deno/deno.lock | 9 | ||||
| -rw-r--r-- | deno/mail-relay/aws/app.ts | 80 | ||||
| -rw-r--r-- | deno/mail-relay/deno.json | 1 | 
4 files changed, 48 insertions, 43 deletions
| diff --git a/deno/deno.json b/deno/deno.json index dbc6cac..f4859d1 100644 --- a/deno/deno.json +++ b/deno/deno.json @@ -13,6 +13,7 @@      "@std/testing": "jsr:@std/testing@^1.0.13",      "@std/dotenv": "jsr:@std/dotenv@^0.225.5",      "@std/fs": "jsr:@std/fs@^1.0.18", +    "yargs": "npm:yargs@^18.0.0"      "@types/yargs": "npm:@types/yargs@^17.0.33"    }  } diff --git a/deno/deno.lock b/deno/deno.lock index 0d8b4eb..0fc543b 100644 --- a/deno/deno.lock +++ b/deno/deno.lock @@ -7,7 +7,6 @@      "jsr:@std/assert@^1.0.13": "1.0.13",      "jsr:@std/async@^1.0.13": "1.0.13",      "jsr:@std/bytes@^1.0.5": "1.0.6", -    "jsr:@std/cli@^1.0.19": "1.0.19",      "jsr:@std/collections@^1.1.1": "1.1.1",      "jsr:@std/csv@^1.0.6": "1.0.6",      "jsr:@std/data-structures@^1.0.8": "1.0.8", @@ -36,6 +35,7 @@      "npm:@types/lodash@*": "4.17.17",      "npm:@types/mustache@*": "4.2.6",      "npm:@types/node@*": "22.15.15", +    "npm:@types/yargs@*": "17.0.33",      "npm:@types/yargs@^17.0.33": "17.0.33",      "npm:email-addresses@5": "5.0.0",      "npm:hono@^4.7.11": "4.7.11", @@ -76,9 +76,6 @@      "@std/bytes@1.0.6": {        "integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a"      }, -    "@std/cli@1.0.19": { -      "integrity": "b3601a54891f89f3f738023af11960c4e6f7a45dc76cde39a6861124cba79e88" -    },      "@std/collections@1.1.1": {        "integrity": "eff6443fbd9d5a6697018fb39c5d13d5f662f0045f21392d640693d0008ab2af"      }, @@ -1310,13 +1307,13 @@        "jsr:@std/io@~0.225.2",        "jsr:@std/path@^1.1.0",        "jsr:@std/testing@^1.0.13", -      "npm:@types/yargs@^17.0.33" +      "npm:@types/yargs@^17.0.33", +      "npm:yargs@18"      ],      "members": {        "mail-relay": {          "dependencies": [            "jsr:@db/sqlite@0.12", -          "jsr:@std/cli@^1.0.19",            "npm:@aws-sdk/client-s3@^3.821.0",            "npm:@aws-sdk/client-sesv2@^3.821.0",            "npm:@hono/zod-validator@0.7", diff --git a/deno/mail-relay/aws/app.ts b/deno/mail-relay/aws/app.ts index 685d7a9..05d93cd 100644 --- a/deno/mail-relay/aws/app.ts +++ b/deno/mail-relay/aws/app.ts @@ -1,9 +1,10 @@  import { join } from "@std/path"; -import { parseArgs } from "@std/cli";  import { z } from "zod";  import { Hono } from "hono";  import { zValidator } from "@hono/zod-validator";  import { FetchHttpHandler } from "@smithy/fetch-http-handler"; +// @ts-types="npm:@types/yargs" +import yargs from "yargs";  import { Logger } from "@crupest/base/log";  import { ConfigDefinition, ConfigProvider } from "@crupest/base/config"; @@ -254,7 +255,9 @@ async function listLives() {    const { logger, fetcher } = createAwsFetchOnlyServices();    const liveMails = await fetcher.listLiveMails();    logger.info(`Total ${liveMails.length}:`); -  logger.info(liveMails.join("\n")); +  if (liveMails.length !== 0) { +    logger.info(liveMails.join("\n")); +  }  }  async function recycleLives() { @@ -263,38 +266,43 @@ async function recycleLives() {  }  if (import.meta.main) { -  const args = parseArgs(Deno.args); - -  if (args._.length === 0) { -    throw new Error("You must specify a command."); -  } - -  const command = String(args._[0]); - -  switch (command) { -    case "sendmail": { -      const { logger, config } = createBaseServices(); -      await sendMail(logger, config.getInt("httpPort")); -      break; -    } -    case "list-lives": { -      await listLives(); -      break; -    } -    case "recycle-lives": { -      await recycleLives(); -      break; -    } -    case "serve": { -      serve(); -      break; -    } -    case "real-serve": { -      serve(true); -      break; -    } -    default: { -      throw new Error(command + " is not a valid command."); -    } -  } +  await yargs(Deno.args) +    .scriptName("mail-relay") +    .command({ +      command: "sendmail", +      describe: "send mail via this server's endpoint", +      handler: async (_argv) => { +        const { logger, config } = createBaseServices(); +        await sendMail(logger, config.getInt("httpPort")); +      }, +    }) +    .command({ +      command: "live", +      describe: "work with live mails", +      builder: (builder) => { +        return builder +          .command({ +            command: "list", +            describe: "list live mails", +            handler: listLives, +          }) +          .command({ +            command: "recycle", +            describe: "recycle all live mails", +            handler: recycleLives, +          }) +          .demandCommand(1, "One command must be specified."); +      }, +      handler: () => {}, +    }) +    .command({ +      command: "serve", +      describe: "start the http and smtp servers", +      builder: (builder) => builder.option("real", { type: "boolean" }), +      handler: (argv) => serve(argv.real), +    }) +    .demandCommand(1, "One command must be specified.") +    .help() +    .strict() +    .parse();  } diff --git a/deno/mail-relay/deno.json b/deno/mail-relay/deno.json index 22aae59..9105747 100644 --- a/deno/mail-relay/deno.json +++ b/deno/mail-relay/deno.json @@ -5,7 +5,6 @@      "compile": "deno compile -o out/crupest-relay -A aws/app.ts"    },    "imports": { -    "@std/cli": "jsr:@std/cli@^1.0.19",      "@aws-sdk/client-s3": "npm:@aws-sdk/client-s3@^3.821.0",      "@aws-sdk/client-sesv2": "npm:@aws-sdk/client-sesv2@^3.821.0",      "@db/sqlite": "jsr:@db/sqlite@^0.12.0", | 
