factor out do_append in prep for delta subcommands

This commit is contained in:
Qyriad 2026-02-02 13:42:07 +01:00
parent 8477c73067
commit 9ae0630db4
8 changed files with 237 additions and 56 deletions

View file

@ -1,17 +1,11 @@
use std::ffi::OsStr;
use std::io::{self, IsTerminal};
use std::iter;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::{error::Error as StdError, sync::Arc};
use dynix::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 = dynix::Parser::parse();
let args = Arc::new(dynix::Args::parse());
dbg!(&args);
@ -24,40 +18,11 @@ fn main_wrapped() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
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()
use dynix::args::Subcommand::*;
match &args.subcommand {
Append(append_args) => dynix::do_append(args.clone(), append_args.clone())?,
};
// Get what file that thing is defined in.
let def_path = dynix::get_where(&args.name, &filepath)?;
dbg!(&def_path);
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 = dynix::get_highest_prio(&args.name, source_file.clone())?;
let new_pri = pri - 1;
let new_pri_line = dynix::get_next_prio_line(
source_file.clone(),
args.name.into(),
new_pri,
args.value.into(),
)?;
eprintln!("new_pri_line={new_pri_line}");
dynix::write_next_prio(source_file, new_pri_line)?;
Ok(())
}