This commit is contained in:
naskya 2024-06-21 14:00:51 +09:00
parent b706cc894d
commit af84e9f7d4
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
2 changed files with 22 additions and 13 deletions

View file

@ -18,24 +18,26 @@ pub(crate) enum UpdateError {
RevisionCheck(#[from] RevisionCheckError), RevisionCheck(#[from] RevisionCheckError),
#[error(transparent)] #[error(transparent)]
FileOperation(#[from] io::Error), FileOperation(#[from] io::Error),
#[error("downgrading is not supported")]
Downgrade,
#[error(transparent)] #[error(transparent)]
V1(#[from] v1::Error), V1(#[from] v1::Error),
} }
pub(super) async fn run(revision: Option<Revision>) -> Result<(), UpdateError> { pub(super) async fn run(revision: Option<Revision>) -> Result<(), UpdateError> {
let current_revision = current_revision()?; let current = current_revision()?;
if current_revision.next().is_none() { if current.next().is_none() {
println!("Your config files are already up-to-date! (as of this fishctl release)"); println!("Your config files are already up-to-date! (as of this fishctl release)");
return Ok(()); return Ok(());
} }
if current_revision != Revision::V0 { if current != Revision::V0 {
take_backup()?; take_backup()?;
} }
match revision { match revision {
Some(revision) => update_to(revision).await, Some(target) => update(current, target).await,
None => update_to_latest_from(current_revision).await, None => update(current, Revision::last().unwrap()).await,
} }
} }
@ -59,17 +61,22 @@ fn take_backup() -> Result<(), UpdateError> {
Ok(()) Ok(())
} }
async fn update_to_latest_from(mut current_revision: Revision) -> Result<(), UpdateError> { /// Updates config files to the specified revision.
while let Some(next_revision) = current_revision.next() { async fn update(mut current: Revision, target: Revision) -> Result<(), UpdateError> {
update_to(next_revision.clone()).await?; if current > target {
current_revision = next_revision; return Err(UpdateError::Downgrade);
}
while current < target {
let next = current.next().unwrap();
update_one_revision(next.clone()).await?;
current = next;
} }
Ok(()) Ok(())
} }
/// Updates config files to the specified revision. /// Updates config file revision from `target - 1` to `target`.
async fn update_to(revision: Revision) -> Result<(), UpdateError> { async fn update_one_revision(target: Revision) -> Result<(), UpdateError> {
match revision { match target {
Revision::V0 => unreachable!(), Revision::V0 => unreachable!(),
Revision::V1 => v1::run().await?, Revision::V1 => v1::run().await?,
} }

View file

@ -11,7 +11,9 @@ use validator::ValidationError;
pub(crate) const SERVER_CONFIG_PATH: &str = "config/server.toml"; pub(crate) const SERVER_CONFIG_PATH: &str = "config/server.toml";
pub(crate) const CLIENT_CONFIG_PATH: &str = "config/client.toml"; pub(crate) const CLIENT_CONFIG_PATH: &str = "config/client.toml";
#[derive(Deserialize_repr, Serialize_repr, PartialEq, Clone, ValueEnum, Sequence, Debug)] #[derive(
Deserialize_repr, Serialize_repr, PartialEq, PartialOrd, Clone, ValueEnum, Sequence, Debug,
)]
#[clap(rename_all = "lowercase")] #[clap(rename_all = "lowercase")]
#[repr(u8)] #[repr(u8)]
pub enum Revision { pub enum Revision {