From 470c723fcf8e44259f31367a6a3a5ee87701eee8 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sun, 22 Nov 2020 18:58:20 +0100 Subject: [PATCH] Preparing stuff --- src/lib.rs | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..73c396f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,119 @@ +use std::fmt; +use std::process::Command; + +/// A command to run. +pub struct CommandLine { + program: String, + args: Vec, +} + +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::>(), + } + } + + /// 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 { + 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 = std::result::Result; + +/// Runs the installer +pub fn main() -> Result<()> { + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..ffbb4d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ fn main() { - println!("Hello, world!"); + if let Err(e) = tforgione_installer::main() { + eprintln!("installer crashed: {}", e); + std::process::exit(1); + } }