Skip to content

Commit

Permalink
✨ support more runtimes (#60)
Browse files Browse the repository at this point in the history
* ✨ support more runtimes

* 🐛 fix runtime paths

* 🐛 fix const reference

* 🐛 fix const reference
  • Loading branch information
pyaillet authored Jan 13, 2024
1 parent 5338428 commit e47a487
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ fn main() {
}

println!("cargo:rustc-env=DOGGY_GIT_INFO={}", git_describe);
println!("cargo:rustc-env=CARGO_PKG_REPOSITORY=https://github.com/pyaillet/doggy/issues/new");
}
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ async fn main() -> Result<()> {
cri::ConnectionConfig::socket(cri),
)),
(None, None) => None,
(Some(_), Some(_)) => {
return Err(eyre!("You should specify --docker or --cri but not both"))?;
}
(Some(_), Some(_)) => Err(eyre!("You should specify --docker or --cri but not both"))?,
}
};

Expand Down
62 changes: 52 additions & 10 deletions src/runtime/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ use crate::utils::get_or_not_found;
use super::{ContainerSummary, ImageSummary, NetworkSummary, VolumeSummary};

const DEFAULT_TIMEOUT: u64 = 120;
const DEFAULT_SOCKET_PATH: &str = "/var/run/docker.sock";
const DEFAULT_DOCKER_SOCKET_PATH: &str = "/var/run/docker.sock";

#[cfg(target_os = "macos")]
const DEFAULT_RANCHER_DESKTOP_SOCKET_PATH: &str = ".rd/docker.sock";
#[cfg(target_os = "macos")]
const DEFAULT_PODMAN_DESKTOP_SOCKET_PATH: &str =
".local/share/containers/podman/machine/podman.sock";
#[cfg(target_os = "macos")]
const DEFAULT_ORBSTACK_DESKTOP_SOCKET_PATH: &str = ".orbstack/run/docker.sock";

#[derive(Clone, Debug)]
pub enum ConnectionConfig {
Expand Down Expand Up @@ -67,12 +75,52 @@ impl Display for ConnectionConfig {
f.write_fmt(format_args!("unix://{}", socket_path))
}
ConnectionConfig::Socket(None) => {
f.write_fmt(format_args!("unix://{}", DEFAULT_SOCKET_PATH))
f.write_fmt(format_args!("unix://{}", DEFAULT_DOCKER_SOCKET_PATH))
}
}
}
}

#[cfg(target_os = "macos")]
fn test_other_default_socket(relative_path: &str) -> Result<ConnectionConfig> {
use eyre::eyre;
use std::path::Path;

let home_dir = env!("HOME");
let socket_path = Path::new(home_dir).join(relative_path);
let socket_path = socket_path
.into_os_string()
.into_string()
.map_err(|_| eyre!("Unable to convert path to string"))?;
fs::metadata(&socket_path).map(|_| Ok(ConnectionConfig::Socket(Some(socket_path))))?
}

#[cfg(target_os = "macos")]
pub fn detect_connection_config() -> Option<ConnectionConfig> {
let docker_host = env::var("DOCKER_HOST");
let docker_cert = env::var("DOCKER_CERT_PATH");
match (docker_host, docker_cert) {
(Ok(host), Ok(certs)) => {
log::debug!("Connect with ssl");
Some(ConnectionConfig::Ssl(host, certs))
}
(Ok(host), Err(_)) => {
log::debug!("Connect with http");
Some(ConnectionConfig::Http(host))
}
_ => {
log::debug!("Connect with socket");
fs::metadata(DEFAULT_DOCKER_SOCKET_PATH)
.map(|_| ConnectionConfig::Socket(Some(DEFAULT_DOCKER_SOCKET_PATH.to_string())))
.or_else(|_| test_other_default_socket(DEFAULT_RANCHER_DESKTOP_SOCKET_PATH))
.or_else(|_| test_other_default_socket(DEFAULT_PODMAN_DESKTOP_SOCKET_PATH))
.or_else(|_| test_other_default_socket(DEFAULT_ORBSTACK_DESKTOP_SOCKET_PATH))
.ok()
}
}
}

#[cfg(target_os = "linux")]
pub fn detect_connection_config() -> Option<ConnectionConfig> {
let docker_host = env::var("DOCKER_HOST");
let docker_cert = env::var("DOCKER_CERT_PATH");
Expand All @@ -87,7 +135,7 @@ pub fn detect_connection_config() -> Option<ConnectionConfig> {
}
_ => {
log::debug!("Connect with socket");
match fs::metadata(DEFAULT_SOCKET_PATH) {
match fs::metadata(DEFAULT_DOCKER_SOCKET_PATH) {
Ok(_) => Some(ConnectionConfig::default_socket()),
Err(_) => None,
}
Expand Down Expand Up @@ -181,13 +229,7 @@ impl Client {
let images = images
.iter()
.map(|i: &bollard::service::ImageSummary| ImageSummary {
id: i
.id
.to_string()
.split(':')
.last()
.unwrap_or("NOT_FOUND")
.to_string(),
id: i.id.split(':').last().unwrap_or("NOT_FOUND").to_string(),
name: get_or_not_found!(i.repo_tags.first()),
size: i.size,
created: i.created,
Expand Down

0 comments on commit e47a487

Please sign in to comment.