Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod about;
pub mod init;
pub mod remove;
pub mod status;
pub mod update;
pub mod update_cli;
167 changes: 167 additions & 0 deletions cli/src/commands/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
use anyhow::Result;
use colored::Colorize;
use std::path::PathBuf;

use crate::config::DevTrailConfig;
use crate::manifest::DistManifest;
use crate::utils;

/// Expected directories inside .devtrail/
const EXPECTED_DIRS: &[&str] = &[
"00-governance",
"01-requirements",
"02-design/decisions",
"03-implementation",
"04-testing",
"05-operations/incidents",
"05-operations/runbooks",
"06-evolution/technical-debt",
"07-ai-audit/agent-logs",
"07-ai-audit/decisions",
"07-ai-audit/ethical-reviews",
"templates",
];

/// Expected files (relative to project root)
const EXPECTED_FILES: &[(&str, &str)] = &[
(".devtrail/config.yml", "config.yml"),
(".devtrail/dist-manifest.yml", "dist-manifest.yml"),
("DEVTRAIL.md", "DEVTRAIL.md"),
];

/// Document type prefixes for counting
const DOC_TYPES: &[&str] = &["ADR", "AIDEC", "AILOG", "ETH", "INC", "REQ", "TDE", "TES"];

pub fn run(path: &str) -> Result<()> {
let target = PathBuf::from(path)
.canonicalize()
.unwrap_or_else(|_| PathBuf::from(path));

let devtrail_dir = target.join(".devtrail");

// Phase 1: Check installation
if !devtrail_dir.exists() {
utils::info(&format!(
"DevTrail is not installed in {}",
target.display()
));
utils::info("Run 'devtrail init' to initialize DevTrail in this directory.");
return Ok(());
}

// Phase 2: Header
let version = load_version(&target);
let language = load_language(&target);

println!();
println!("{}", "DevTrail Status".bold());
println!(" {} {}", "Path:".dimmed(), target.display());
println!(" {} {}", "Version:".dimmed(), version);
println!(" {} {}", "Language:".dimmed(), language);

// Phase 2: Structure check
println!();
println!("{}", "Structure".bold());

for dir in EXPECTED_DIRS {
let dir_path = devtrail_dir.join(dir);
if dir_path.exists() {
utils::success(&format!("{dir}/"));
} else {
println!(
"{} {} {}",
"!".yellow().bold(),
format!("{dir}/").yellow(),
"(missing)".dimmed()
);
}
}

for &(rel_path, label) in EXPECTED_FILES {
let file_path = target.join(rel_path);
if file_path.exists() {
utils::success(label);
} else {
println!(
"{} {} {}",
"!".yellow().bold(),
label.yellow(),
"(missing)".dimmed()
);
}
}

// Phase 3: Documentation statistics
let counts = count_documents(&devtrail_dir);
let total: usize = counts.iter().map(|(_, c)| c).sum();

println!();
println!("{}", "Documentation".bold());
println!(" {:<7} {:>5}", "Type", "Count");
println!(" {:<7} {:>5}", "───────", "─────");
for (doc_type, count) in &counts {
println!(" {:<7} {:>5}", doc_type, count);
}
println!(" {:<7} {:>5}", "───────", "─────");
println!(" {:<7} {:>5}", "Total", total);
println!();

Ok(())
}

fn load_version(project_root: &std::path::Path) -> String {
let manifest_path = project_root.join(".devtrail/dist-manifest.yml");
match DistManifest::load(&manifest_path) {
Ok(m) => m.version,
Err(_) => {
utils::warn("Could not read dist-manifest.yml");
"unknown".to_string()
}
}
}

fn load_language(project_root: &std::path::Path) -> String {
match DevTrailConfig::load(project_root) {
Ok(c) => c.language,
Err(_) => {
utils::warn("Could not read config.yml");
"unknown".to_string()
}
}
}

