sastri 0.1.0

Scaffold multi-ecosystem packaging and CI/CD for Rust CLIs (Homebrew, Scoop, PyPI, npm, Hex, mise, asdf, …)
Documentation
//! Interactive wizard — asks for project/org/mechanism choices; never stores secrets.

use std::path::Path;

use anyhow::Result;
use dialoguer::{Confirm, Input, MultiSelect};

use crate::config::{Org, Paths, Project, Release, SastriConfig};
use crate::engine::registry;

pub fn run_wizard(config_path: &Path) -> Result<SastriConfig> {
    println!("sastri — multi-ecosystem packaging for Rust CLIs");
    println!(
        "Non-sensitive answers are saved to {}.\n",
        config_path.display()
    );
    println!("Secrets (NPM_TOKEN, HEX_API_KEY, …) are never written to the config.\n");

    let existing = if config_path.exists() {
        Some(SastriConfig::load(config_path)?)
    } else {
        None
    };

    let name: String = Input::new()
        .with_prompt("Project / crate name")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.name.clone())
                .unwrap_or_else(|| "mycli".into()),
        )
        .interact_text()?;

    let binary_name: String = Input::new()
        .with_prompt("Binary name")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.binary_name.clone())
                .unwrap_or_else(|| name.clone()),
        )
        .interact_text()?;

    let description: String = Input::new()
        .with_prompt("Short description")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.description.clone())
                .unwrap_or_else(|| format!("{binary_name} CLI")),
        )
        .interact_text()?;

    let github_owner: String = Input::new()
        .with_prompt("GitHub owner / org")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.github_owner.clone())
                .unwrap_or_else(|| "latentmeta".into()),
        )
        .interact_text()?;

    let github_repo: String = Input::new()
        .with_prompt("GitHub repository name")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.github_repo.clone())
                .unwrap_or_else(|| name.clone()),
        )
        .interact_text()?;

    let version: String = Input::new()
        .with_prompt("Current version (no leading v)")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.version.clone())
                .unwrap_or_else(|| "0.1.0".into()),
        )
        .interact_text()?;

    let cargo_manifest: String = Input::new()
        .with_prompt("Cargo.toml path for the CLI package")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.cargo_manifest.clone())
                .unwrap_or_else(|| "Cargo.toml".into()),
        )
        .interact_text()?;

    let license: String = Input::new()
        .with_prompt("License")
        .default(
            existing
                .as_ref()
                .map(|c| c.project.license.clone())
                .unwrap_or_else(|| "MIT".into()),
        )
        .interact_text()?;

    let homebrew_tap: String = Input::new()
        .with_prompt("Homebrew tap repo (owner/homebrew-tap)")
        .default(format!("{github_owner}/homebrew-tap"))
        .interact_text()?;

    let scoop_bucket: String = Input::new()
        .with_prompt("Scoop bucket repo")
        .default(format!("{github_owner}/scoop-bucket"))
        .interact_text()?;

    let npm_org: String = Input::new()
        .with_prompt("npm org (without @)")
        .default(github_owner.clone())
        .interact_text()?;

    let npm_package: String = Input::new()
        .with_prompt("npm package name")
        .default(format!("@{npm_org}/{binary_name}"))
        .interact_text()?;

    let pypi_package: String = Input::new()
        .with_prompt("PyPI package name")
        .default(binary_name.clone())
        .interact_text()?;

    let hex_package: String = Input::new()
        .with_prompt("Hex package name")
        .default(binary_name.clone())
        .interact_text()?;

    let asdf_plugin_repo: String = Input::new()
        .with_prompt("asdf plugin repo")
        .default(format!("{github_owner}/asdf-{binary_name}"))
        .interact_text()?;

    let mechs = registry::all();
    let labels: Vec<String> = mechs
        .iter()
        .map(|m| {
            let tag = if m.stable() { "" } else { " [stub]" };
            format!("{}{tag} — {}", m.id(), m.description())
        })
        .collect();

    let defaults: Vec<bool> = mechs
        .iter()
        .map(|m| {
            existing
                .as_ref()
                .map(|c| c.mechanism_enabled(m.id()))
                .unwrap_or(m.stable())
        })
        .collect();

    let selected = MultiSelect::new()
        .with_prompt("Enable install mechanisms (space to toggle, enter to confirm)")
        .items(&labels)
        .defaults(&defaults)
        .interact()?;

    let mut mechanisms = SastriConfig::default_mechanisms();
    for m in &mechs {
        mechanisms.insert(m.id().into(), false);
    }
    for idx in selected {
        mechanisms.insert(mechs[idx].id().into(), true);
    }

    let publish_crates = Confirm::new()
        .with_prompt("Publish to crates.io on v* tags?")
        .default(true)
        .interact()?;

    println!("\nSecrets you will need later (not stored in Sastri.toml):");
    println!("  - CRATES_IO_TOKEN (if crates.io publish)");
    println!("  - NPM_TOKEN (granular, bypass 2FA) if npm enabled");
    println!("  - HEX_API_KEY if Hex enabled");
    println!("  - PyPI Trusted Publisher (OIDC; no long-lived token)");
    println!("Run `sastri secrets` anytime for the checklist.\n");

    Ok(SastriConfig {
        project: Project {
            name,
            description,
            github_owner: github_owner.clone(),
            github_repo,
            binary_name,
            license,
            version,
            cargo_manifest,
            homepage: None,
        },
        org: Org {
            homebrew_tap: Some(homebrew_tap),
            scoop_bucket: Some(scoop_bucket),
            npm_org: Some(npm_org),
            npm_package: Some(npm_package),
            hex_package: Some(hex_package),
            pypi_package: Some(pypi_package),
            asdf_plugin_repo: Some(asdf_plugin_repo),
        },
        mechanisms,
        release: Release {
            platforms: Release::default().platforms,
            publish_crates_io: publish_crates,
            publish_sha256sums: true,
        },
        paths: Paths::default(),
    })
}