Skip to content
Closed
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
44 changes: 44 additions & 0 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 @@ -16,6 +16,7 @@ anyhow = "1.*"
clap = { version = "4.0.26", features = ["derive"] }
clap_complete = "4.0.5"
crossterm = "0.25.0"
dialoguer = { git = "https://github.com/SIMULATAN/dialoguer", features = [] }
diff = "0.1.*"
handlebars = "5.*"
hostname = "0.3.*"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Usage: dotter [OPTIONS] [COMMAND]

Commands:
deploy Deploy the files to their respective targets. This is the default subcommand
config Interactively modify the local configuration file
undeploy Delete all deployed files from their target locations. Note that this operates on all files that are currently in cache
init Initialize global.toml with a single package containing all the files in the current directory pointing to a dummy value and a local.toml that selects that package
watch Run continuously, watching the repository for changes and deploying as soon as they happen. Can be ran with `--dry-run`
Expand Down
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ pub enum Action {
#[default]
Deploy,

/// Interactively modify the local configuration file.
Config,

/// Delete all deployed files from their target locations.
/// Note that this operates on all files that are currently in cache.
Undeploy,
Expand Down
73 changes: 42 additions & 31 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,64 +96,44 @@ pub struct Configuration {
#[serde(deny_unknown_fields)]
pub struct Package {
#[serde(default)]
depends: Vec<String>,
pub depends: Vec<String>,
#[serde(default)]
files: Files,
#[serde(default)]
variables: Variables,
}

#[derive(Debug, Deserialize, Serialize)]
struct GlobalConfig {
pub struct GlobalConfig {
#[serde(default)]
#[cfg(feature = "scripting")]
helpers: Helpers,
#[serde(flatten)]
packages: BTreeMap<String, Package>,
pub packages: BTreeMap<String, Package>,
}

type IncludedConfig = BTreeMap<String, Package>;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(deny_unknown_fields)]
struct LocalConfig {
pub struct LocalConfig {
#[serde(default)]
includes: Vec<PathBuf>,
packages: Vec<String>,
pub packages: Vec<String>,
#[serde(default)]
files: Files,
#[serde(default)]
variables: Variables,
}

pub fn load_configuration(
local_config: &Path,
global_config: &Path,
local_config_path: &Path,
global_config_path: &Path,
patch: Option<Package>,
) -> Result<Configuration> {
let global: GlobalConfig = filesystem::load_file(global_config)
.and_then(|c| c.ok_or_else(|| anyhow::anyhow!("file not found")))
.with_context(|| format!("load global config {:?}", global_config))?;
trace!("Global config: {:#?}", global);

// If local.toml can't be found, look for a file named <hostname>.toml instead
let mut local_config_buf = local_config.to_path_buf();
if !local_config_buf.exists() {
let hostname = hostname::get()
.context("failed to get the computer hostname")?
.into_string()
.expect("hostname cannot be converted to string");
info!(
"{:?} not found, using {}.toml instead (based on hostname)",
local_config, hostname
);
local_config_buf.set_file_name(&format!("{}.toml", hostname));
}
let global = load_global_config(global_config_path)?;

let local: LocalConfig = filesystem::load_file(local_config_buf.as_path())
.and_then(|c| c.ok_or_else(|| anyhow::anyhow!("file not found")))
.with_context(|| format!("load local config {:?}", local_config))?;
trace!("Local config: {:#?}", local);
let local = load_local_config(local_config_path)?.context("local config not found")?;

let mut merged_config =
merge_configuration_files(global, local, patch).context("merge configuration files")?;
Expand Down Expand Up @@ -185,6 +165,37 @@ pub fn load_configuration(
Ok(merged_config)
}

pub fn load_global_config(global_config: &Path) -> Result<GlobalConfig> {
let global: GlobalConfig = filesystem::load_file(global_config)
.and_then(|c| c.ok_or_else(|| anyhow::anyhow!("file not found")))
.with_context(|| format!("load global config {:?}", global_config))?;
trace!("Global config: {:#?}", global);

Ok(global)
}

pub fn load_local_config(local_config: &Path) -> Result<Option<LocalConfig>> {
// If local.toml can't be found, look for a file named <hostname>.toml instead
let mut local_config_buf = local_config.to_path_buf();
if !local_config_buf.exists() {
let hostname = hostname::get()
.context("failed to get the computer hostname")?
.into_string()
.expect("hostname cannot be converted to string");
info!(
"{:?} not found, using {}.toml instead (based on hostname)",
local_config, hostname
);
local_config_buf.set_file_name(&format!("{}.toml", hostname));
}

let local: Option<LocalConfig> = filesystem::load_file(local_config_buf.as_path())
.with_context(|| format!("load local config {:?}", local_config))?;
trace!("Local config: {:#?}", local);

Ok(local)
}

#[derive(Debug, Serialize, Deserialize, Default, Clone)]
#[serde(deny_unknown_fields)]
pub struct Cache {
Expand Down Expand Up @@ -258,7 +269,7 @@ fn recursive_extend_map(
}

#[allow(clippy::map_entry)]
fn merge_configuration_files(
pub fn merge_configuration_files(
mut global: GlobalConfig,
local: LocalConfig,
patch: Option<Package>,
Expand Down
Loading