devalang_wasm/tools/cli/
mod.rs

1#![cfg(feature = "cli")]
2
3mod commands;
4pub mod config;
5pub mod io;
6pub mod state;
7
8use anyhow::Result;
9use clap::{Parser, Subcommand};
10use commands::play::PlayCommand;
11use state::CliContext;
12
13#[derive(Parser, Debug)]
14#[command(name = "devalang")]
15#[command(
16    version,
17    about = "🦊 Devalang – A programming language for music and sound."
18)]
19pub struct Cli {
20    #[command(subcommand)]
21    command: Commands,
22}
23
24#[derive(Subcommand, Debug)]
25pub enum Commands {
26    /// Build and play deva file(s)
27    Play(PlayCommand),
28    /// Initialize a new project
29    Init(commands::init::InitCommand),
30    /// Builds deva file(s)
31    Build(commands::build::BuildCommand),
32    /// Check syntax without building
33    Check(commands::check::CheckCommand),
34    /// Manages addons (install, update, remove, list, discover)
35    Addon(commands::addon::AddonCommand),
36    /// Login to Devalang (authenticate with token)
37    Login {
38        /// Authentication token (optional, will prompt if not provided)
39        token: Option<String>,
40    },
41    /// Logout from Devalang
42    Logout,
43    /// Check authentication status
44    Me,
45    /// Manage telemetry settings
46    Telemetry {
47        #[command(subcommand)]
48        action: TelemetryAction,
49    },
50}
51
52#[derive(Subcommand, Debug)]
53pub enum TelemetryAction {
54    /// Enable telemetry
55    Enable,
56    /// Disable telemetry
57    Disable,
58    /// Show telemetry status
59    Status,
60}
61
62pub fn run() -> Result<()> {
63    let cli = Cli::parse();
64    let ctx = CliContext::new();
65    let runtime = tokio::runtime::Runtime::new()?;
66
67    runtime.block_on(async move {
68        match cli.command {
69            Commands::Play(command) => commands::play::execute(command, &ctx).await?,
70            Commands::Init(command) => command.execute(&ctx).await?,
71            Commands::Build(command) => command.execute(&ctx).await?,
72            Commands::Check(command) => command.execute(&ctx).await?,
73            Commands::Addon(command) => command.execute(&ctx).await?,
74            Commands::Login { token } => commands::auth::login(token).await?,
75            Commands::Logout => commands::auth::logout().await?,
76            Commands::Me => commands::auth::check_auth_status().await?,
77            Commands::Telemetry { action } => {
78                let logger = ctx.logger();
79                match action {
80                    TelemetryAction::Enable => {
81                        config::telemetry::enable_telemetry()?;
82                        logger.success("Telemetry enabled");
83                        logger.info("Thank you for helping us improve Devalang!");
84                    }
85                    TelemetryAction::Disable => {
86                        config::telemetry::disable_telemetry()?;
87                        logger.success("Telemetry disabled");
88                    }
89                    TelemetryAction::Status => {
90                        let status = config::telemetry::get_telemetry_status();
91                        logger.info(format!("Telemetry is currently: {}", status));
92                    }
93                }
94            }
95        }
96        Ok(())
97    })
98}