Configure iPhone Git commit identity (#76)
This commit is contained in:
@@ -16,10 +16,13 @@ use cap_tempfile::TempFile;
|
||||
use serde::Deserialize;
|
||||
use url::Url;
|
||||
|
||||
use crate::authentication::{AuthenticationTimeout, DEFAULT_AUTHENTICATION_TIMEOUT};
|
||||
use crate::mobile::MobileTab;
|
||||
use crate::presentation::{ClipboardTimeout, DEFAULT_CLIPBOARD_TIMEOUT};
|
||||
use crate::repository::EntryPath;
|
||||
use crate::{
|
||||
authentication::{AuthenticationTimeout, DEFAULT_AUTHENTICATION_TIMEOUT},
|
||||
git::GitIdentity,
|
||||
mobile::MobileTab,
|
||||
presentation::{ClipboardTimeout, DEFAULT_CLIPBOARD_TIMEOUT},
|
||||
repository::EntryPath,
|
||||
};
|
||||
|
||||
const APPLICATION_DIRECTORY: &str = "ironstorage";
|
||||
const CONFIG_FILE: &str = "config.toml";
|
||||
@@ -40,6 +43,7 @@ pub struct Config {
|
||||
mobile_tab: MobileTab,
|
||||
mobile_home_refreshed_at: Option<i64>,
|
||||
watch_shared_totp_entries: BTreeSet<EntryPath>,
|
||||
git_identity: GitIdentity,
|
||||
git_remotes: Vec<GitRemote>,
|
||||
}
|
||||
|
||||
@@ -145,6 +149,10 @@ impl Config {
|
||||
&self.git_remotes
|
||||
}
|
||||
|
||||
pub fn git_identity(&self) -> &GitIdentity {
|
||||
&self.git_identity
|
||||
}
|
||||
|
||||
pub fn settings(&self) -> ConfigSettings {
|
||||
let editor = self.editor.as_ref().map(|editor| {
|
||||
std::iter::once(editor.program.clone())
|
||||
@@ -272,6 +280,35 @@ impl Config {
|
||||
validate_config(self.source.clone(), document, raw)?.persist()
|
||||
}
|
||||
|
||||
pub fn update_git_identity(&self, identity: &GitIdentity) -> Result<(), ConfigError> {
|
||||
let mut document = self.current_document()?;
|
||||
let root = document
|
||||
.as_table_mut()
|
||||
.ok_or_else(|| ConfigError::Malformed {
|
||||
path: self.source.clone(),
|
||||
})?;
|
||||
let git = root
|
||||
.entry("git")
|
||||
.or_insert_with(|| toml::Value::Table(toml::Table::new()))
|
||||
.as_table_mut()
|
||||
.ok_or(ConfigError::InvalidField { field: "git" })?;
|
||||
git.insert(
|
||||
"user_name".to_owned(),
|
||||
toml::Value::String(identity.name().to_owned()),
|
||||
);
|
||||
git.insert(
|
||||
"user_email".to_owned(),
|
||||
toml::Value::String(identity.email().to_owned()),
|
||||
);
|
||||
let raw = document
|
||||
.clone()
|
||||
.try_into::<RawConfig>()
|
||||
.map_err(|_| ConfigError::Malformed {
|
||||
path: self.source.clone(),
|
||||
})?;
|
||||
validate_config(self.source.clone(), document, raw)?.persist()
|
||||
}
|
||||
|
||||
fn current_document(&self) -> Result<toml::Value, ConfigError> {
|
||||
Self::load(Some(&self.source)).map(|config| config.document)
|
||||
}
|
||||
@@ -930,6 +967,8 @@ enum RawEditor {
|
||||
#[derive(Default, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct RawGit {
|
||||
user_name: Option<String>,
|
||||
user_email: Option<String>,
|
||||
#[serde(default)]
|
||||
remotes: Vec<RawGitRemote>,
|
||||
}
|
||||
@@ -1019,6 +1058,19 @@ fn validate_config(
|
||||
})
|
||||
})
|
||||
.collect::<Result<BTreeSet<_>, _>>()?;
|
||||
let git_identity = match (raw.git.user_name, raw.git.user_email) {
|
||||
(None, None) => GitIdentity::ironstorage(),
|
||||
(Some(name), Some(email)) => {
|
||||
GitIdentity::new(name, email).map_err(|_| ConfigError::InvalidField {
|
||||
field: "git.user_identity",
|
||||
})?
|
||||
}
|
||||
_ => {
|
||||
return Err(ConfigError::InvalidField {
|
||||
field: "git.user_identity",
|
||||
});
|
||||
}
|
||||
};
|
||||
let git_remotes = validate_remotes(raw.git.remotes)?;
|
||||
|
||||
Ok(Config {
|
||||
@@ -1034,6 +1086,7 @@ fn validate_config(
|
||||
mobile_tab,
|
||||
mobile_home_refreshed_at,
|
||||
watch_shared_totp_entries,
|
||||
git_identity,
|
||||
git_remotes,
|
||||
})
|
||||
}
|
||||
@@ -1228,7 +1281,7 @@ fn validate_known_fields(value: &toml::Value, source: &Path) -> Result<(), Confi
|
||||
let git = git.as_table().ok_or_else(|| ConfigError::Malformed {
|
||||
path: source.to_owned(),
|
||||
})?;
|
||||
validate_table(git, "git", &["remotes"])?;
|
||||
validate_table(git, "git", &["user_name", "user_email", "remotes"])?;
|
||||
if let Some(remotes) = git.get("remotes") {
|
||||
let remotes = remotes.as_array().ok_or_else(|| ConfigError::Malformed {
|
||||
path: source.to_owned(),
|
||||
|
||||
Reference in New Issue
Block a user