Preparing stuff

This commit is contained in:
Thomas Forgione 2020-11-22 18:58:20 +01:00
parent 7e9d69cd4c
commit 470c723fcf
2 changed files with 123 additions and 1 deletions

119
src/lib.rs Normal file
View File

@ -0,0 +1,119 @@
use std::fmt;
use std::process::Command;
/// A command to run.
pub struct CommandLine {
program: String,
args: Vec<String>,
}
impl CommandLine {
/// Creates a new command line.
pub fn new(program: &str, args: &[&str]) -> CommandLine {
CommandLine {
program: program.to_string(),
args: args.iter().map(|x| String::from(*x)).collect::<Vec<_>>(),
}
}
/// Creates the command from the command line.
pub fn command(self) -> Command {
let mut command = Command::new(self.program);
command.args(&self.args);
command
}
}
/// The supported package manager.
#[derive(Copy, Clone)]
pub enum PackageManager {
Apt,
Pacman,
}
impl PackageManager {
pub fn install<'a>(&self, package: &'a str) -> CommandLine {
match self {
PackageManager::Apt => CommandLine::new("sudo", &["apt", "install", package]),
PackageManager::Pacman => CommandLine::new("sudo", &["pacman", "-S", package]),
}
}
}
/// All the installable components.
#[derive(Copy, Clone)]
pub enum Component {
Sudo,
Git,
Zsh,
Dotfiles,
}
impl Component {
/// List all components.
pub fn all() -> Vec<Component> {
vec![
Component::Sudo,
Component::Git,
Component::Zsh,
Component::Dotfiles,
]
}
/// Installs a component.
pub fn install(self) -> Result<()> {
match self {
Component::Sudo => todo!(),
Component::Git => todo!(),
Component::Zsh => todo!(),
Component::Dotfiles => todo!(),
}
}
/// Returns the dependencies of each component.
pub fn dependencies(self) -> &'static [Component] {
match self {
Component::Sudo => &[],
Component::Git => &[Component::Sudo],
Component::Zsh => &[Component::Sudo],
Component::Dotfiles => &[Component::Git],
}
}
}
impl fmt::Display for Component {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Component::Sudo => write!(fmt, "root"),
Component::Git => write!(fmt, "git"),
Component::Zsh => write!(fmt, "zsh"),
Component::Dotfiles => write!(fmt, "dotfiles"),
}
}
}
/// The struct that holds the information about the install.
pub struct Installer;
impl Installer {
/// Creates a new installer.
pub fn new() -> Installer {
Installer
}
}
/// The error type of this library.
pub enum Error {}
impl fmt::Display for Error {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
Ok(())
}
}
pub type Result<T> = std::result::Result<T, Error>;
/// Runs the installer
pub fn main() -> Result<()> {
Ok(())
}

View File

@ -1,3 +1,6 @@
fn main() {
println!("Hello, world!");
if let Err(e) = tforgione_installer::main() {
eprintln!("installer crashed: {}", e);
std::process::exit(1);
}
}