fishctl/src/command/config.rs

125 lines
3.5 KiB
Rust
Raw Normal View History

2024-06-20 08:17:13 +09:00
//! `config` subcommand
2024-06-21 05:00:46 +09:00
mod update;
2024-06-20 02:07:34 +09:00
mod validate;
2024-06-21 04:36:44 +09:00
use crate::config::{Revision, CLIENT_CONFIG_PATH, OLD_CONFIG_PATH, SERVER_CONFIG_PATH};
2024-06-21 03:41:48 +09:00
use clap::Subcommand;
2024-06-21 04:36:44 +09:00
use serde::Deserialize;
2024-06-21 11:05:27 +09:00
use std::{
fs,
io::{self, Read},
path::Path,
};
2024-06-20 02:07:34 +09:00
#[derive(Subcommand)]
pub(crate) enum Commands {
2024-06-21 05:00:46 +09:00
/// Convert old config files to the new format
Update { revision: Option<Revision> },
2024-06-21 05:01:57 +09:00
/// Validate the config files
2024-06-20 02:07:34 +09:00
Validate,
}
2024-06-21 09:10:28 +09:00
/// Errors that can happen in `config` subcommand
2024-06-20 02:07:34 +09:00
#[derive(thiserror::Error, Debug)]
2024-06-21 09:10:28 +09:00
pub(crate) enum ConfigError {
2024-06-20 02:07:34 +09:00
#[error(transparent)]
2024-06-21 05:00:46 +09:00
Update(#[from] update::UpdateError),
2024-06-20 02:07:34 +09:00
#[error(transparent)]
2024-06-21 03:13:58 +09:00
Validate(#[from] validate::ValidationError),
2024-06-20 02:07:34 +09:00
}
2024-06-21 04:36:44 +09:00
#[derive(thiserror::Error, Debug)]
pub(crate) enum RevisionCheckError {
#[error("failed to determine the current config revision ({0})")]
UnknownRevision(&'static str),
#[error(transparent)]
2024-06-21 11:05:27 +09:00
ReadFile(#[from] ReadError),
}
#[derive(thiserror::Error, Debug)]
pub(crate) enum ReadError {
2024-06-21 04:36:44 +09:00
#[error(transparent)]
2024-06-21 11:05:27 +09:00
ReadFile(#[from] io::Error),
#[error("the config file is not written in the correct format")]
InvalidFormat(#[from] toml::de::Error),
}
fn read_file_string(path: &str) -> Result<String, ReadError> {
let mut file = fs::File::open(path)?;
let mut result = String::new();
file.read_to_string(&mut result)?;
Ok(result)
}
fn read_server_config<T>() -> Result<T, ReadError>
where
T: serde::de::DeserializeOwned,
{
toml::from_str(&read_file_string(SERVER_CONFIG_PATH)?).map_err(ReadError::InvalidFormat)
}
fn read_client_config<T>() -> Result<T, ReadError>
where
T: serde::de::DeserializeOwned,
{
toml::from_str(&read_file_string(CLIENT_CONFIG_PATH)?).map_err(ReadError::InvalidFormat)
2024-06-21 04:36:44 +09:00
}
2024-06-21 09:47:24 +09:00
fn current_revision() -> Result<Revision, RevisionCheckError> {
2024-06-21 04:36:44 +09:00
let old_config_exists = Path::new(OLD_CONFIG_PATH).is_file();
let server_config_exists = Path::new(SERVER_CONFIG_PATH).is_file();
let client_config_exists = Path::new(CLIENT_CONFIG_PATH).is_file();
if server_config_exists && !client_config_exists {
return Err(RevisionCheckError::UnknownRevision(
"client config file does not exist",
));
}
if !server_config_exists && client_config_exists {
return Err(RevisionCheckError::UnknownRevision(
"server config file does not exist",
));
}
if !old_config_exists && !server_config_exists && !client_config_exists {
return Err(RevisionCheckError::UnknownRevision(
"config file does not exist",
));
}
if old_config_exists && !server_config_exists && !client_config_exists {
2024-06-21 09:47:24 +09:00
return Ok(Revision::V0);
2024-06-21 04:36:44 +09:00
}
#[derive(Deserialize)]
struct Config {
config_revision: Revision,
}
2024-06-21 11:05:27 +09:00
let server_config_revision = read_server_config::<Config>()?.config_revision;
let client_config_revision = read_server_config::<Config>()?.config_revision;
2024-06-21 04:36:44 +09:00
if server_config_revision != client_config_revision {
return Err(RevisionCheckError::UnknownRevision(
"server config revision and client config revision are different",
));
}
2024-06-21 10:20:33 +09:00
match server_config_revision {
Revision::V0 => Err(RevisionCheckError::UnknownRevision(
"revision 0 does not exist",
)),
_ => Ok(server_config_revision),
}
2024-06-21 04:36:44 +09:00
}
2024-06-21 09:10:28 +09:00
pub(crate) async fn run(command: Commands) -> Result<(), ConfigError> {
2024-06-20 02:07:34 +09:00
match command {
2024-06-21 05:00:46 +09:00
Commands::Update { revision } => update::run(revision).await?,
2024-06-20 02:07:34 +09:00
Commands::Validate => validate::run()?,
}
Ok(())
}