dynix/src/main.rs

69 lines
2 KiB
Rust
Raw Normal View History

2026-01-27 09:20:04 -07:00
use std::{error::Error as StdError, sync::Arc};
use std::io::{self, IsTerminal};
2026-01-27 03:29:10 -07:00
use std::path::Path;
use std::process::ExitCode;
2026-01-27 09:20:04 -07:00
use append_override::source::SourceFile;
2026-01-27 03:29:10 -07:00
use clap::{ColorChoice, Parser as _};
2026-01-27 09:20:04 -07:00
use fs_err::File;
use fs_err::os::unix::fs::OpenOptionsExt;
2026-01-27 03:29:10 -07:00
fn main_wrapped() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
let args = append_override::Parser::parse();
let success = append_override::CLI_ENABLE_COLOR.set(match args.color {
ColorChoice::Always => true,
2026-01-27 09:20:04 -07:00
ColorChoice::Auto => io::stdin().is_terminal(),
2026-01-27 03:29:10 -07:00
ColorChoice::Never => false,
});
if cfg!(debug_assertions) {
success.expect("logic error in CLI_ENABLE_COLOR");
}
// FIXME: handle relative paths without leading ./
let filepath = Path::new(&args.file);
// Get what file that thing is defined in.
let def_path = append_override::get_where(&args.name, filepath)?;
2026-01-27 09:20:04 -07:00
let def_path = Arc::from(def_path);
let mut opts = File::options();
opts
.read(true)
.write(true)
.create(false)
.custom_flags(libc::O_CLOEXEC);
let source_file = SourceFile::open_from(Arc::clone(&def_path), opts)?;
let last_def_line = append_override::get_highest_prio(&args.name, source_file.clone())?;
let pri: i64 = {
let text = last_def_line.text();
let pat = "lib.mkOverride (";
let start = text.find(pat).unwrap();
let substr = &text[start..];
let substr = substr.strip_prefix(pat).unwrap();
let end_paren = substr.find(")").unwrap();
let final_num = &substr[..end_paren];
final_num.parse().unwrap()
};
2026-01-27 09:20:04 -07:00
eprintln!("{last_def_line}");
2026-01-27 03:29:10 -07:00
dbg!(&pri);
append_override::write_next_prio(source_file, last_def_line, pri, pri - 1)?;
2026-01-27 03:29:10 -07:00
Ok(())
}
fn main() -> ExitCode {
match main_wrapped() {
Ok(_) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("append-override: error: {}", e);
ExitCode::FAILURE
}
}
}