diff --git a/.gitignore b/.gitignore index 7bca086..e2e323d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ debug/ target/ - # These are backup files generated by rustfmt **/*.rs.bk diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..91a92c5 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,62 @@ +use std::io::Read; + +/// List of errors that the program can return. +#[derive(Debug)] +pub enum DepotError { + UnknownOperatingSystem, +} + +/// Result type wich wither take a type T or a DepotError. +pub type DepotResult = std::result::Result; +pub fn unwrap_depot_error(result: DepotResult) -> T { + match result { + Ok(value) => value, + Err(error) => { + eprintln!( + "{}", + match error { + DepotError::UnknownOperatingSystem => + "Unable to determine your current operating system.", + } + ); + std::process::exit(1); + } + } +} + +/// List of all supported operating systems. +#[derive(Debug)] +pub enum OperatingSystem { + Arch, + Alpine, + Debian, + Ubuntu, + Fedora, +} +impl OperatingSystem { + /// Get the currently running operating system. + pub fn current() -> DepotResult { + match std::env::consts::OS { + "linux" => { + let mut contents = String::new(); + std::fs::File::open("/etc/os-release") + .unwrap() + .read_to_string(&mut contents) + .unwrap(); + match contents + .split('\n') + .find(|line| line.starts_with("ID=")) + .unwrap() + { + "ID=arch" => Ok(OperatingSystem::Arch), + "ID=alpine" => Ok(OperatingSystem::Alpine), + "ID=debian" => Ok(OperatingSystem::Debian), + "ID=ubuntu" => Ok(OperatingSystem::Ubuntu), + "ID=fedora" => Ok(OperatingSystem::Fedora), + _ => Err(DepotError::UnknownOperatingSystem), + } + } + _ => Err(DepotError::UnknownOperatingSystem), + } + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..4e2fd33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,12 @@ +use depot::{unwrap_depot_error, OperatingSystem}; + +/// Main function of the program +/// Parse the command line arguments +/// Look for the package manager in +/// - the command line arguments +/// - the environment variables +/// - get the os name and deduce it from there fn main() { - println!("Hello, world!"); + let os = unwrap_depot_error(OperatingSystem::current()); + println!("OS: {:?}", os); } diff --git a/test/Dockerfiles-os/Dockerfile.alpine b/test/Dockerfiles-os/Dockerfile.alpine new file mode 100644 index 0000000..be611c7 --- /dev/null +++ b/test/Dockerfiles-os/Dockerfile.alpine @@ -0,0 +1,7 @@ +# bash says that the binary is not found on alpine +FROM alpine + +WORKDIR /app +COPY ./target/release/depot ./ +CMD ["./depot"] + diff --git a/test/Dockerfiles-os/Dockerfile.arch b/test/Dockerfiles-os/Dockerfile.arch new file mode 100644 index 0000000..2b7dd67 --- /dev/null +++ b/test/Dockerfiles-os/Dockerfile.arch @@ -0,0 +1,6 @@ +FROM archlinux/archlinux + +WORKDIR /app +COPY ./target/release/depot ./ +CMD ["./depot"] + diff --git a/test/Dockerfiles-os/Dockerfile.debian b/test/Dockerfiles-os/Dockerfile.debian new file mode 100644 index 0000000..3548b5f --- /dev/null +++ b/test/Dockerfiles-os/Dockerfile.debian @@ -0,0 +1,6 @@ +FROM debian + +WORKDIR /app +COPY ./target/release/depot ./ +CMD ["./depot"] + diff --git a/test/Dockerfiles-os/Dockerfile.fedora b/test/Dockerfiles-os/Dockerfile.fedora new file mode 100644 index 0000000..02a3a28 --- /dev/null +++ b/test/Dockerfiles-os/Dockerfile.fedora @@ -0,0 +1,6 @@ +FROM fedora + +WORKDIR /app +COPY ./target/release/depot ./ +CMD ["./depot"] + diff --git a/test/Dockerfiles-os/Dockerfile.ubuntu b/test/Dockerfiles-os/Dockerfile.ubuntu new file mode 100644 index 0000000..fde0dab --- /dev/null +++ b/test/Dockerfiles-os/Dockerfile.ubuntu @@ -0,0 +1,6 @@ +FROM ubuntu + +WORKDIR /app +COPY ./target/release/depot ./ +CMD ["./depot"] + diff --git a/test/test_on_all_os.sh b/test/test_on_all_os.sh new file mode 100755 index 0000000..911ae72 --- /dev/null +++ b/test/test_on_all_os.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# check if the script is executed from the parent directory of where it is located +script_dir=$(dirname "$(realpath "$0")") +if [ "$(dirname "$script_dir")" != "$(pwd)" ]; then + echo "Please execute the script from the parent directory of where it is located." + exit 1 +fi + +# get the list of all Dockerfiles from the Dockerfiles directory +dockerfiles=$(find "$script_dir/Dockerfiles-os" -type f -name "Dockerfile*") + +# get the end of each Dockerfile name (e.g., Dockerfile.ubuntu -> ubuntu) +os_names=$(echo "$dockerfiles" | sed -n 's/.*Dockerfile\.\(.*\)/\1/p') + +# build the rust project +cargo build --release + +# build all Docker images +for os_name in $os_names; do + docker build -t "test_on_all_os:$os_name" -f "$script_dir/Dockerfiles-os/Dockerfile.$os_name" . +done + +echo "" + +# run all docker containers and say the OS name +for os_name in $os_names; do + echo "test from: $os_name" + docker run --rm "test_on_all_os:$os_name" +done + +echo "" + +# remove all Docker images +docker rmi $(docker images -q test_on_all_os) +