cargo-remote/src/config.rs

108 lines
3.2 KiB
Rust
Raw Normal View History

2021-03-31 14:49:44 +00:00
use serde::Deserialize;
2021-03-28 17:46:21 +00:00
2021-03-31 14:49:44 +00:00
#[derive(Debug, Clone)]
2021-03-28 17:46:21 +00:00
pub struct Remote {
2021-03-31 14:49:44 +00:00
pub name: String,
2021-03-28 17:46:21 +00:00
pub host: String,
pub ssh_port: u16,
pub temp_dir: String,
pub env: String,
2021-03-28 17:46:21 +00:00
}
2021-03-31 14:49:44 +00:00
#[derive(Debug, Deserialize)]
2021-03-31 14:53:46 +00:00
struct PartialRemote {
2021-03-31 14:49:44 +00:00
pub name: Option<String>,
pub host: String,
pub ssh_port: Option<u16>,
pub temp_dir: Option<String>,
pub env: Option<String>,
2021-03-31 14:49:44 +00:00
}
2021-03-28 17:46:21 +00:00
impl Default for Remote {
fn default() -> Self {
Self {
2021-03-31 14:49:44 +00:00
name: String::new(),
2021-03-28 17:46:21 +00:00
host: String::new(),
ssh_port: 22,
temp_dir: "~/remote-builds".to_string(),
env: "/etc/profile".to_string(),
2021-03-28 17:46:21 +00:00
}
}
}
2021-03-31 14:53:46 +00:00
impl From<PartialRemote> for Remote {
fn from(minimal_remote: PartialRemote) -> Self {
2021-03-31 14:49:44 +00:00
let default = Remote::default();
let name = minimal_remote.name.unwrap_or(default.name);
let ssh_port = minimal_remote.ssh_port.unwrap_or(default.ssh_port);
let temp_dir = minimal_remote.temp_dir.unwrap_or(default.temp_dir);
let env = minimal_remote.env.unwrap_or(default.env);
2021-03-31 14:49:44 +00:00
Remote {
name,
host: minimal_remote.host,
ssh_port,
temp_dir,
env,
2021-03-31 14:49:44 +00:00
}
}
}
impl<'de> Deserialize<'de> for Remote {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
2021-03-31 14:53:46 +00:00
PartialRemote::deserialize(deserializer).map(Self::from)
2021-03-31 14:49:44 +00:00
}
}
#[derive(Debug, Default, Deserialize)]
2021-03-28 17:46:21 +00:00
pub struct Config {
2021-03-31 14:49:44 +00:00
#[serde(rename = "remote")]
remotes: Option<Vec<Remote>>,
2021-03-28 17:46:21 +00:00
}
impl Config {
pub fn new(project_dir: &std::path::Path) -> Result<Self, config::ConfigError> {
2021-03-31 14:49:44 +00:00
let mut conf = config::Config::new();
2021-03-28 17:46:21 +00:00
if let Some(config_file) = xdg::BaseDirectories::with_prefix("cargo-remote")
.ok()
.and_then(|base| base.find_config_file("cargo-remote.toml"))
{
conf.merge(config::File::from(config_file))?;
}
let project_config = project_dir.join(".cargo-remote.toml");
if project_config.is_file() {
conf.merge(config::File::from(project_config))?;
}
conf.try_into()
}
2021-03-31 14:49:44 +00:00
pub fn get_remote(&self, opts: &crate::RemoteOpts) -> Option<Remote> {
2021-03-31 14:49:44 +00:00
let remotes: Vec<_> = self.remotes.clone().unwrap_or_default();
let config_remote = match &opts.name {
2021-03-31 14:49:44 +00:00
Some(remote_name) => remotes
.into_iter()
.find(|remote| remote.name == *remote_name),
None => remotes.into_iter().next(),
};
let blueprint_remote = match (config_remote, opts.host.is_some()) {
2021-03-31 14:49:44 +00:00
(Some(config_remote), _) => config_remote,
(None, true) => Remote::default(),
(None, false) => return None,
};
Some(Remote {
name: opts.name.clone().unwrap_or(blueprint_remote.name),
host: opts.host.clone().unwrap_or(blueprint_remote.host),
ssh_port: opts.ssh_port.clone().unwrap_or(blueprint_remote.ssh_port),
temp_dir: opts.temp_dir.clone().unwrap_or(blueprint_remote.temp_dir),
env: opts.env.clone().unwrap_or(blueprint_remote.env),
2021-03-31 14:49:44 +00:00
})
}
2021-03-28 17:46:21 +00:00
}