-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect wich operating system the user uses (#6)
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
Showing
9 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
debug/ | ||
target/ | ||
|
||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM debian | ||
|
||
WORKDIR /app | ||
COPY ./target/release/depot ./ | ||
CMD ["./depot"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM fedora | ||
|
||
WORKDIR /app | ||
COPY ./target/release/depot ./ | ||
CMD ["./depot"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM ubuntu | ||
|
||
WORKDIR /app | ||
COPY ./target/release/depot ./ | ||
CMD ["./depot"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|