cargo-rustapi 0.1.550

The official CLI tool for the RustAPI framework. Scaffold new projects, run development servers, and manage database migrations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Deployment Commands
//!
//! Generate deployment configurations and deploy to various platforms.

use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use indicatif::{ProgressBar, ProgressStyle};
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;
use std::time::Duration;

use crate::config::load_config;

/// Arguments for deployment commands
#[derive(Subcommand, Debug)]
pub enum DeployArgs {
    /// Deploy to RustAPI Cloud (managed hosting)
    #[cfg(feature = "cloud")]
    Cloud(CloudArgs),

    /// Check status of a RustAPI Cloud deployment
    #[cfg(feature = "cloud")]
    Status(DeployStatusArgs),

    /// Generate a Dockerfile for the project
    Docker(DockerArgs),

    /// Deploy to Fly.io
    Fly(FlyArgs),

    /// Deploy to Railway
    Railway(RailwayArgs),

    /// Deploy to Shuttle.rs
    Shuttle(ShuttleArgs),
}

#[cfg(feature = "cloud")]
#[derive(Args, Debug)]
pub struct DeployStatusArgs {
    /// Deploy ID returned by `deploy cloud`
    pub deploy_id: String,
}

#[cfg(feature = "cloud")]
#[derive(Args, Debug)]
pub struct CloudArgs {
    /// Project name (defaults to Cargo.toml package name)
    #[arg(short, long)]
    pub name: Option<String>,

    /// Do not wait for deployment to complete
    #[arg(long)]
    pub no_wait: bool,
}

#[derive(Args, Debug)]
pub struct DockerArgs {
    /// Output path for Dockerfile
    #[arg(short, long, default_value = "./Dockerfile")]
    pub output: PathBuf,

    /// Rust toolchain version
    #[arg(long, default_value = "1.78")]
    pub rust_version: String,

    /// Binary name (defaults to package name)
    #[arg(short, long)]
    pub binary: Option<String>,

    /// Port to expose
    #[arg(short, long, default_value = "8080")]
    pub port: u16,
}

#[derive(Args, Debug)]
pub struct FlyArgs {
    /// Application name
    #[arg(short, long)]
    pub app: Option<String>,

    /// Region to deploy to
    #[arg(short, long, default_value = "iad")]
    pub region: String,

    /// Initialize only (don't deploy)
    #[arg(long)]
    pub init_only: bool,
}

#[derive(Args, Debug)]
pub struct RailwayArgs {
    /// Project name
    #[arg(short, long)]
    pub project: Option<String>,

    /// Environment (production, staging)
    #[arg(short, long, default_value = "production")]
    pub environment: String,
}

#[derive(Args, Debug)]
pub struct ShuttleArgs {
    /// Project name
    #[arg(short, long)]
    pub project: Option<String>,

    /// Initialize only
    #[arg(long)]
    pub init_only: bool,
}

/// Execute deployment command
pub async fn deploy(args: DeployArgs) -> Result<()> {
    match args {
        #[cfg(feature = "cloud")]
        DeployArgs::Cloud(cloud_args) => deploy_cloud(cloud_args).await,
        #[cfg(feature = "cloud")]
        DeployArgs::Status(status_args) => deploy_status(status_args).await,
        DeployArgs::Docker(docker_args) => generate_dockerfile(docker_args).await,
        DeployArgs::Fly(fly_args) => deploy_fly(fly_args).await,
        DeployArgs::Railway(railway_args) => deploy_railway(railway_args).await,
        DeployArgs::Shuttle(shuttle_args) => deploy_shuttle(shuttle_args).await,
    }
}

async fn generate_dockerfile(args: DockerArgs) -> Result<()> {
    println!("🐳 Generating Dockerfile...");

    // Try to get package name from Cargo.toml
    let binary_name = args
        .binary
        .unwrap_or_else(|| get_package_name().unwrap_or_else(|_| "app".to_string()));

    let dockerfile = format!(
        r#"# Build stage
FROM rust:{rust_version}-slim as builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy manifests first for dependency caching
COPY Cargo.toml Cargo.lock* ./
COPY crates ./crates

# Build dependencies (this layer will be cached)
RUN mkdir src && echo "fn main() {{}}" > src/main.rs
RUN cargo build --release
RUN rm -rf src

# Copy actual source code
COPY . .

# Build the application
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy the binary from builder
COPY --from=builder /app/target/release/{binary_name} /usr/local/bin/app

# Expose port
EXPOSE {port}

# Set environment variables
ENV RUST_LOG=info
ENV PORT={port}

# Run the application
CMD ["app"]
"#,
        rust_version = args.rust_version,
        binary_name = binary_name,
        port = args.port
    );

    fs::write(&args.output, dockerfile).context("Failed to write Dockerfile")?;

    println!("✅ Dockerfile generated at: {}", args.output.display());
    println!();
    println!("Build and run with:");
    println!("  docker build -t myapp .");
    println!("  docker run -p {}:{} myapp", args.port, args.port);

    Ok(())
}

