65 lines
1.9 KiB
Rust
65 lines
1.9 KiB
Rust
use std::io::{self, IsTerminal};
|
|
use std::path::Path;
|
|
use std::process::ExitCode;
|
|
use std::{error::Error as StdError, sync::Arc};
|
|
|
|
use append_override::source::SourceFile;
|
|
use clap::{ColorChoice, Parser as _};
|
|
use fs_err::File;
|
|
use fs_err::os::unix::fs::OpenOptionsExt;
|
|
|
|
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,
|
|
ColorChoice::Auto => io::stdin().is_terminal(),
|
|
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)?;
|
|
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 (pri, last_def_line) = append_override::get_highest_prio(&args.name, source_file.clone())?;
|
|
let new_pri = pri - 1;
|
|
|
|
eprintln!("{last_def_line}");
|
|
|
|
let new_pri_line = append_override::get_next_prio_line(
|
|
source_file.clone(),
|
|
args.name.into(),
|
|
last_def_line.clone(),
|
|
new_pri,
|
|
args.value.into(),
|
|
)?;
|
|
|
|
eprintln!("new_pri_line={new_pri_line}");
|
|
|
|
append_override::write_next_prio(source_file, last_def_line, new_pri_line.text())?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn main() -> ExitCode {
|
|
match main_wrapped() {
|
|
Ok(_) => ExitCode::SUCCESS,
|
|
Err(e) => {
|
|
eprintln!("append-override: error: {}", e);
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|