Skip to content

Commit

Permalink
feat: detect wich operating system the user uses (#6)
Browse files Browse the repository at this point in the history
feat: create an enum of supported language

feat: detect if the user is on linux

feat: read the `/etc/os-releas` file to get the os

refactor: change `Result` type to have `DepotError` be silenced

test: print the os name

test: add bash script to quickly test on all os
  • Loading branch information
Rignchen authored Jul 11, 2024
1 parent 731b597 commit 7dc9d48
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
debug/
target/


# These are backup files generated by rustfmt
**/*.rs.bk

Expand Down
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, DepotError>;
pub fn unwrap_depot_error<T>(result: DepotResult<T>) -> 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<OperatingSystem> {
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),
}
}
}
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 7 additions & 0 deletions test/Dockerfiles-os/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# bash says that the binary is not found on alpine
FROM alpine

WORKDIR /app
COPY ./target/release/depot ./
CMD ["./depot"]

6 changes: 6 additions & 0 deletions test/Dockerfiles-os/Dockerfile.arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM archlinux/archlinux

WORKDIR /app
COPY ./target/release/depot ./
CMD ["./depot"]

6 changes: 6 additions & 0 deletions test/Dockerfiles-os/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM debian

WORKDIR /app
COPY ./target/release/depot ./
CMD ["./depot"]

6 changes: 6 additions & 0 deletions test/Dockerfiles-os/Dockerfile.fedora
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM fedora

WORKDIR /app
COPY ./target/release/depot ./
CMD ["./depot"]

6 changes: 6 additions & 0 deletions test/Dockerfiles-os/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ubuntu

WORKDIR /app
COPY ./target/release/depot ./
CMD ["./depot"]

36 changes: 36 additions & 0 deletions test/test_on_all_os.sh
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7dc9d48

Please sign in to comment.