feat: argparse and dry-run.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
use std::path::Path;
|
||||
|
||||
pub struct ShellScript {
|
||||
lines: Vec<String>,
|
||||
}
|
||||
|
||||
impl ShellScript {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
lines: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_comment(&mut self, comment: &str) {
|
||||
self.lines.push(format!("# {}", comment));
|
||||
}
|
||||
|
||||
pub fn add_variable(&mut self, name: &str, value: &Path) {
|
||||
let escaped = escape_path(value);
|
||||
self.lines.push(format!("{}=\"{}\"", name, escaped));
|
||||
}
|
||||
|
||||
pub fn add_command(&mut self, command: &str) {
|
||||
self.lines.push(command.to_string());
|
||||
}
|
||||
|
||||
pub fn add_empty_line(&mut self) {
|
||||
self.lines.push(String::new());
|
||||
}
|
||||
|
||||
pub fn output(&self) -> String {
|
||||
self.lines.join("\n")
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_path(path: &Path) -> String {
|
||||
path.display().to_string().replace('"', r#"\""#).replace('$', r#"\$"#)
|
||||
}
|
||||
|
||||
pub fn print_shell_script_header() {
|
||||
println!("#!/bin/sh");
|
||||
println!("# Generated by bilibili-merge dry-run mode");
|
||||
println!();
|
||||
}
|
||||
|
||||
pub fn print_shell_script(script: &ShellScript) {
|
||||
println!("{}", script.output());
|
||||
}
|
||||
Reference in New Issue
Block a user