refactor: inline run_headless
This commit is contained in:
94
src/main.rs
94
src/main.rs
@ -85,11 +85,45 @@ struct HeadlessArgs {
|
||||
|
||||
impl Runnable for HeadlessArgs {
|
||||
fn run(self: Box<Self>) -> Result<()> {
|
||||
run_headless(self.file_path, self.commands, self.script)
|
||||
let mut model = get_initial_model(&self.file_path)?;
|
||||
let mut cmds: Vec<String> = self.commands;
|
||||
if let Some(script_path) = self.script {
|
||||
let content = std::fs::read_to_string(&script_path)?;
|
||||
for line in content.lines() {
|
||||
let trimmed = line.trim();
|
||||
if !trimmed.is_empty() && !trimmed.starts_with("//") && !trimmed.starts_with('#') {
|
||||
cmds.push(trimmed.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct HelpArgs {}
|
||||
let mut exit_code = 0;
|
||||
for raw_cmd in &cmds {
|
||||
let parsed: command::Command = match serde_json::from_str(raw_cmd) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
let r = command::CommandResult::err(format!("JSON parse error: {e}"));
|
||||
println!("{}", serde_json::to_string(&r)?);
|
||||
exit_code = 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let result = command::dispatch(&mut model, &parsed);
|
||||
if !result.ok {
|
||||
exit_code = 1;
|
||||
}
|
||||
println!("{}", serde_json::to_string(&result)?);
|
||||
}
|
||||
|
||||
if let Some(path) = self.file_path {
|
||||
persistence::save(&mut model, &path)?;
|
||||
}
|
||||
|
||||
std::process::exit(exit_code);
|
||||
}
|
||||
}
|
||||
|
||||
struct HelpArgs;
|
||||
|
||||
impl Runnable for HelpArgs {
|
||||
fn run(self: Box<Self>) -> Result<()> {
|
||||
@ -121,7 +155,7 @@ fn parse_args(args: Vec<String>) -> Box<dyn Runnable> {
|
||||
import_path = args.get(i).map(PathBuf::from);
|
||||
}
|
||||
"--help" | "-h" => {
|
||||
return Box::new(HelpArgs {});
|
||||
return Box::new(HelpArgs);
|
||||
}
|
||||
arg if !arg.starts_with('-') => {
|
||||
file_path = Some(PathBuf::from(arg));
|
||||
@ -132,17 +166,17 @@ fn parse_args(args: Vec<String>) -> Box<dyn Runnable> {
|
||||
}
|
||||
|
||||
if !headless_cmds.is_empty() || headless_script.is_some() {
|
||||
return Box::new(HeadlessArgs {
|
||||
Box::new(HeadlessArgs {
|
||||
file_path,
|
||||
commands: headless_cmds,
|
||||
script: headless_script,
|
||||
});
|
||||
}
|
||||
|
||||
return Box::new(CmdLineArgs {
|
||||
})
|
||||
} else {
|
||||
Box::new(CmdLineArgs {
|
||||
file_path,
|
||||
import_path,
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn get_initial_model(file_path: &Option<PathBuf>) -> Result<Model> {
|
||||
@ -165,48 +199,6 @@ fn get_initial_model(file_path: &Option<PathBuf>) -> Result<Model> {
|
||||
};
|
||||
}
|
||||
|
||||
fn run_headless(
|
||||
file_path: Option<PathBuf>,
|
||||
inline_cmds: Vec<String>,
|
||||
script: Option<PathBuf>,
|
||||
) -> Result<()> {
|
||||
let mut model = get_initial_model(&file_path)?;
|
||||
let mut cmds: Vec<String> = inline_cmds;
|
||||
if let Some(script_path) = script {
|
||||
let content = std::fs::read_to_string(&script_path)?;
|
||||
for line in content.lines() {
|
||||
let trimmed = line.trim();
|
||||
if !trimmed.is_empty() && !trimmed.starts_with("//") && !trimmed.starts_with('#') {
|
||||
cmds.push(trimmed.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut exit_code = 0;
|
||||
for raw_cmd in &cmds {
|
||||
let parsed: command::Command = match serde_json::from_str(raw_cmd) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
let r = command::CommandResult::err(format!("JSON parse error: {e}"));
|
||||
println!("{}", serde_json::to_string(&r)?);
|
||||
exit_code = 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let result = command::dispatch(&mut model, &parsed);
|
||||
if !result.ok {
|
||||
exit_code = 1;
|
||||
}
|
||||
println!("{}", serde_json::to_string(&result)?);
|
||||
}
|
||||
|
||||
if let Some(path) = file_path {
|
||||
persistence::save(&mut model, &path)?;
|
||||
}
|
||||
|
||||
std::process::exit(exit_code);
|
||||
}
|
||||
|
||||
fn run_tui(
|
||||
model: Model,
|
||||
file_path: Option<PathBuf>,
|
||||
|
||||
Reference in New Issue
Block a user