fn count_documents(devtrail_dir: &std::path::Path) -> Vec<(&'static str, usize)> {
let files = walk_files(devtrail_dir);
DOC_TYPES
.iter()
.map(|&doc_type| {
let prefix = format!("{}-", doc_type);
let count = files
.iter()
.filter(|p| {
utils::is_user_document(p)
&& p.file_name()
.and_then(|n| n.to_str())
.map(|n| n.starts_with(&prefix))
.unwrap_or(false)
})
.count();
(doc_type, count)
})
.collect()
}

fn walk_files(dir: &std::path::Path) -> Vec<PathBuf> {
let mut files = Vec::new();
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
files.extend(walk_files(&path));
} else {
files.push(path);
}
}
}
files
}
7 changes: 7 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ enum Commands {
#[arg(long)]
full: bool,
},
/// Show DevTrail installation status and documentation statistics
Status {
/// Target directory (default: current directory)
#[arg(default_value = ".")]
path: String,
},
/// Show version, author, and license information
About,
}
Expand All @@ -52,6 +58,7 @@ fn main() {
Commands::Update => commands::update::run(),
Commands::UpdateCli => commands::update_cli::run(),
Commands::Remove { full } => commands::remove::run(full),
Commands::Status { path } => commands::status::run(&path),
Commands::About => commands::about::run(),
};

Expand Down
98 changes: 98 additions & 0 deletions cli/tests/status_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

#[test]
fn test_status_not_installed() {
let dir = TempDir::new().unwrap();

let mut cmd = Command::cargo_bin("devtrail").unwrap();
cmd.arg("status")
.arg(dir.path().to_str().unwrap())
.assert()
.success()
.stdout(predicate::str::contains("not installed"));
}

#[test]
fn test_status_with_minimal_install() {
let dir = TempDir::new().unwrap();
let devtrail = dir.path().join(".devtrail");
std::fs::create_dir_all(&devtrail).unwrap();

// Create minimal config and manifest
std::fs::write(
devtrail.join("config.yml"),
"language: es\n",
)
.unwrap();
std::fs::write(
devtrail.join("dist-manifest.yml"),
"version: \"2.1.0\"\ndescription: test\nfiles: []\n",
)
.unwrap();

// Create some fake documents
let reqs_dir = devtrail.join("01-requirements");
std::fs::create_dir_all(&reqs_dir).unwrap();
std::fs::write(
reqs_dir.join("REQ-2025-01-01-001-test.md"),
"# Test requirement",
)
.unwrap();
std::fs::write(
reqs_dir.join("REQ-2025-01-02-002-another.md"),
"# Another",
)
.unwrap();

let logs_dir = devtrail.join("07-ai-audit/agent-logs");
std::fs::create_dir_all(&logs_dir).unwrap();
std::fs::write(
logs_dir.join("AILOG-2025-03-01-001-session.md"),
"# Log",
)
.unwrap();

let mut cmd = Command::cargo_bin("devtrail").unwrap();
cmd.arg("status")
.arg(dir.path().to_str().unwrap())
.assert()
.success()
.stdout(
predicate::str::contains("2.1.0")
.and(predicate::str::contains("es"))
.and(predicate::str::contains("REQ"))
.and(predicate::str::contains("AILOG")),
);
}

#[test]
fn test_status_incomplete_structure() {
let dir = TempDir::new().unwrap();
let devtrail = dir.path().join(".devtrail");

// Create only some directories
std::fs::create_dir_all(devtrail.join("00-governance")).unwrap();
std::fs::create_dir_all(devtrail.join("01-requirements")).unwrap();
// Intentionally skip other directories

let mut cmd = Command::cargo_bin("devtrail").unwrap();
cmd.arg("status")
.arg(dir.path().to_str().unwrap())
.assert()
.success()
.stdout(predicate::str::contains("missing"));
}

#[test]
fn test_status_explicit_path_argument() {
let dir = TempDir::new().unwrap();

let mut cmd = Command::cargo_bin("devtrail").unwrap();
cmd.arg("status")
.arg(dir.path().to_str().unwrap())
.assert()
.success()
.stdout(predicate::str::contains("not installed"));
}