Skip to content
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
84 changes: 78 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tokio-util = "0.7"
sha2 = "0.10"
miette = { version = "7.5", features = ["fancy"] }
indicatif = "0.17"
serde_json5 = "0.2.1"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
8 changes: 8 additions & 0 deletions config_examples/basic.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ exec_cmds = ["apk add helix asciiquarium"]
entry_cmd = "/bin/ash"
create_options = [ "-it"]
entry_options = ["-it"]

[environment.dev]
devcontainer_json = "$PWD/.devcontainer/devcontainer.json"
create_options = ["-it", "-v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK", "-e SSH_AUTH_SOCK=$SSH_AUTH_SOCK"]
cp_cmds = ["/home/archie/dotfiles/bundler/bundle/. CONTAINER:/tmp/bundle"]
exec_cmds = ["bash -c 'cd /tmp/bundle && bash install.sh'"]
entry_options = ["-it", "-e EDITOR=hx"]
entry_cmd = "/bin/bash"
3 changes: 3 additions & 0 deletions config_examples/devcontainer_example/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM ubuntu:24.04

CMD ["/bin/bash"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "MyPreset",
"build": {
"dockerfile": "Dockerfile"
},
"workspaceMount": "source=${localWorkspaceFolder},target=/root,type=bind"
}
5 changes: 5 additions & 0 deletions config_examples/devcontainer_example/simple_preset.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[environment.dev]
devcontainer_json = "$PWD/.devcontainer/devcontainer.json"
entry_cmd = "/bin/bash"
create_options = [ "-it"]
entry_options = ["-it"]
8 changes: 0 additions & 8 deletions config_examples/simple_preset.toml

This file was deleted.

4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
cargo build

[no-cd, no-exit-message]
@ test: clippy
cargo test
test *args: clippy
cargo test {{args}}

[no-cd, no-exit-message]
@ clippy:
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl AppConfig {
fn set_config_path(config_path: Option<PathBuf>) -> Result<PathBuf> {
if let Some(path) = config_path {
return if path.exists() && path.is_file() {
Ok(path)
Ok(path.canonicalize().unwrap())
} else {
Err(CliError::NoConfigAtProvidedPath(path.as_os_str().into()).into())
};
Expand Down
61 changes: 59 additions & 2 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
};
use thiserror::Error;

use crate::{cli::AppConfig, util::UnexpectedExt};
use crate::{cli::AppConfig, devcontainer::Metadata, util::UnexpectedExt};

#[derive(Debug, Error, PartialEq, Diagnostic)]
pub enum ConfigError {
Expand Down Expand Up @@ -108,6 +108,9 @@ pub struct TomlEnvironment {
#[serde(default)]
build_context: String,

#[serde(default)]
devcontainer_json: String,

#[serde(default)]
entry_options: Vec<String>,

Expand Down Expand Up @@ -143,6 +146,9 @@ pub struct TomlPreset {
#[serde(default)]
build_context: String,

#[serde(default)]
devcontainer_json: String,

#[serde(default)]
entry_options: Vec<String>,

Expand Down Expand Up @@ -178,6 +184,7 @@ pub struct Environment {
pub image: String,
pub dockerfile: Option<PathBuf>,
pub build_context: Option<PathBuf>,
pub devcontainer_json: Option<String>,
pub entry_cmd: String,
pub entry_options: Vec<String>,
pub exec_cmds: Vec<String>,
Expand Down Expand Up @@ -207,6 +214,7 @@ impl Configuration {
let config = self.check_presets_exist(config)?;
let config = self.valid_unique_fields(config)?;
let envs = self.merge_presets(config)?;
let envs = self.inject_devcontaier(envs)?;
let envs = self.validate_environments(envs)?;
self.create_environment(envs)
}
Expand All @@ -233,6 +241,40 @@ impl Configuration {
}
}

fn inject_devcontaier(&self, mut envs: TomlEnvs) -> Result<TomlEnvs> {
for (_, env) in envs.iter_mut() {
if !env.devcontainer_json.is_empty() {
let mut options = ExpandOptions::new();
options.expansion_type = Some(ExpansionType::Unix);

let path = envmnt::expand(&env.devcontainer_json, Some(options));
let json = Metadata::new(path).unwrap();

if let Some(user) = json.remote_user {
let user_commands = vec![format!("-u {user}"), format!("-e USER={user}")];
env.exec_options.extend_from_slice(&user_commands);
env.entry_options.extend_from_slice(&user_commands);
}

if let Some(mount) = json.workspace_mount {
env.create_options.push(mount);
}

if env.provided_image.is_empty() {
env.provided_image = json.image.unwrap_or_default();
}

if env.dockerfile.is_empty() && env.build_context.is_empty() {
if let Some(build) = json.build {
env.dockerfile = build.dockerfile.unwrap_or_default();
env.build_context = build.context.unwrap_or_default();
}
}
}
}
Ok(envs)
}

fn check_presets_exist(&self, config: TomlConfiguration) -> Result<TomlConfiguration> {
for (env_name, env) in &config.environments {
for preset_name in &env.presets {
Expand Down Expand Up @@ -294,6 +336,7 @@ impl Configuration {
"image" => !env.provided_image.is_empty(),
"dockerfile" => !env.dockerfile.is_empty(),
"build_context" => !env.build_context.is_empty(),
"devcontainer_json" => !env.devcontainer_json.is_empty(),
_ => unreachable!("Unknown field {field}"),
};

Expand All @@ -315,6 +358,9 @@ impl Configuration {
"image" => !config.presets[preset_name].provided_image.is_empty(),
"dockerfile" => !config.presets[preset_name].dockerfile.is_empty(),
"build_context" => !config.presets[preset_name].build_context.is_empty(),
"devcontainer_json" => {
!config.presets[preset_name].devcontainer_json.is_empty()
}
_ => unreachable!("Unknown field {field}"),
};

Expand Down Expand Up @@ -359,7 +405,13 @@ impl Configuration {
Ok(())
};

let unique_fields = ["entry_cmd", "image", "dockerfile", "build_context"];
let unique_fields = [
"entry_cmd",
"image",
"dockerfile",
"build_context",
"devcontainer_json",
];
for (env_name, env) in &config.environments {
for field in unique_fields {
check_unique(field, env, env_name)?;
Expand All @@ -385,6 +437,10 @@ impl Configuration {
env.dockerfile = preset.dockerfile.clone();
}

if !preset.devcontainer_json.is_empty() {
env.devcontainer_json = preset.devcontainer_json.clone();
}

env.entry_options.extend_from_slice(&preset.entry_options);
env.exec_cmds.extend_from_slice(&preset.exec_cmds);
env.exec_options.extend_from_slice(&preset.exec_options);
Expand Down Expand Up @@ -503,6 +559,7 @@ impl Configuration {
dockerfile,
build_context,
entry_cmd: env.entry_cmd,
devcontainer_json: Some(env.devcontainer_json),
entry_options: env.entry_options,
exec_cmds: env.exec_cmds,
exec_options: env.exec_options,
Expand Down
Loading