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,4 +1,4 @@
use std::sync::Arc;
use std::{iter, sync::Arc};
pub(crate) mod prelude {
#![allow(unused_imports)]
@ -32,7 +32,7 @@ pub(crate) mod prelude {
use prelude::*;
pub mod args;
pub use args::Parser;
pub use args::{AppendCmd, Args};
mod color;
pub use color::{CLI_ENABLE_COLOR, SHOULD_COLOR};
pub mod line;
@ -47,6 +47,43 @@ use crate::source::SourceFile;
pub const ASCII_WHITESPACE: &[char] = &['\t', '\n', '\x0C', '\r', ' '];
pub fn do_append(args: Arc<Args>, append_args: AppendCmd) -> Result<(), BoxDynError> {
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()
};
// Get what file that thing is defined in.
let def_path = get_where(&append_args.name, &filepath)?;
let mut opts = File::options();
opts.read(true)
.write(true)
.create(false)
.custom_flags(libc::O_CLOEXEC);
let source_file = SourceFile::open_from(Arc::from(def_path), opts)?;
let pri = get_highest_prio(&append_args.name, source_file.clone())?;
let new_pri = pri - 1;
let new_pri_line = get_next_prio_line(
source_file.clone(),
append_args.name.into(),
new_pri,
append_args.value.into(),
)?;
eprintln!("new_pri_line={new_pri_line}");
write_next_prio(source_file, new_pri_line)?;
Ok(())
}
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
pub struct DefinitionWithLocation {
pub file: Box<Path>,