async fn deploy_fly(args: FlyArgs) -> Result<()> {
    println!("✈️  Deploying to Fly.io...");

    let app_name = args
        .app
        .unwrap_or_else(|| get_package_name().unwrap_or_else(|_| "rustapi-app".to_string()));

    // Generate fly.toml
    let fly_toml = format!(
        r#"# Fly.io configuration
# Generated by RustAPI CLI

app = "{app_name}"
primary_region = "{region}"

[build]
  dockerfile = "Dockerfile"

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0

[[vm]]
  memory = "256mb"
  cpu_kind = "shared"
  cpus = 1
"#,
        app_name = app_name,
        region = args.region
    );

    fs::write("fly.toml", &fly_toml).context("Failed to write fly.toml")?;

    println!("✅ fly.toml generated");

    if args.init_only {
        println!();
        println!("To deploy, run:");
        println!("  fly launch");
        println!("  fly deploy");
    } else {
        println!();
        println!("To complete deployment:");
        println!("  1. Install flyctl: curl -L https://fly.io/install.sh | sh");
        println!("  2. Login: fly auth login");
        println!("  3. Launch: fly launch");
        println!("  4. Deploy: fly deploy");
    }

    Ok(())
}

async fn deploy_railway(args: RailwayArgs) -> Result<()> {
    println!("🚂 Deploying to Railway...");

    let project_name = args
        .project
        .unwrap_or_else(|| get_package_name().unwrap_or_else(|_| "rustapi-app".to_string()));

    // Generate railway.toml
    let railway_toml = r#"# Railway configuration
# Generated by RustAPI CLI

[build]
builder = "dockerfile"
dockerfilePath = "Dockerfile"

[deploy]
numReplicas = 1
healthcheckPath = "/health"
healthcheckTimeout = 100
restartPolicyType = "on_failure"
restartPolicyMaxRetries = 3
"#
    .to_string();

    fs::write("railway.toml", &railway_toml).context("Failed to write railway.toml")?;

    println!("✅ railway.toml generated for: {}", project_name);
    println!();
    println!("To deploy:");
    println!("  1. Install Railway CLI: npm i -g @railway/cli");
    println!("  2. Login: railway login");
    println!("  3. Link project: railway link");
    println!("  4. Deploy: railway up");

    Ok(())
}

async fn deploy_shuttle(args: ShuttleArgs) -> Result<()> {
    println!("🚀 Setting up Shuttle.rs...");

    let project_name = args
        .project
        .unwrap_or_else(|| get_package_name().unwrap_or_else(|_| "rustapi-app".to_string()));

    // Generate Shuttle.toml
    let shuttle_toml = format!(
        r#"# Shuttle configuration
# Generated by RustAPI CLI

name = "{project_name}"
"#
    );

    fs::write("Shuttle.toml", &shuttle_toml).context("Failed to write Shuttle.toml")?;

    println!("✅ Shuttle.toml generated");
    println!();
    println!("⚠️  Note: Shuttle requires code modifications to use their runtime.");
    println!();
    println!("To deploy:");
    println!("  1. Install Shuttle CLI: cargo install cargo-shuttle");
    println!("  2. Login: cargo shuttle login");
    println!("  3. Init: cargo shuttle init");
    println!("  4. Deploy: cargo shuttle deploy");

    Ok(())
}

fn get_package_name() -> Result<String> {
    let cargo_toml = fs::read_to_string("Cargo.toml").context("Failed to read Cargo.toml")?;

    for line in cargo_toml.lines() {
        if line.starts_with("name") {
            if let Some(name) = line.split('=').nth(1) {
                return Ok(name.trim().trim_matches('"').to_string());
            }
        }
    }

    anyhow::bail!("Could not find package name in Cargo.toml")
}

// --- Cloud Deploy ---

#[cfg(feature = "cloud")]
#[derive(Deserialize)]
struct DeployResponse {
    deploy_id: String,
    status: String,
    #[serde(default)]
    url: Option<String>,
}

