Skip to content

Instantly share code, notes, and snippets.

@tylercritchlow
Created November 28, 2023 05:00
Show Gist options
  • Save tylercritchlow/6bf0c13a625e5d8c0bdffc22c2168e87 to your computer and use it in GitHub Desktop.
Save tylercritchlow/6bf0c13a625e5d8c0bdffc22c2168e87 to your computer and use it in GitHub Desktop.
[package]
name = "untitled"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
toml = "0.8.8"
serde = "1.0.136"
serde_derive = "1.0.136"
use serde_derive::Deserialize;
use std::fs::{self, File};
use std::io::{self, Write};
use std::process::exit;
use toml;
#[derive(Deserialize, Debug)]
struct Package {
remote: String,
name: String,
}
#[derive(Deserialize, Debug)]
struct Local {
home: String,
}
#[derive(Deserialize, Debug)]
struct Install {
steps: Vec<String>,
}
#[derive(Deserialize, Debug)]
struct Data {
package: Package,
local: Local,
install: Install,
}
fn main() {
let file_path = "/home/tyler/RustroverProjects/untitled/config.winch";
let contents = match fs::read_to_string(file_path) {
Ok(c) => c,
Err(e) => {
eprintln!("Could not read file {:?}", file_path);
eprintln!("{}", e);
exit(1);
}
};
let data: Data = match toml::from_str(&contents) {
Ok(d) => d,
Err(_) => {
eprintln!("Unable to load data from {:?}", file_path);
exit(1);
}
};
let script_file_name = format!("/home/tyler/RustroverProjects/untitled/install_{}.sh", data.package.name);
let mut script_file = match File::create(&script_file_name) {
Ok(file) => file,
Err(e) => {
eprintln!("Could not create script file {:?}", script_file_name);
eprintln!("{}", e);
exit(1);
}
};
writeln!(script_file, "#!/bin/bash").expect("Error writing to script file");
writeln!(script_file, "cd {} || mkdir ~/winch && cd ~/winch" , data.local.home).expect("Error writing to script file");
// Git clone command
writeln!(
script_file,
"echo 'Cloning repository: {}'",
data.package.remote
)
.expect("Error writing to script file");
writeln!(
script_file,
"git clone {} || exit 1",
data.package.remote
)
.expect("Error writing to script file");
writeln!(script_file, "cd {}", data.package.name).expect("Error writing to script file");
writeln!(script_file, "").expect("Error writing to script file");
for (idx, step) in data.install.steps.iter().enumerate() {
writeln!(script_file, "echo 'Running step {}'", idx + 1).expect("Error writing to script file");
writeln!(script_file, "{} || exit 1", step).expect("Error writing to script file");
writeln!(script_file, "").expect("Error writing to script file");
}
println!("Script file created at: {:?}", script_file_name);
}
@tylercritchlow
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment