fix: preserve CLI log output and avoid incidental migrations
This commit is contained in:
1 parent
7ea4860202
commit
cf9007fa06
7 files changed
+114
-81
No files matched your search
+29
-15
@@ -44,28 +44,42 @@ async fn show_log(matches: &ArgMatches) -> CliResult {
|
||||
require_instance(&instances_dir, instance)?;
|
||||
let path = resolve_log_path(&instances_dir, instance, file)?;
|
||||
|
||||
let lines = crate::instance::logs::files::read_log_file(&path);
|
||||
for line in &lines {
|
||||
println!("{}", line);
|
||||
}
|
||||
|
||||
// ghetto tail -f: re-read the whole file and print new lines.
|
||||
// not efficient, but log files are small and this is simple.
|
||||
if follow {
|
||||
let mut last_len = lines.len();
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
let new_lines = crate::instance::logs::files::read_log_file(&path);
|
||||
for line in new_lines.iter().skip(last_len) {
|
||||
println!("{}", line);
|
||||
}
|
||||
last_len = new_lines.len();
|
||||
use std::io::Write;
|
||||
if path.extension().is_some_and(|extension| extension == "gz") {
|
||||
return Err(io::Error::other("Cannot follow a compressed log").into());
|
||||
}
|
||||
let mut offset = 0;
|
||||
loop {
|
||||
{
|
||||
let bytes = read_new_log_bytes(&path, &mut offset)?;
|
||||
let mut output = io::stdout().lock();
|
||||
output.write_all(&bytes)?;
|
||||
output.flush()?;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
||||
for line in crate::instance::logs::files::read_log_file(&path) {
|
||||
println!("{line}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_new_log_bytes(path: &Path, offset: &mut u64) -> io::Result<Vec<u8>> {
|
||||
use std::io::{Read, Seek};
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
if file.metadata()?.len() < *offset {
|
||||
*offset = 0;
|
||||
}
|
||||
file.seek(io::SeekFrom::Start(*offset))?;
|
||||
let mut bytes = Vec::new();
|
||||
file.read_to_end(&mut bytes)?;
|
||||
*offset += bytes.len() as u64;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_log_path(
|
||||
instances_dir: &Path,
|
||||
instance: &str,
|
||||
|
||||
+38
-37
@@ -23,51 +23,52 @@ pub async fn init() {
|
||||
return;
|
||||
}
|
||||
|
||||
let instances_dir = crate::config::SETTINGS.read().paths.resolve_instances_dir();
|
||||
let meta_dir = crate::config::SETTINGS.read().paths.resolve_meta_dir();
|
||||
let config = crate::config::get_config_path().join("config.toml");
|
||||
if crate::layout_migration::is_needed(&instances_dir, &meta_dir) {
|
||||
if let Err(error) =
|
||||
crate::layout_migration::run(&instances_dir, &meta_dir, &config, |progress| {
|
||||
eprintln!(
|
||||
"{}: {} ({}/{})",
|
||||
progress.phase, progress.item, progress.current, progress.total
|
||||
);
|
||||
})
|
||||
{
|
||||
eprintln!("error: layout migration failed safely: {error}");
|
||||
if !matches!(matches.subcommand_name(), Some("account" | "version")) {
|
||||
let instances_dir = crate::config::SETTINGS.read().paths.resolve_instances_dir();
|
||||
let meta_dir = crate::config::SETTINGS.read().paths.resolve_meta_dir();
|
||||
let config = crate::config::get_config_path().join("config.toml");
|
||||
if crate::layout_migration::is_needed(&instances_dir, &meta_dir) {
|
||||
if let Err(error) =
|
||||
crate::layout_migration::run(&instances_dir, &meta_dir, &config, |progress| {
|
||||
eprintln!(
|
||||
"{}: {} ({}/{})",
|
||||
progress.phase, progress.item, progress.current, progress.total
|
||||
);
|
||||
})
|
||||
{
|
||||
eprintln!("error: layout migration failed safely: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
} else if let Err(error) = crate::layout_migration::initialize_new_layout(&meta_dir) {
|
||||
eprintln!("error: cannot initialize metadata layout: {error}");
|
||||
std::process::exit(1);
|
||||
} else if let Err(error) = crate::config::upgrade_config_file(&config) {
|
||||
eprintln!("warning: cannot upgrade launcher config: {error}");
|
||||
}
|
||||
} else if let Err(error) = crate::layout_migration::initialize_new_layout(&meta_dir) {
|
||||
eprintln!("error: cannot initialize metadata layout: {error}");
|
||||
std::process::exit(1);
|
||||
} else if let Err(error) = crate::config::upgrade_config_file(&config) {
|
||||
eprintln!("warning: cannot upgrade launcher config: {error}");
|
||||
}
|
||||
if crate::layout_migration::cache_rebuild_pending(&meta_dir) {
|
||||
let manager = crate::instance::InstanceManager::new(&instances_dir, &meta_dir);
|
||||
let instances = manager.load_all();
|
||||
for (index, instance) in instances.iter().enumerate() {
|
||||
eprintln!(
|
||||
"Rebuilding runtime cache: {} ({}/{})",
|
||||
instance.name,
|
||||
index + 1,
|
||||
instances.len()
|
||||
);
|
||||
if let Err(error) = manager.repair_runtime_cache(instance).await {
|
||||
if crate::layout_migration::cache_rebuild_pending(&meta_dir) {
|
||||
let manager = crate::instance::InstanceManager::new(&instances_dir, &meta_dir);
|
||||
let instances = manager.load_all();
|
||||
for (index, instance) in instances.iter().enumerate() {
|
||||
eprintln!(
|
||||
"error: runtime cache rebuild stopped safely for '{}': {error}",
|
||||
instance.name
|
||||
"Rebuilding runtime cache: {} ({}/{})",
|
||||
instance.name,
|
||||
index + 1,
|
||||
instances.len()
|
||||
);
|
||||
if let Err(error) = manager.repair_runtime_cache(instance).await {
|
||||
eprintln!(
|
||||
"error: runtime cache rebuild stopped safely for '{}': {error}",
|
||||
instance.name
|
||||
);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
if let Err(error) = crate::layout_migration::finish_cache_rebuild(&meta_dir) {
|
||||
eprintln!("error: cannot finish runtime cache migration: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
if let Err(error) = crate::layout_migration::finish_cache_rebuild(&meta_dir) {
|
||||
eprintln!("error: cannot finish runtime cache migration: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
let result = match matches.subcommand() {
|
||||
Some(("instance", sub_matches)) => instance::handle_instance(sub_matches).await,
|
||||
Some(("mod", sub_matches)) => content::handle_mod(sub_matches),
|
||||
|
||||
+1
-4
@@ -39,8 +39,6 @@ pub fn active_marker(active: bool) -> &'static str {
|
||||
if active { ">" } else { " " }
|
||||
}
|
||||
|
||||
// display width (not byte length): instance and pack names are often
|
||||
// non-ASCII, and padding by bytes misaligns every row below them.
|
||||
fn display_width(value: &str) -> usize {
|
||||
Span::raw(value).width()
|
||||
}
|
||||
@@ -66,8 +64,7 @@ fn render_row(row: &[String], widths: &[usize]) -> String {
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, width)| {
|
||||
// manual space padding: {:<width$} counts chars, not columns,
|
||||
// so it would under-pad multi-byte names.
|
||||
// Rust's width formatter counts characters rather than terminal cells.
|
||||
let value = row.get(index).map(String::as_str).unwrap_or("");
|
||||
let mut cell = String::from(value);
|
||||
cell.push_str(&" ".repeat(width.saturating_sub(display_width(value))));
|
||||
|
||||
@@ -1,6 +1,29 @@
|
||||
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
#[test]
|
||||
fn following_log_bytes_keeps_partial_lines_and_handles_truncation() {
|
||||
use super::read_new_log_bytes;
|
||||
use std::io::Write;
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("latest.log");
|
||||
std::fs::write(&path, b"abc").unwrap();
|
||||
let mut offset = 0;
|
||||
assert_eq!(read_new_log_bytes(&path, &mut offset).unwrap(), b"abc");
|
||||
let mut file = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(&path)
|
||||
.unwrap();
|
||||
file.write_all(b"def\nnext\n").unwrap();
|
||||
assert_eq!(
|
||||
read_new_log_bytes(&path, &mut offset).unwrap(),
|
||||
b"def\nnext\n"
|
||||
);
|
||||
assert!(read_new_log_bytes(&path, &mut offset).unwrap().is_empty());
|
||||
std::fs::write(&path, b"new\n").unwrap();
|
||||
assert_eq!(read_new_log_bytes(&path, &mut offset).unwrap(), b"new\n");
|
||||
}
|
||||
|
||||
use super::resolve_log_path;
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -44,3 +44,19 @@ fn formats_datetime_consistently() {
|
||||
let dt = Utc.with_ymd_and_hms(2024, 1, 2, 3, 4, 5).unwrap();
|
||||
assert_eq!(format_datetime(&dt), "2024-01-02 03:04:05 UTC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_padding_uses_cells_for_wide_and_combining_names() {
|
||||
let table = render_table(
|
||||
&["Name", "State"],
|
||||
&[
|
||||
vec!["界界界".to_owned(), "first".to_owned()],
|
||||
vec!["e\u{301}".to_owned(), "second".to_owned()],
|
||||
],
|
||||
);
|
||||
let rows = table.lines().skip(2).collect::<Vec<_>>();
|
||||
for row in rows {
|
||||
let start = row.find(['f', 's']).unwrap();
|
||||
assert_eq!(ratatui::text::Span::raw(&row[..start]).width(), 8);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -20,8 +20,7 @@ pub fn required_arg<'a>(matches: &'a ArgMatches, name: &str) -> Result<&'a str,
|
||||
.ok_or_else(|| io::Error::other(format!("missing required argument '{name}'")))
|
||||
}
|
||||
|
||||
// checks for instance.json rather than just the directory, since a folder
|
||||
// without config is just a sad empty directory pretending to be an instance
|
||||
// A directory without instance.json is not an instance.
|
||||
pub fn require_instance(instances_dir: &std::path::Path, name: &str) -> Result<(), io::Error> {
|
||||
crate::instance::manager::validate_name(name).map_err(io::Error::other)?;
|
||||
if !instances_dir.join(name).join("instance.json").exists() {
|
||||
|
||||
+6
-23
@@ -50,29 +50,12 @@ async fn fetch_supported_versions(
|
||||
client: &HttpClient,
|
||||
loader: ModLoader,
|
||||
) -> Result<HashSet<String>, Box<dyn std::error::Error>> {
|
||||
let versions: HashSet<String> = match loader {
|
||||
ModLoader::Vanilla => HashSet::new(),
|
||||
ModLoader::Fabric => crate::net::fabric::fetch_fabric_game_versions(client)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|version| version.id)
|
||||
.collect::<HashSet<_>>(),
|
||||
ModLoader::Forge => crate::net::forge::fetch_forge_game_versions(client)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|version| version.id)
|
||||
.collect::<HashSet<_>>(),
|
||||
ModLoader::NeoForge => crate::net::neoforge::fetch_neoforge_game_versions(client)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|version| version.id)
|
||||
.collect::<HashSet<_>>(),
|
||||
ModLoader::Quilt => crate::net::quilt::fetch_quilt_game_versions(client)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|version| version.id)
|
||||
.collect::<HashSet<_>>(),
|
||||
};
|
||||
let versions = crate::instance::loader::get_installer(loader)
|
||||
.get_game_versions(client)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|version| version.id)
|
||||
.collect();
|
||||
|
||||
Ok(versions)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user