refactor: now make cli more consistent

This commit is contained in:
robcholz
2026-02-07 18:15:34 -05:00
parent df08e45fae
commit 2ef4011335
4 changed files with 89 additions and 22 deletions

View File

@@ -48,7 +48,8 @@
3. [x] add an actual config file.
4. [x] set up the cli.
9. [ ] fix ui overlap, and consistency issue.
10. [ ] intensive integration test.
10. [ ] `clean_cache` to cleanup the cache.
11. [ ] intensive integration test.
## Publish

View File

@@ -11,7 +11,8 @@ use color_eyre::Result;
use dialoguer::Confirm;
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
use vibebox::tui::{AppState, VmInfo};
use vibebox::{
@@ -39,11 +40,11 @@ enum Command {
}
fn main() -> Result<()> {
init_tracing();
color_eyre::install()?;
let cwd = env::current_dir().map_err(|err| color_eyre::eyre::eyre!(err.to_string()))?;
init_tracing(&cwd);
let cli = Cli::parse();
let cwd = env::current_dir().map_err(|err| color_eyre::eyre::eyre!(err.to_string()))?;
tracing::info!(cwd = %cwd.display(), "starting vibebox cli");
if let Some(command) = cli.command {
return handle_command(command, &cwd, cli.config.as_deref());
@@ -249,13 +250,58 @@ fn format_last_active(value: Option<&str>) -> String {
format!("{} day{} ago", days, if days == 1 { "" } else { "s" })
}
fn init_tracing() {
fn init_tracing(cwd: &Path) {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let ansi = std::io::stderr().is_terminal() && env::var("VIBEBOX_LOG_NO_COLOR").is_err();
let _ = tracing_subscriber::fmt()
.with_env_filter(filter)
.with_target(false)
.with_ansi(ansi)
.with_writer(std::io::stderr)
.try_init();
let stderr_is_tty = std::io::stderr().is_terminal();
let ansi = stderr_is_tty && env::var("VIBEBOX_LOG_NO_COLOR").is_err();
let file = instance::ensure_instance_dir(cwd)
.ok()
.and_then(|instance_dir| {
let log_path = instance_dir.join("cli.log");
std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(log_path)
.ok()
});
if stderr_is_tty {
let stderr_layer = fmt::layer()
.with_target(false)
.with_ansi(ansi)
.without_time()
.with_writer(std::io::stderr)
.with_filter(LevelFilter::INFO);
let subscriber = tracing_subscriber::registry()
.with(filter)
.with(stderr_layer);
if let Some(file) = file {
let file_layer = fmt::layer()
.with_target(false)
.with_ansi(false)
.with_writer(file);
let _ = subscriber.with(file_layer).try_init();
} else {
let _ = subscriber.try_init();
}
} else {
let stderr_layer = fmt::layer()
.with_target(false)
.with_ansi(ansi)
.with_writer(std::io::stderr)
.with_filter(LevelFilter::INFO);
let subscriber = tracing_subscriber::registry()
.with(filter)
.with(stderr_layer);
if let Some(file) = file {
let file_layer = fmt::layer()
.with_target(false)
.with_ansi(false)
.with_writer(file);
let _ = subscriber.with(file_layer).try_init();
} else {
let _ = subscriber.try_init();
}
}
}

View File

@@ -5,6 +5,9 @@ use std::{
use color_eyre::Result;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt;
use tracing_subscriber::prelude::*;
use vibebox::{config, instance, vm, vm_manager};
@@ -40,11 +43,22 @@ fn main() -> Result<()> {
fn init_tracing() {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let ansi = std::io::stderr().is_terminal() && env::var("VIBEBOX_LOG_NO_COLOR").is_err();
let _ = tracing_subscriber::fmt()
.with_env_filter(filter)
.with_target(false)
.with_ansi(ansi)
.with_writer(io::stderr)
.try_init();
let stderr_is_tty = std::io::stderr().is_terminal();
let ansi = stderr_is_tty && env::var("VIBEBOX_LOG_NO_COLOR").is_err();
if stderr_is_tty {
let stderr_layer = fmt::layer()
.with_target(false)
.with_ansi(ansi)
.without_time()
.with_writer(io::stderr)
.with_filter(LevelFilter::INFO);
let _ = tracing_subscriber::registry().with(stderr_layer).try_init();
} else {
let stderr_layer = fmt::layer()
.with_target(false)
.with_ansi(ansi)
.with_writer(io::stderr)
.with_filter(filter);
let _ = tracing_subscriber::registry().with(stderr_layer).try_init();
}
}

View File

@@ -72,7 +72,6 @@ pub fn run_with_ssh(manager_conn: UnixStream) -> Result<(), Box<dyn std::error::
tracing::debug!(ssh_user = %ssh_user, "loaded instance config");
let _manager_conn = manager_conn;
tracing::debug!("waiting for vm ipv4");
wait_for_vm_ipv4(&instance_dir, Duration::from_secs(120))?;
let ip = load_or_create_instance_config(&instance_dir)?
@@ -247,7 +246,8 @@ fn wait_for_vm_ipv4(
) -> Result<(), Box<dyn std::error::Error>> {
let start = Instant::now();
let mut next_log_at = start + Duration::from_secs(10);
tracing::debug!("waiting for vm ipv4");
tracing::info!("waiting for vm ipv4");
let mut once_hint = false;
loop {
let config = load_or_create_instance_config(instance_dir)?;
if config.vm_ipv4.is_some() {
@@ -258,7 +258,13 @@ fn wait_for_vm_ipv4(
}
if Instant::now() >= next_log_at {
let waited = start.elapsed();
tracing::debug!("still waiting for vm ipv4, {}s elapsed", waited.as_secs(),);
if waited.as_secs() > 15 && !once_hint {
tracing::info!(
"if vibebox is just initialized in this directory, it might take up to 1 minutes depending on your machine, and then you can enjoy the speed vibecoding! go pack!"
);
once_hint = true;
}
tracing::info!("still waiting for vm ipv4, {}s elapsed", waited.as_secs(),);
next_log_at += Duration::from_secs(10);
}
thread::sleep(Duration::from_millis(200));