Skip to main content

semora_cli/
lib.rs

1//! The `semora` CLI: publish Markdown and HTML to a Semora server and
2//! wait for feedback from the page.
3//!
4//! The library exists for the binary and its tests; it is not a stable API.
5
6pub mod args;
7pub mod bundle;
8pub mod checkout;
9pub mod client;
10pub mod commands;
11pub mod config;
12pub mod credentials;
13pub mod docref;
14pub mod exit;
15pub mod output;
16pub mod project_link;
17pub mod remember;
18
19use clap::Parser;
20
21use crate::args::{Cli, Cmd};
22use crate::config::{Flags, Settings};
23
24/// Everything a command needs.
25pub struct Ctx {
26    pub settings: Settings,
27    pub json: bool,
28    pub store: credentials::Store,
29}
30
31impl Ctx {
32    pub fn client(&self) -> anyhow::Result<client::Client> {
33        client::Client::new(&self.settings)
34    }
35}
36
37/// Parses arguments, runs the command and returns the process exit code.
38pub async fn main_entry() -> i32 {
39    config::load_dotenv();
40    let cli = Cli::parse();
41    let env = |k: &str| std::env::var(k).ok();
42    let result = async {
43        let ctx = build_ctx(&cli, &env)?;
44        run(ctx, cli.cmd).await
45    }
46    .await;
47    match result {
48        Ok(code) => code,
49        Err(e) => {
50            match e.downcast_ref::<exit::Exit>() {
51                Some(x) => eprintln!("error: {}", x.message),
52                None => eprintln!("error: {e:#}"),
53            }
54            exit::code_of(&e)
55        }
56    }
57}
58
59fn build_ctx(cli: &Cli, env: &dyn Fn(&str) -> Option<String>) -> anyhow::Result<Ctx> {
60    let dir = config::config_dir(env);
61    let file = config::read_file_config(&dir)?;
62    let no_keyring = env("SEMORA_NO_KEYRING").is_some_and(|v| !v.is_empty() && v != "0");
63    let store = credentials::Store::new(&dir, !no_keyring);
64    let flags = Flags {
65        server: cli.server.clone(),
66        api_key: cli.api_key.clone(),
67    };
68    let settings = config::resolve(&flags, env, &file, store.default_server(), &|u| {
69        store.get(&config::origin(u))
70    })?;
71    Ok(Ctx {
72        settings,
73        json: cli.json,
74        store,
75    })
76}
77
78async fn run(ctx: Ctx, cmd: Cmd) -> anyhow::Result<i32> {
79    use commands::*;
80    match cmd {
81        Cmd::Push(a) => push::run(&ctx, a).await,
82        Cmd::Watch(a) => watch::run(&ctx, a).await,
83        Cmd::Pull(a) => pull::pull(&ctx, a).await,
84        Cmd::Status { path } => pull::status(&ctx, path).await,
85        Cmd::Wait(a) => wait::run(&ctx, a).await,
86        Cmd::Say {
87            doc,
88            text,
89            reply_to,
90        } => events::say(&ctx, &doc, &text, reply_to).await,
91        Cmd::Ls {
92            query,
93            tag,
94            kind,
95            project,
96            limit,
97        } => docs::ls(&ctx, query, tag, kind, project, limit).await,
98        Cmd::Show { doc } => docs::show(&ctx, &doc).await,
99        Cmd::Get {
100            doc,
101            path,
102            rev,
103            output,
104        } => docs::get(&ctx, &doc, path, rev, output).await,
105        Cmd::Tag { doc, changes } => docs::tag(&ctx, &doc, &changes).await,
106        Cmd::Rm { doc } => docs::rm(&ctx, &doc).await,
107        Cmd::Open { doc } => docs::open(&ctx, &doc).await,
108        Cmd::Whoami => account::whoami(&ctx).await,
109        Cmd::Login { no_browser, name } => login::login(&ctx, no_browser, name).await,
110        Cmd::Logout => login::logout(&ctx).await,
111        Cmd::Keys(k) => account::keys(&ctx, k).await,
112        Cmd::Project(p) => project::run(&ctx, p).await,
113        Cmd::Skill => skill::run(&ctx),
114    }
115}