Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: docker socket is configurable #21

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ Ducker is configured via a yaml file found in the relevant config directory for

The following table summarises the available config values:

| Key | Default | Description |
| ------------ | ----------- | --------------------------------------------------------------------------------------------------------------------------- |
| prompt | 🦆 | The default prompt to display in the command pane |
| default_exec | `/bin/bash` | The default prompt to display in the command pane. NB - currently uses this for all exec's; it is planned to offer a choice |
| theme | [See below] | The colour theme configuration |
| Key | Default | Description |
| ------------ | ----------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| prompt | 🦆 | The default prompt to display in the command pane |
| default_exec | `/bin/bash` | The default prompt to display in the command pane. NB - currently uses this for all exec's; it is planned to offer a choice |
| docker_path | `unix:///var/run/docker.sock` | The location of the socket on which the docker daemon is exposed (defaults to `npipe:////./pipe/docker_engine` on windows) |
| theme | [See below] | The colour theme configuration |

If a value is unset or if the config file is unfound, Ducker will use the default values. If a value is malformed, Ducker will fail to run.

Expand Down
30 changes: 23 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@
#[serde(default = "default_exec")]
pub default_exec: String,

#[serde(default = "default_docker_path")]
pub docker_path: String,

#[serde(default)]
pub theme: Theme,
}

impl Config {
pub fn new(write: &bool) -> Result<Self> {
pub fn new(write: &bool, docker_path: Option<String>) -> Result<Self> {

Check warning on line 27 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L27

Added line #L27 was not covered by tests
let config_path = get_app_config_path()?.join("config.yaml");
if *write {
write_default_config(&config_path).context("failed to write default config")?;
}

if let Ok(f) = File::open(config_path) {
let config: Config =
serde_yml::from_reader(BufReader::new(f)).context("unable to parse config")?;
let mut config: Config;

Ok(config)
if let Ok(f) = File::open(config_path) {
config = serde_yml::from_reader(BufReader::new(f)).context("unable to parse config")?;

Check warning on line 36 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L35-L36

Added lines #L35 - L36 were not covered by tests
} else {
Ok(Config::default())
config = Config::default()

Check warning on line 38 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L38

Added line #L38 was not covered by tests
}

if let Some(p) = docker_path {
config.docker_path = p;

Check warning on line 42 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L41-L42

Added lines #L41 - L42 were not covered by tests
}

Ok(config)

Check warning on line 45 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L45

Added line #L45 was not covered by tests
}
}

Expand All @@ -46,6 +54,14 @@
"/bin/bash".into()
}

fn default_docker_path() -> String {
#[cfg(unix)]
return "unix:///var/run/docker.sock".into();

#[cfg(windows)]
return "npipe:////./pipe/docker_engine".into();
}

Check warning on line 63 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L57-L63

Added lines #L57 - L63 were not covered by tests

fn default_use_theme() -> bool {
false
}
Expand All @@ -55,7 +71,7 @@
Self {
prompt: default_prompt(),
default_exec: default_exec(),

docker_path: default_docker_path(),

Check warning on line 74 in src/config.rs

View check run for this annotation

Codecov / codecov/patch

src/config.rs#L74

Added line #L74 was not covered by tests
theme: Theme::default(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/docker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod container;
pub mod image;
pub mod logs;
pub mod util;
14 changes: 14 additions & 0 deletions src/docker/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use bollard::{Docker, API_DEFAULT_VERSION};
use color_eyre::eyre::{Context, Result};

use super::container::DockerContainer;

pub async fn new_local_docker_connection(socket_path: &str) -> Result<Docker> {
let docker = bollard::Docker::connect_with_socket(socket_path, 120, API_DEFAULT_VERSION)
.with_context(|| "unable to connect to local docker socket")?;

Check warning on line 8 in src/docker/util.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/util.rs#L6-L8

Added lines #L6 - L8 were not covered by tests

DockerContainer::list(&docker)
.await
.context("unable to connect to local docker socket")?;
Ok(docker)
}

Check warning on line 14 in src/docker/util.rs

View check run for this annotation

Codecov / codecov/patch

src/docker/util.rs#L10-L14

Added lines #L10 - L14 were not covered by tests
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use color_eyre::eyre::Context;
use config::Config;
use docker::util::new_local_docker_connection;
use events::{EventLoop, Key, Message};
use ui::App;

Expand All @@ -18,26 +19,38 @@
mod ui;
mod widgets;

const CONFIGURATION_DOC_PATH: &str = "https://github.com/robertpsoane/ducker#configuration";

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Export default config to default config directory
/// (usually ~/.config/ducker/config.yaml)
#[clap(long, short, action)]
export_default_config: bool,

/// Path at which to find the socket to communicate with
/// docker
#[clap(long, short)]
docker_path: Option<String>,
}

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
let args = Args::parse();
let config = Config::new(&args.export_default_config)?;
let config = Config::new(&args.export_default_config, args.docker_path)?;

let docker = new_local_docker_connection(&config.docker_path)
.await
.context(format!("failed to create docker connection, potentially due to misconfiguration (see {CONFIGURATION_DOC_PATH})"))?;

Check warning on line 45 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L41-L45

Added lines #L41 - L45 were not covered by tests

terminal::init_panic_hook();

Check warning on line 48 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L48

Added line #L48 was not covered by tests
let mut terminal = terminal::init().context("failed to initialise terminal")?;

let mut events = EventLoop::new();
let events_tx = events.get_tx();
let mut app = App::new(events_tx, config)
let mut app = App::new(events_tx, docker, config)

Check warning on line 53 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L53

Added line #L53 was not covered by tests
.await
.context("failed to create app")?;

Expand Down
9 changes: 7 additions & 2 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bollard::Docker;
use color_eyre::eyre::{Context, Result};
use ratatui::{
layout::{Constraint, Layout, Rect},
Expand Down Expand Up @@ -42,12 +43,16 @@
}

impl App {
pub async fn new(tx: Sender<Message<Key, Transition>>, config: Config) -> Result<Self> {
pub async fn new(
tx: Sender<Message<Key, Transition>>,
docker: Docker,
config: Config,
) -> Result<Self> {

Check warning on line 50 in src/ui/app.rs

View check run for this annotation

Codecov / codecov/patch

src/ui/app.rs#L46-L50

Added lines #L46 - L50 were not covered by tests
let config = Box::new(config);

let page = state::CurrentPage::default();

let body = PageManager::new(page.clone(), tx.clone(), config.clone())
let body = PageManager::new(page.clone(), tx.clone(), docker, config.clone())

Check warning on line 55 in src/ui/app.rs

View check run for this annotation

Codecov / codecov/patch

src/ui/app.rs#L55

Added line #L55 was not covered by tests
.await
.context("unable to create new body component")?;

Expand Down
4 changes: 1 addition & 3 deletions src/ui/page_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
pub async fn new(
page: state::CurrentPage,
tx: Sender<Message<Key, Transition>>,
docker: Docker,

Check warning on line 32 in src/ui/page_manager.rs

View check run for this annotation

Codecov / codecov/patch

src/ui/page_manager.rs#L32

Added line #L32 was not covered by tests
config: Box<Config>,
) -> Result<Self> {
let docker = bollard::Docker::connect_with_socket_defaults()
.context("unable to connect to local docker daemon")?;

let containers = Box::new(Containers::new(docker.clone(), tx.clone(), config.clone()));

let mut page_manager = Self {
Expand Down
Loading