#[cfg(feature = "cloud")]
async fn deploy_cloud(args: CloudArgs) -> Result<()> {
    let config = load_config()?;

    let token = config
        .token
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Not logged in. Run `rustapi login` first."))?;

    let cloud_url = config
        .cloud_url
        .as_deref()
        .unwrap_or("https://api.rustapi.cloud")
        .trim_end_matches('/');

    let project_name = args
        .name
        .unwrap_or_else(|| get_package_name().unwrap_or_else(|_| "rustapi-app".to_string()));

    // Verify project uses RustAPI
    let cargo_toml =
        fs::read_to_string("Cargo.toml").context("Not in a Rust project (no Cargo.toml found)")?;

    if !cargo_toml.contains("rustapi") {
        println!("  ⚠️  This project doesn't appear to use RustAPI.");
        println!("  Continuing anyway...");
    }

    // Build
    println!("  🔨 Building {} (release)...", project_name);

    let build = std::process::Command::new("cargo")
        .args(["build", "--release"])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .context("Failed to start cargo build")?;

    let output = build
        .wait_with_output()
        .context("Failed to wait for cargo build")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(anyhow::anyhow!("Build failed:\n{}", stderr));
    }

    println!("  ✅ Build complete");

    // Find binary
    let binary_path = PathBuf::from("target/release")
        .join(&project_name)
        .with_extension(std::env::consts::EXE_SUFFIX);

    if !binary_path.exists() {
        return Err(anyhow::anyhow!(
            "Binary not found at {}. Make sure the package name matches the binary name.",
            binary_path.display()
        ));
    }

    // Package binary
    println!("  📦 Packaging...");
    let binary_data = fs::read(&binary_path).context("Failed to read binary")?;

    // Upload
    println!("  ☁️  Uploading to RustAPI Cloud...");

    let client = cloud_http_client()?;
    let form = reqwest::multipart::Form::new()
        .text("project_name", project_name.clone())
        .part(
            "binary",
            reqwest::multipart::Part::bytes(binary_data).file_name(format!("{}.bin", project_name)),
        );

    let deploy_resp: DeployResponse = client
        .post(format!("{}/deploy", cloud_url))
        .header("Authorization", format!("Bearer {}", token))
        .multipart(form)
        .send()
        .await
        .context("Failed to connect to RustAPI Cloud")?
        .json()
        .await
        .context("Invalid response from deploy endpoint")?;

    let deploy_id = deploy_resp.deploy_id;

    if args.no_wait {
        println!("  ✅ Deploy queued: {}", deploy_id);
        println!("  Check status: cargo rustapi deploy status {}", deploy_id);
        return Ok(());
    }

    // Poll for status
    let spinner = ProgressBar::new_spinner();
    spinner.set_style(ProgressStyle::with_template("  {spinner} Deploying...").unwrap());
    spinner.enable_steady_tick(Duration::from_millis(100));

    for _ in 0..120 {
        tokio::time::sleep(Duration::from_secs(3)).await;

        let status_resp: DeployResponse =
            match fetch_deploy_status(cloud_url, token, &deploy_id).await {
                Ok(body) => body,
                Err(_) => continue,
            };

        match status_resp.status.as_str() {
            "running" | "live" => {
                spinner.finish_and_clear();
                println!(
                    "  🚀 Deployed: {}",
                    status_resp.url.as_deref().unwrap_or("(url pending)")
                );
                return Ok(());
            }
            "failed" => {
                spinner.finish_with_message("Deploy failed");
                return Err(anyhow::anyhow!(
                    "Deployment failed. Check logs in the dashboard."
                ));
            }
            _ => continue,
        }
    }

    spinner.finish_with_message("Deploy timed out");
    println!("  Deploy ID: {} (still processing)", deploy_id);
    println!("  Check: cargo rustapi deploy status {}", deploy_id);

    Ok(())
}

#[cfg(feature = "cloud")]
fn cloud_http_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(Duration::from_secs(10))
        .build()
        .context("Failed to build HTTP client")
}

#[cfg(feature = "cloud")]
async fn fetch_deploy_status(
    cloud_url: &str,
    token: &str,
    deploy_id: &str,
) -> Result<DeployResponse> {
    let client = cloud_http_client()?;
    let response = client
        .get(format!("{}/deploy/{}/status", cloud_url, deploy_id))
        .header("Authorization", format!("Bearer {}", token))
        .send()
        .await
        .context("Failed to connect to RustAPI Cloud")?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return Err(anyhow::anyhow!(
            "Deploy status request failed ({}): {}",
            status,
            body
        ));
    }

    response
        .json::<DeployResponse>()
        .await
        .context("Invalid response from deploy status endpoint")
}

#[cfg(all(feature = "cloud", test))]
mod status_tests {
    use super::DeployResponse;

    #[test]
    fn deploy_response_matches_cloud_api_shape() {
        let json = r#"{"deploy_id":"d-1","status":"live","url":"http://127.0.0.1:30001"}"#;
        let parsed: DeployResponse = serde_json::from_str(json).expect("shape");
        assert_eq!(parsed.deploy_id, "d-1");
        assert_eq!(parsed.status, "live");
        assert_eq!(parsed.url.as_deref(), Some("http://127.0.0.1:30001"));
    }
}

#[cfg(feature = "cloud")]
async fn deploy_status(args: DeployStatusArgs) -> Result<()> {
    let config = load_config()?;

    let token = config
        .token
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Not logged in. Run `rustapi login` first."))?;

    let cloud_url = config
        .cloud_url
        .as_deref()
        .unwrap_or("https://api.rustapi.cloud")
        .trim_end_matches('/');

    let status = fetch_deploy_status(cloud_url, token, &args.deploy_id).await?;

    println!("  Deploy ID: {}", status.deploy_id);
    println!("  Status:    {}", status.status);
    if let Some(url) = status.url {
        println!("  URL:       {}", url);
    }

    Ok(())
}