rearrange

This commit is contained in:
naskya 2024-06-21 13:20:14 +09:00
parent 701cf74693
commit 2f6bfee5c7
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
3 changed files with 62 additions and 63 deletions

View file

@ -29,6 +29,14 @@ pub(crate) enum ConfigError {
Validate(#[from] validate::ValidationError), Validate(#[from] validate::ValidationError),
} }
pub(crate) async fn run(command: Commands) -> Result<(), ConfigError> {
match command {
Commands::Update { revision } => update::run(revision).await?,
Commands::Validate => validate::run()?,
}
Ok(())
}
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub(crate) enum RevisionCheckError { pub(crate) enum RevisionCheckError {
#[error("failed to determine the current config revision ({0})")] #[error("failed to determine the current config revision ({0})")]
@ -113,12 +121,3 @@ fn current_revision() -> Result<Revision, RevisionCheckError> {
_ => Ok(server_config_revision), _ => Ok(server_config_revision),
} }
} }
pub(crate) async fn run(command: Commands) -> Result<(), ConfigError> {
match command {
Commands::Update { revision } => update::run(revision).await?,
Commands::Validate => validate::run()?,
}
Ok(())
}

View file

@ -22,6 +22,23 @@ pub(crate) enum UpdateError {
V1(#[from] v1::Error), V1(#[from] v1::Error),
} }
pub(super) async fn run(revision: Option<Revision>) -> Result<(), UpdateError> {
let current_revision = current_revision()?;
if current_revision.next().is_none() {
println!("Your config files are already up-to-date! (as of this fishctl release)");
return Ok(());
}
if current_revision != Revision::V0 {
take_backup()?;
}
match revision {
Some(revision) => update_to(revision).await,
None => update_to_latest_from(current_revision).await,
}
}
fn take_backup() -> Result<(), UpdateError> { fn take_backup() -> Result<(), UpdateError> {
let current_time = Local::now().format("%Y%m%d%H%M%S").to_string(); let current_time = Local::now().format("%Y%m%d%H%M%S").to_string();
let server_backup_filename = format!("{}-backup-{}", SERVER_CONFIG_PATH, current_time); let server_backup_filename = format!("{}-backup-{}", SERVER_CONFIG_PATH, current_time);
@ -58,20 +75,3 @@ async fn update_to(revision: Revision) -> Result<(), UpdateError> {
} }
Ok(()) Ok(())
} }
pub(super) async fn run(revision: Option<Revision>) -> Result<(), UpdateError> {
let current_revision = current_revision()?;
if current_revision.next().is_none() {
println!("Your config files are already up-to-date! (as of this fishctl release)");
return Ok(());
}
if current_revision != Revision::V0 {
take_backup()?;
}
match revision {
Some(revision) => update_to(revision).await,
None => update_to_latest_from(current_revision).await,
}
}

View file

@ -31,6 +31,43 @@ pub(crate) enum Error {
InvalidUrl(#[from] url::ParseError), InvalidUrl(#[from] url::ParseError),
} }
pub(super) async fn run() -> Result<(), Error> {
println!("Updating the config to revision 1...");
let (default_yml, meta) = read_old_config().await?;
let server_config = create_new_server_config(default_yml, &meta)?;
let client_config = create_new_client_config(meta)?;
if !Path::new("config").exists() {
fs::create_dir("config").map_err(WriteTomlConfigError::Mkdir)?;
}
let mut server_toml =
fs::File::create(SERVER_CONFIG_PATH).map_err(WriteTomlConfigError::ServerWrite)?;
server_toml
.write(
toml::to_string_pretty(&server_config)
.map_err(WriteTomlConfigError::ServerSer)?
.as_bytes(),
)
.map_err(WriteTomlConfigError::ServerWrite)?;
cprintln!("<bold>{}</> has been created!", SERVER_CONFIG_PATH);
let mut client_toml =
fs::File::create(CLIENT_CONFIG_PATH).map_err(WriteTomlConfigError::ClientWrite)?;
client_toml
.write(
toml::to_string_pretty(&client_config)
.map_err(WriteTomlConfigError::ClientSer)?
.as_bytes(),
)
.map_err(WriteTomlConfigError::ClientWrite)?;
cprintln!("<bold>{}</> has been created!", CLIENT_CONFIG_PATH);
Ok(())
}
/// Errors that can happen while reading `.config/default.yml` /// Errors that can happen while reading `.config/default.yml`
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub(crate) enum ReadYamlConfigError { pub(crate) enum ReadYamlConfigError {
@ -429,40 +466,3 @@ fn create_new_client_config(meta: Meta) -> Result<client::Config, Error> {
Ok(config) Ok(config)
} }
pub(super) async fn run() -> Result<(), Error> {
println!("Updating the config to revision 1...");
let (default_yml, meta) = read_old_config().await?;
let server_config = create_new_server_config(default_yml, &meta)?;
let client_config = create_new_client_config(meta)?;
if !Path::new("config").exists() {
fs::create_dir("config").map_err(WriteTomlConfigError::Mkdir)?;
}
let mut server_toml =
fs::File::create(SERVER_CONFIG_PATH).map_err(WriteTomlConfigError::ServerWrite)?;
server_toml
.write(
toml::to_string_pretty(&server_config)
.map_err(WriteTomlConfigError::ServerSer)?
.as_bytes(),
)
.map_err(WriteTomlConfigError::ServerWrite)?;
cprintln!("<bold>{}</> has been created!", SERVER_CONFIG_PATH);
let mut client_toml =
fs::File::create(CLIENT_CONFIG_PATH).map_err(WriteTomlConfigError::ClientWrite)?;
client_toml
.write(
toml::to_string_pretty(&client_config)
.map_err(WriteTomlConfigError::ClientSer)?
.as_bytes(),
)
.map_err(WriteTomlConfigError::ClientWrite)?;
cprintln!("<bold>{}</> has been created!", CLIENT_CONFIG_PATH);
Ok(())
}