2026-01-29 13:52:54 +01:00
|
|
|
use std::ffi::OsStr;
|
2026-01-27 17:20:04 +01:00
|
|
|
use std::io::{self, IsTerminal};
|
2026-01-29 13:52:54 +01:00
|
|
|
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-02-02 11:34:52 +01:00
|
|
|
use dynix::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>> {
|
2026-02-02 11:34:52 +01:00
|
|
|
let args = dynix::Parser::parse();
|
2026-01-27 11:29:10 +01:00
|
|
|
|
2026-01-30 19:39:58 +01:00
|
|
|
dbg!(&args);
|
|
|
|
|
|
2026-02-02 11:34:52 +01:00
|
|
|
let success = dynix::CLI_ENABLE_COLOR.set(match args.color {
|
2026-01-27 11:29:10 +01:00
|
|
|
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);
|
2026-01-29 13:52:54 +01:00
|
|
|
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.
|
2026-02-02 11:34:52 +01:00
|
|
|
let def_path = dynix::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-02-02 11:34:52 +01:00
|
|
|
let pri = dynix::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-02-02 11:34:52 +01:00
|
|
|
let new_pri_line = dynix::get_next_prio_line(
|
2026-01-28 19:30:59 +01:00
|
|
|
source_file.clone(),
|
|
|
|
|
args.name.into(),
|
|
|
|
|
new_pri,
|
|
|
|
|
args.value.into(),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
eprintln!("new_pri_line={new_pri_line}");
|
2026-01-28 14:31:34 +01:00
|
|
|
|
2026-02-02 11:34:52 +01:00
|
|
|
dynix::write_next_prio(source_file, new_pri_line)?;
|
2026-01-28 14:31:34 +01:00
|
|
|
|
2026-01-27 11:29:10 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> ExitCode {
|
|
|
|
|
match main_wrapped() {
|
|
|
|
|
Ok(_) => ExitCode::SUCCESS,
|
|
|
|
|
Err(e) => {
|
2026-02-02 11:34:52 +01:00
|
|
|
eprintln!("dynix: error: {}", e);
|
2026-01-27 11:29:10 +01:00
|
|
|
ExitCode::FAILURE
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|