src/main.rs 2.0 K raw
1
use clap::{Parser, Subcommand};
2
use std::path::PathBuf;
3
4
#[derive(Parser)]
5
#[command(name = "sipp", about = "Snippet manager — TUI, server, and CLI")]
6
struct Cli {
7
    /// Remote server URL (e.g. http://localhost:3000)
8
    #[arg(short, long, env = "SIPP_REMOTE_URL")]
9
    remote: Option<String>,
10
11
    /// API key for authenticated operations
12
    #[arg(short = 'k', long, env = "SIPP_API_KEY")]
13
    api_key: Option<String>,
14
15
    /// File path to create a snippet from
16
    #[arg(value_name = "FILE")]
17
    file: Option<PathBuf>,
18
19
    #[command(subcommand)]
20
    command: Option<Commands>,
21
}
22
23
#[derive(Subcommand)]
24
enum Commands {
25
    /// Start the web server
26
    Server {
27
        /// Port to listen on
28
        #[arg(short, long, default_value_t = 3000)]
29
        port: u16,
30
31
        /// Host to bind to
32
        #[arg(long, default_value = "localhost")]
33
        host: String,
34
    },
35
    /// Launch the interactive TUI
36
    Tui {
37
        /// Remote server URL (e.g. http://localhost:3000)
38
        #[arg(short, long, env = "SIPP_REMOTE_URL")]
39
        remote: Option<String>,
40
41
        /// API key for authenticated operations
42
        #[arg(short = 'k', long, env = "SIPP_API_KEY")]
43
        api_key: Option<String>,
44
    },
45
    /// Save remote URL and API key to config file
46
    Auth,
47
}
48
49
fn main() -> Result<(), Box<dyn std::error::Error>> {
50
    let cli = Cli::parse();
51
52
    match cli.command {
53
        Some(Commands::Server { port, host }) => {
54
            let rt = tokio::runtime::Runtime::new()?;
55
            rt.block_on(sipp_so::server::run(host, port));
56
        }
57
        Some(Commands::Tui { remote, api_key }) => {
58
            sipp_so::tui::run_interactive(remote, api_key)?;
59
        }
60
        Some(Commands::Auth) => {
61
            sipp_so::tui::run_auth()?;
62
        }
63
        None => {
64
            if let Some(file) = cli.file {
65
                sipp_so::tui::run_file_upload(cli.remote, cli.api_key, file)?;
66
            } else {
67
                sipp_so::tui::run_interactive(cli.remote, cli.api_key)?;
68
            }
69
        }
70
    }
71
72
    Ok(())
73
}