export function getCommand()

in tools/awps-tunnel/server/commander.ts [55:121]


export function getCommand(appConfigPath: string, dbFile: string): Command {
  function configureHelpOptions(command: Command): Command {
    const helpText = "Show help details.";
    command.helpOption("-h, --help", helpText);
    return command;
  }
  const settings: Settings = fs.existsSync(appConfigPath) ? (JSON.parse(fs.readFileSync(appConfigPath, "utf-8")) as Settings) : { WebPubSub: {} };

  program.name(name).version(packageJson.version, "-v, --version", "Show the version number.").description(packageJson.description);
  const status = program
    .command("status")
    .description("Show the current configuration status.")
    .action(() => createStatusAction(settings));
  configureHelpOptions(status);
  const bind = program.command("bind").description("Bind configurations to the tool so that you don't need to specify them every time running the tool.");
  bind
    .option("-e, --endpoint [endpoint]", "Sepcify the Web PubSub service endpoint URL to connect to.")
    .option("--hub [hub]", "Specify the hub to connect to.")
    .option("-u, --upstream [upstream]", "Specify the upstream URL to connect to, URL scheme could be ommited, defaults to http, e.g. localhost:3000 or https://localhost:5001.")
    .option("--webviewPort [webviewPort]", "Specify the webview port to use. If not specified, it defaults to [upstreamPort+1000].")
    .option("--webviewHost [webviewHost]", "Specify the webview hostname to use. If not specified, it defaults to 127.0.0.1.")
    .option("-s, --subscription [subscription]", "Specify the subscriptionId your Web PubSub service belongs to. Specify subscriptionId and resource group to let the tool fetch hub settings for you.")
    .option(
      "-g, --resourceGroup [resourceGroup]",
      "Specify the resource group your Web PubSub service belongs to. Specify subscriptionId and resource group to let the tool fetch hub settings for you.",
    )
    .action((update) =>
      createBindAction(bind, settings, update, (updatedSettings) => {
        fs.writeFileSync(appConfigPath, JSON.stringify(updatedSettings, null, 2));
        printer.text(`Settings stored to ${appConfigPath}`);
        print(updatedSettings);
      }),
    );
  configureHelpOptions(bind);
  const run = program.command("run").description("Run the tool.");
  run
    .option(
      "-e, --endpoint [endpoint]",
      "Specify the Web PubSub service endpoint URL to connect to, you don't need to set it if WebPubSubConnectionString environment variable is set. If both are set, this option will be used.",
    )
    .option(
      "-c, --connection [connection]",
      "Specify the Web PubSub service connection string to connection to, this option overrides the --endpoint option or WebPubSubConnectionString environment variable.",
    )
    .option("--hub [hub]", "Specify the hub to connect to. If not specified, the hub value set with `awps-tunnel bind --hub [hub]` will be used.")
    .option(
      "-u, --upstream [upstream]",
      "Specify the upstream URL to connect to, URL scheme could be ommited, defaults to http, e.g. localhost:3000 or https://localhost:5001. If not specified, http://localhost:3000 will be used.",
    )
    .option("--webviewPort [webviewPort]", "Specify the webview port to use. If not specified, it defaults to [upstreamPort+1000].")
    .option("--webviewHost [webviewHost]", "Specify the webview hostname to use. If not specified, it defaults to 127.0.0.1.")
    .option("--noWebview", "Disable the webview")
    .option("-s, --subscription [subscription]", "Specify the subscriptionId your Web PubSub service belongs to. Specify subscriptionId and resource group to let the tool fetch hub settings for you.")
    .option(
      "-g, --resourceGroup [resourceGroup]",
      "Specify the resource group your Web PubSub service belongs to. Specify subscriptionId and resource group to let the tool fetch hub settings for you.",
    )
    .option("--verbose", "Enable verbose logs")
    .action((updated) => {
      createRunCommand(run, dbFile, settings, updated);
    });
  configureHelpOptions(run);
  configureHelpOptions(program);
  program.addHelpCommand(true, "Display help details for subcommand.");
  program.addHelpText("after", `\nYou could also set WebPubSubConnectionString environment variable if you don't want to configure endpoint.`);
  return program;
}