dynix/src/main.rs

73 lines
2 KiB
Rust
Raw Normal View History

use std::ffi::OsStr;
2026-01-27 17:20:04 +01:00
use std::io::{self, IsTerminal};
use std::iter;
use std::path::{Path, PathBuf};
2026-01-27 11:29:10 +01:00
use std::process::ExitCode;
2026-01-28 19:30:59 +01:00
use std::{error::Error as StdError, sync::Arc};
2026-01-27 11:29:10 +01:00
2026-01-27 17:20:04 +01:00
use append_override::source::SourceFile;
2026-01-27 11:29:10 +01:00
use clap::{ColorChoice, Parser as _};
2026-01-27 17:20:04 +01:00
use fs_err::File;
use fs_err::os::unix::fs::OpenOptionsExt;
2026-01-27 11:29:10 +01:00
fn main_wrapped() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
let args = append_override::Parser::parse();
2026-01-30 19:39:58 +01:00
dbg!(&args);
2026-01-27 11:29:10 +01:00
let success = append_override::CLI_ENABLE_COLOR.set(match args.color {
ColorChoice::Always => true,
2026-01-27 17:20:04 +01:00
ColorChoice::Auto => io::stdin().is_terminal(),
2026-01-27 11:29:10 +01:00
ColorChoice::Never => false,
});
if cfg!(debug_assertions) {
success.expect("logic error in CLI_ENABLE_COLOR");
}
let filepath = Path::new(&args.file);
let filepath: PathBuf = if filepath.is_relative() && !filepath.starts_with("./") {
iter::once(OsStr::new("./"))
.chain(filepath.iter())
.collect()
} else {
filepath.to_path_buf()
};
2026-01-27 11:29:10 +01:00
// Get what file that thing is defined in.
let def_path = append_override::get_where(&args.name, &filepath)?;
2026-01-30 19:39:58 +01:00
dbg!(&def_path);
2026-01-27 17:20:04 +01:00
let def_path = Arc::from(def_path);
let mut opts = File::options();
2026-01-28 19:30:59 +01:00
opts.read(true)
2026-01-27 17:20:04 +01:00
.write(true)
.create(false)
.custom_flags(libc::O_CLOEXEC);
let source_file = SourceFile::open_from(Arc::clone(&def_path), opts)?;
2026-01-30 19:39:58 +01:00
let pri = append_override::get_highest_prio(&args.name, source_file.clone())?;
2026-01-28 19:30:59 +01:00
let new_pri = pri - 1;
2026-01-27 17:20:04 +01:00
2026-01-28 19:30:59 +01:00
let new_pri_line = append_override::get_next_prio_line(
source_file.clone(),
args.name.into(),
new_pri,
args.value.into(),
)?;
eprintln!("new_pri_line={new_pri_line}");
2026-01-30 19:39:58 +01:00
append_override::write_next_prio(source_file, new_pri_line)?;
2026-01-27 11:29:10 +01:00
Ok(())
}
fn main() -> ExitCode {
match main_wrapped() {
Ok(_) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("append-override: error: {}", e);
ExitCode::FAILURE
}
}
}