templateme/items/
util.rs

1use std::fs::{OpenOptions};
2use std::string::FromUtf8Error;
3use std::io::prelude::*;
4use std::process::Command;
5
6pub fn create_file(name: &str, content: &str) {
7    let mut file = OpenOptions::new().write(true).create(true).open(name).unwrap();
8    file.write_all(content.as_bytes()).unwrap();
9}
10
11pub fn get_git_name() -> Result<String, FromUtf8Error> {
12    let output = Command::new("git")
13                         .arg("config")
14                         .arg("user.name")
15                         .output()
16                         .expect("Failed to execute command");
17
18    let len = output.stdout.as_slice().len();
19    if len <= 1 {
20        panic!("No git user.name found.");
21    }
22    let len = len - 1;
23    let output = output.stdout.as_slice().get(..len).unwrap().to_vec();
24
25    String::from_utf8(output)
26}