exec method
Run command.
The contents of katana.yaml and the arguments of the command are passed to context.
コマンドを実行します。
contextにkatana.yamlの内容やコマンドの引数が渡されます。
Implementation
@override
Future<void> exec(ExecContext context) async {
final args = context.args.where((arg) => !arg.startsWith("--")).toList();
final region = context.args.contains("--region");
final path = args.get(4, "");
if (path.isEmpty) {
error(
"[path] is not specified. Please enter [path] according to the following command.\r\nkatana code server schedule cloudflare [path] [--region]\r\n",
);
return;
}
if (!validateFilePath(path)) {
error(
"Invalid path: $path. Please enter a valid path according to the following command.\r\nkatana code server schedule cloudflare [path] [--region]\r\n\r\n([path] must be entered in snake_case; numbers and underscores cannot be used at the beginning or end of the path. Also, you can create directories by using /.)\r\n",
);
return;
}
if (region && !_checkCloudflareRegionWorker(context)) {
return;
}
label(
"Create a server code for the scheduler in Cloudflare Workers in `$directory/$path.ts`.",
);
final parentPath = path.parentPath();
if (parentPath.isNotEmpty) {
final parentDir = Directory("$directory/$parentPath");
if (!parentDir.existsSync()) {
await parentDir.create(recursive: true);
}
}
await CodeServerScheduleCloudflareCliCommand(region: region)
.generateDartCode("$directory/$path", path, ext: "ts");
final className = "${_cloudflareClassName(path)}Schedule";
await _registerCloudflareWorker(
path: path,
className: className,
region: region,
);
if (region) {
final projectId =
context.yaml.getAsMap("cloudflare").get("project_id", "");
final name = projectId.isNotEmpty ? projectId : "<project_id>";
label(
"Next steps for the region schedule `$className`. region用スケジュール`$className`の次の手順:\r\n"
"1. Add the cron to `cloudflare/$cloudflareRegionWranglerConfig`. `cloudflare/$cloudflareRegionWranglerConfig`にcronを追加してください。\r\n"
" \"triggers\": { \"crons\": [\"0 * * * *\"] }\r\n"
"2. Add the self service binding to `cloudflare/$cloudflareRegionWranglerConfig` (under `env.<flavor>` for each flavor). `cloudflare/$cloudflareRegionWranglerConfig`に自身へのService bindingを追加してください(flavorごとに`env.<flavor>`内)。\r\n"
" \"services\": [{ \"binding\": \"SELF\", \"service\": \"$name-region\" }]\r\n"
"3. Set the internal secret for each flavor. flavorごとに内部用secretを設定してください。\r\n"
" cd cloudflare && wrangler secret put MASAMUNE_INTERNAL_SECRET -c $cloudflareRegionWranglerConfig --env <flavor>",
);
}
}