Skip to content

Commit

Permalink
add args
Browse files Browse the repository at this point in the history
  • Loading branch information
daschr committed Sep 13, 2023
1 parent ade272a commit 25037b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ edition = "2021"
serde_json = "1.0.105"
serde = {version = "1.0.183", features = ["derive"]}
curl = "0.4.44"
clap = { version = "4.4.3", features = ["derive"] }

[profile.release]
strip=true
lto=true
28 changes: 22 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
use std::env;
use std::io::Error as IoError;
use std::path::Path;
use std::process::exit;

use clap::Parser;

mod docker;
mod hosts;
use hosts::Hosts;

const DOCKER_SOCKET: &str = "/var/run/docker.sock";
const HOSTS: &str = "/etc/hosts";
/// Generates /etc/hosts entries for all docker containers
#[derive(Parser, Debug)]
#[command(
version,
about,
long_about = None
)]
struct Args {
/// Unix socket path to docker
#[arg(long, default_value_t = String::from("/var/run/docker.sock"))]
docker_socket: String,

/// Path to hosts
#[arg(long, default_value_t = String::from("/etc/hosts"))]
hosts: String,
}

fn main() -> Result<(), IoError> {
let lister = docker::ContainerLister::new(DOCKER_SOCKET);
let args = Args::parse();

let lister = docker::ContainerLister::new(args.docker_socket.as_str());
let container_entries: Vec<String> = match lister.fetch() {
Ok(v) => v
.iter()
Expand All @@ -27,7 +43,7 @@ fn main() -> Result<(), IoError> {
exit(0);
}

let mut hosts = match Hosts::new(HOSTS) {
let mut hosts = match Hosts::new(args.hosts) {
Ok(v) => v,
Err(e) => {
eprintln!("Error: could not open hosts: {:?}", e);
Expand Down

0 comments on commit 25037b3

Please sign in to comment.