about summary refs log tree commit diff
path: root/src/main.rs
blob: c15cc541b433a77f3616762d67fc09a239cede6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::path::PathBuf;

fn main() {
	// We don't need anything fancy here now
	if std::env::args().len() != 2 {
		eprintln!(
			"usage: popline <file>\n\
			removes the first line of a file,\
			writes it back to disk without that line"
		);
		std::process::exit(1);
	}

	// A panic is fine, really; it'll be caught while writing
	let file = PathBuf::from(std::env::args().nth(1).unwrap());
	let string = std::fs::read_to_string(&file).unwrap();
	let mut lines = string.lines();

	let line = match lines.next() {
		None => {
			std::process::exit(0);
		}
		Some(line) => line,
	};

	let write = lines.collect::<Vec<&str>>().join("\n");
	std::fs::write(file, write).unwrap();

	println!("{line}");
}