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::{
|
use args::{Args, Command, GenerateScriptsArgs, InstallFromConfigArgs};
|
||||||
tasks::{User, UsersConfig},
|
use clap::Parser;
|
||||||
TaskExecutor,
|
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")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> miette::Result<()> {
|
async fn main() {
|
||||||
color_eyre::install().unwrap();
|
color_eyre::install().unwrap();
|
||||||
dotenv::dotenv().unwrap();
|
dotenv::dotenv().unwrap();
|
||||||
let executor = TaskExecutor::default();
|
let args = Args::parse();
|
||||||
let user_cfg = UsersConfig {
|
|
||||||
users: vec![
|
match args.command {
|
||||||
User {
|
Command::InstallFromConfig(args) => install_from_config(args).await,
|
||||||
name: String::from("test"),
|
Command::GenerateScripts(args) => generate_scripts(args).await,
|
||||||
password: String::from("password"),
|
}
|
||||||
sudoer: false,
|
.unwrap();
|
||||||
shell: String::from("/bin/zsh"),
|
}
|
||||||
},
|
|
||||||
User {
|
async fn install_from_config(args: InstallFromConfigArgs) -> AppResult<()> {
|
||||||
name: String::from("test2"),
|
let mut file = OpenOptions::new()
|
||||||
password: String::from("superpassword"),
|
.read(true)
|
||||||
sudoer: true,
|
.open(args.path)
|
||||||
shell: String::from("/bin/nu"),
|
.await
|
||||||
},
|
.into_diagnostic()
|
||||||
],
|
.context("Could not read file")?;
|
||||||
};
|
let mut cfg_contents = String::new();
|
||||||
executor.setup_users(&user_cfg).await?;
|
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