Skip to main content

omni_dev/cli/
browser.rs

1//! Browser bridge CLI commands.
2
3pub(crate) mod bridge;
4pub(crate) mod harvest;
5pub(crate) mod request;
6
7use std::path::Path;
8
9use anyhow::Result;
10use clap::{Parser, Subcommand};
11
12use crate::browser::auth;
13
14/// Resolves a thin-client session token for `request` / `harvest`.
15///
16/// Tries `--token-file` then `OMNI_BRIDGE_TOKEN` (via
17/// [`auth::resolve_existing_token`]); if neither is set, falls back to the
18/// daemon-written token file ([`crate::daemon::paths::token_path`]) when it
19/// exists, so clients work transparently against a daemon-hosted bridge. The
20/// original "no token" error is preserved when nothing resolves.
21pub(crate) fn resolve_client_token(token_file: Option<&Path>) -> Result<String> {
22    match auth::resolve_existing_token(token_file) {
23        Ok(token) => Ok(token),
24        Err(e) => {
25            if token_file.is_none() {
26                if let Ok(path) = crate::daemon::paths::token_path() {
27                    if path.exists() {
28                        return auth::resolve_existing_token(Some(&path));
29                    }
30                }
31            }
32            Err(e)
33        }
34    }
35}
36
37/// Browser bridge: drive authenticated requests through a browser tab.
38#[derive(Parser)]
39pub struct BrowserCommand {
40    /// The browser subcommand to execute.
41    #[command(subcommand)]
42    pub command: BrowserSubcommands,
43}
44
45/// Browser subcommands.
46#[derive(Subcommand)]
47pub enum BrowserSubcommands {
48    /// Bridge: run the server (`serve`) or send a request through it (`request`).
49    Bridge(bridge::BridgeCommand),
50}
51
52impl BrowserCommand {
53    /// Executes the browser command.
54    pub async fn execute(self) -> Result<()> {
55        match self.command {
56            BrowserSubcommands::Bridge(cmd) => cmd.execute().await,
57        }
58    }
59}