bastion-toolkit 0.2.1

🏰 Bastion Security Toolkit - Industrial Grade Protection for Vibe Coders
Documentation
//! # Init - ăƒ†ăƒłăƒ—ăƒŹăƒŒăƒˆç”Ÿæˆăƒąă‚žăƒ„ăƒŒăƒ«
//!
//! ă‚»ă‚­ăƒ„ăƒȘăƒ†ă‚Łé–ąé€Łăźăƒ†ăƒłăƒ—ăƒŹăƒŒăƒˆăƒ•ă‚Ąă‚€ăƒ«ă‚’ăƒ—ăƒ­ă‚žă‚§ă‚Żăƒˆă«ć±•é–‹ă™ă‚‹ă€‚

use anyhow::{bail, Result};
use colored::*;
use std::fs;
use std::path::Path;
use crate::common::{self, ProjectType};

/// guardrails ăƒ†ăƒłăƒ—ăƒŹăƒŒăƒˆïŒˆăƒă‚€ăƒŠăƒȘă«ćŸ‹ă‚èŸŒăżïŒ‰
const GUARDRAILS_TEMPLATE: &str = include_str!("../templates/guardrails_template.rs");

/// secure_requirements ăƒ†ăƒłăƒ—ăƒŹăƒŒăƒˆïŒˆăƒă‚€ăƒŠăƒȘă«ćŸ‹ă‚èŸŒăżïŒ‰
const SECURE_REQUIREMENTS_TEMPLATE: &str = include_str!("../templates/secure_requirements.txt");

/// æŒ‡ćźšă•ă‚ŒăŸèš€èȘžăźăƒ†ăƒłăƒ—ăƒŹăƒŒăƒˆă‚’ç”Ÿæˆă™ă‚‹
pub fn run_init(language: &str) -> Result<()> {
    match language {
        "rust" => init_rust(),
        "python" => init_python(),
        "auto" => {
            println!("{}", "Detecting project type...".cyan());
            match common::detect_project_type() {
                ProjectType::Rust => init_rust(),
                ProjectType::Python => init_python(),
                ProjectType::Unknown => bail!("Could not auto-detect project type. Please specify 'rust' or 'python'."),
            }
        }
        _ => bail!(
            "Unknown language: '{}'. Supported: rust, python, auto",
            language
        ),
    }
}

fn init_rust() -> Result<()> {
    let target_path = "src/guardrails.rs";

    if Path::new(target_path).exists() {
        println!(
            "{} '{}' already exists. Skipping to avoid overwriting.",
            "Warning:".yellow().bold(),
            target_path
        );
        return Ok(());
    }

    if !Path::new("src").exists() {
        fs::create_dir_all("src")?;
    }

    fs::write(target_path, GUARDRAILS_TEMPLATE)?;

    println!("{} Generated '{}'", "✓".green().bold(), target_path);
    println!("");
    println!("  {} Add 'regex = \"1.10\"' to your Cargo.toml", "Next steps:".cyan().bold());
    println!("  Then use it in your code: 'mod guardrails; use guardrails::validate_input;'");

    Ok(())
}

fn init_python() -> Result<()> {
    let target_path = "secure_requirements.txt";

    if Path::new(target_path).exists() {
        println!(
            "{} '{}' already exists. Skipping to avoid overwriting.",
            "Warning:".yellow().bold(),
            target_path
        );
        return Ok(());
    }

    fs::write(target_path, SECURE_REQUIREMENTS_TEMPLATE)?;

    println!("{} Generated '{}'", "✓".green().bold(), target_path);
    println!("");
    println!("  {} Append to requirements.txt:", "Next steps:".cyan().bold());
    println!("  'cat secure_requirements.txt >> requirements.txt && pip install -r requirements.txt'");

    Ok(())
}