use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::model::SCHEMA_VERSION;
use crate::remote::git::GitAuth;
pub const REMOTES_FILE: &str = "remotes.toml";
pub const DEFAULT_KEEP: u32 = 10;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RemoteKind {
Git,
Folder,
}
impl RemoteKind {
pub const fn as_str(self) -> &'static str {
match self {
RemoteKind::Git => "git",
RemoteKind::Folder => "folder",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FolderMode {
#[default]
RepoPerMachine,
Mirror,
Archive,
}
impl FolderMode {
pub const fn as_str(self) -> &'static str {
match self {
FolderMode::RepoPerMachine => "repo-per-machine",
FolderMode::Mirror => "mirror",
FolderMode::Archive => "archive",
}
}
pub fn parse(text: &str) -> Result<Self, CoreError> {
match text {
"repo-per-machine" => Ok(Self::RepoPerMachine),
"mirror" => Ok(Self::Mirror),
"archive" => Ok(Self::Archive),
other => Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"unknown folder mode {other:?}; expected repo-per-machine, mirror or archive"
),
)),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RemoteConfig {
pub name: String,
pub kind: RemoteKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mode: Option<FolderMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub keep: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub auth: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub username: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub push: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pull: Option<String>,
#[serde(flatten)]
pub extra: toml::Table,
}
impl RemoteConfig {
pub fn folder(name: &str, path: &str, mode: FolderMode) -> Self {
Self {
name: name.to_owned(),
kind: RemoteKind::Folder,
path: Some(path.to_owned()),
mode: Some(mode),
keep: None,
url: None,
auth: None,
username: None,
push: Some("on-commit".to_owned()),
pull: Some("manual".to_owned()),
extra: toml::Table::new(),
}
}
pub fn git(name: &str, url: &str, auth: GitAuth) -> Self {
Self {
name: name.to_owned(),
kind: RemoteKind::Git,
path: None,
mode: None,
keep: None,
url: Some(url.to_owned()),
auth: Some(auth.as_str().to_owned()),
username: None,
push: Some("on-commit".to_owned()),
pull: Some("on-start".to_owned()),
extra: toml::Table::new(),
}
}
pub fn folder_mode(&self) -> FolderMode {
self.mode.unwrap_or_default()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RemotesFile {
pub schema: i64,
#[serde(default, rename = "remote")]
pub remotes: Vec<RemoteConfig>,
#[serde(flatten)]
pub extra: toml::Table,
}
impl Default for RemotesFile {
fn default() -> Self {
Self {
schema: SCHEMA_VERSION,
remotes: Vec::new(),
extra: toml::Table::new(),
}
}
}
impl RemotesFile {
pub fn load(fs: &dyn Fs, state_dir: &Path) -> Result<Self, CoreError> {
let path = state_dir.join(REMOTES_FILE);
if !fs.exists(&path) {
return Ok(Self::default());
}
let text = fs.read_to_string(&path).map_err(|err| {
CoreError::new(ErrorCode::IoError, format!("cannot read file: {err}")).with_path(&path)
})?;
let file: Self = toml::from_str(&text).map_err(|err| {
CoreError::new(ErrorCode::ParseError, err.message().to_owned()).with_path(&path)
})?;
if file.schema != SCHEMA_VERSION {
return Err(CoreError::new(
ErrorCode::UnsupportedSchema,
format!("schema {} is not read by this build", file.schema),
)
.with_path(&path));
}
Ok(file)
}
pub fn save(&self, fs: &dyn Fs, state_dir: &Path) -> Result<(), CoreError> {
let text = toml::to_string_pretty(self).map_err(|err| {
CoreError::new(
ErrorCode::InvalidValue,
format!("cannot write remotes: {err}"),
)
})?;
crate::apply::write_atomic(fs, &state_dir.join(REMOTES_FILE), text.as_bytes())
}
pub fn get(&self, name: &str) -> Option<&RemoteConfig> {
self.remotes.iter().find(|remote| remote.name == name)
}
pub fn add(&mut self, remote: RemoteConfig) -> Result<(), CoreError> {
check_name(&remote.name)?;
if self.get(&remote.name).is_some() {
return Err(CoreError::new(
ErrorCode::AlreadyExists,
format!("a remote named {:?} exists already", remote.name),
));
}
self.remotes.push(remote);
Ok(())
}
pub fn remove(&mut self, name: &str) -> Result<RemoteConfig, CoreError> {
let index = self
.remotes
.iter()
.position(|remote| remote.name == name)
.ok_or_else(|| not_found(name))?;
Ok(self.remotes.remove(index))
}
pub fn select(&self, name: Option<&str>) -> Result<Vec<&RemoteConfig>, CoreError> {
match name {
Some(name) => self
.get(name)
.map(|r| vec![r])
.ok_or_else(|| not_found(name)),
None => Ok(self.remotes.iter().collect()),
}
}
}
fn not_found(name: &str) -> CoreError {
CoreError::new(ErrorCode::NotFound, format!("no remote named {name:?}"))
}
pub fn check_name(name: &str) -> Result<(), CoreError> {
let valid = !name.is_empty()
&& name.len() <= 64
&& name
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
&& !name.starts_with(['.', '-'])
&& !name.ends_with('.')
&& !name.contains("..")
&& !name.ends_with(".lock");
if valid {
Ok(())
} else {
Err(CoreError::new(
ErrorCode::InvalidValue,
format!("invalid remote name {name:?}: letters, digits, '.', '_' or '-'"),
))
}
}