Add cli
parent
fae57a7e3f
commit
8d37f60cd7
@ -0,0 +1,20 @@
|
||||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use cargo_toml::Manifest;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct Metadata {
|
||||
codename: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let manifest = Manifest::<Metadata>::from_path_with_metadata(PathBuf::from("Cargo.toml"))
|
||||
.expect("Failed to read manifest (Cargo.toml)");
|
||||
|
||||
if let Some(package) = manifest.package {
|
||||
if let Some(metadata) = package.metadata {
|
||||
println!("cargo:rustc-env=TOURMALINE_CODENAME={}", metadata.codename);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
use clap::Subcommand;
|
||||
|
||||
const VERSION: &str = concat!(
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (",
|
||||
env!("TOURMALINE_CODENAME"),
|
||||
")",
|
||||
);
|
||||
|
||||
#[derive(Debug, Clone, Parser)]
|
||||
#[clap(bin_name = "trm", name = "Tourmaline", version=VERSION, about= env!("CARGO_PKG_DESCRIPTION"), infer_subcommands = true)]
|
||||
pub struct Args {
|
||||
#[command(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Subcommand)]
|
||||
pub enum Command {
|
||||
/// Installs the system from the given config
|
||||
#[command()]
|
||||
InstallFromConfig(InstallFromConfigArgs),
|
||||
|
||||
/// Generates empty script files for the installation
|
||||
#[command()]
|
||||
GenerateScripts(GenerateScriptsArgs),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Parser)]
|
||||
pub struct InstallFromConfigArgs {
|
||||
/// The path to the json config file
|
||||
#[arg()]
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Parser)]
|
||||
pub struct GenerateScriptsArgs {
|
||||
/// The path to the folder where the scripts should be generated in
|
||||
#[arg()]
|
||||
pub path: PathBuf,
|
||||
}
|
@ -1,30 +1,45 @@
|
||||
use tourmaline::{
|
||||
tasks::{User, UsersConfig},
|
||||
TaskExecutor,
|
||||
};
|
||||
use args::{Args, Command, GenerateScriptsArgs, InstallFromConfigArgs};
|
||||
use clap::Parser;
|
||||
use miette::{Context, IntoDiagnostic};
|
||||
use tokio::{fs::OpenOptions, io::AsyncReadExt};
|
||||
use tourmaline::{config::Config, error::AppResult, generate_script_files, TaskExecutor};
|
||||
|
||||
mod args;
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> miette::Result<()> {
|
||||
async fn main() {
|
||||
color_eyre::install().unwrap();
|
||||
dotenv::dotenv().unwrap();
|
||||
let executor = TaskExecutor::default();
|
||||
let user_cfg = UsersConfig {
|
||||
users: vec![
|
||||
User {
|
||||
name: String::from("test"),
|
||||
password: String::from("password"),
|
||||
sudoer: false,
|
||||
shell: String::from("/bin/zsh"),
|
||||
},
|
||||
User {
|
||||
name: String::from("test2"),
|
||||
password: String::from("superpassword"),
|
||||
sudoer: true,
|
||||
shell: String::from("/bin/nu"),
|
||||
},
|
||||
],
|
||||
};
|
||||
executor.setup_users(&user_cfg).await?;
|
||||
let args = Args::parse();
|
||||
|
||||
match args.command {
|
||||
Command::InstallFromConfig(args) => install_from_config(args).await,
|
||||
Command::GenerateScripts(args) => generate_scripts(args).await,
|
||||
}
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn install_from_config(args: InstallFromConfigArgs) -> AppResult<()> {
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(args.path)
|
||||
.await
|
||||
.into_diagnostic()
|
||||
.context("Could not read file")?;
|
||||
let mut cfg_contents = String::new();
|
||||
file.read_to_string(&mut cfg_contents)
|
||||
.await
|
||||
.into_diagnostic()
|
||||
.context("Could not read file")?;
|
||||
let config: Config = serde_json::from_str(&cfg_contents)
|
||||
.into_diagnostic()
|
||||
.context("Could not parse config as JSON")?;
|
||||
|
||||
TaskExecutor::with_config(config)
|
||||
.install_from_config()
|
||||
.await
|
||||
}
|
||||
|
||||
Ok(())
|
||||
async fn generate_scripts(args: GenerateScriptsArgs) -> AppResult<()> {
|
||||
generate_script_files(args.path).await
|
||||
}
|
||||
|
Loading…
Reference in New Issue