From c831afa9f46a0ecc2fe6062ee9d0b68ea21a8883 Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 20 Jun 2024 12:40:12 +0900 Subject: [PATCH] WIP --- src/command/config/migrate/v20240701.rs | 51 +++++++++++++++++++++++-- src/command/config/validate.rs | 12 ++---- src/config/server.rs | 1 + 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/command/config/migrate/v20240701.rs b/src/command/config/migrate/v20240701.rs index 33b6c7c..f3a5ee4 100644 --- a/src/command/config/migrate/v20240701.rs +++ b/src/command/config/migrate/v20240701.rs @@ -5,7 +5,12 @@ use crate::config::{client, server}; use sqlx::{postgres::PgConnectOptions, ConnectOptions}; -use std::{collections::HashMap, env, fs, io::Read}; +use std::{ + collections::HashMap, + fs, + io::{self, Read, Write}, + path::Path, +}; use url::Url; use yaml_rust::{Yaml, YamlLoader}; @@ -13,6 +18,8 @@ use yaml_rust::{Yaml, YamlLoader}; pub(crate) enum Error { #[error("failed to parse the old config file (.config/default.yml)")] ReadYaml(#[from] ReadYamlConfigError), + #[error(transparent)] + WriteToml(#[from] WriteTomlConfigError), #[error("failed to read the meta table")] ReadMeta(#[from] sqlx::Error), #[error("invalid config ({0})")] @@ -31,9 +38,22 @@ pub(crate) enum ReadYamlConfigError { InvalidConfig(String), } +#[derive(thiserror::Error, Debug)] +pub(crate) enum WriteTomlConfigError { + #[error("failed to serialize the new server config into TOML format")] + ServerSer(#[source] toml::ser::Error), + #[error("failed to serialize the new client config into TOML format")] + ClientSer(#[source] toml::ser::Error), + #[error("failed to write to `config/server.toml`")] + ServerWrite(#[source] io::Error), + #[error("failed to write to `config/client.toml`")] + ClientWrite(#[source] io::Error), + #[error("failed to create `config` directory")] + Mkdir(#[source] io::Error), +} + fn read_default_yml() -> Result, ReadYamlConfigError> { - let cwd = env::current_dir()?; - let mut default_yml = fs::File::open(cwd.join(".config/default.yml"))?; + let mut default_yml = fs::File::open(".config/default.yml")?; let mut buffer = String::new(); default_yml.read_to_string(&mut buffer)?; @@ -409,8 +429,33 @@ fn create_new_client_config(meta: Meta) -> Result { pub(super) async fn run() -> Result<(), Error> { 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_new("config/server.toml").map_err(WriteTomlConfigError::ServerWrite)?; + server_toml + .write( + toml::to_string_pretty(&server_config) + .map_err(WriteTomlConfigError::ServerSer)? + .as_bytes(), + ) + .map_err(WriteTomlConfigError::ServerWrite)?; + + let mut client_toml = + fs::File::create_new("config/client.toml").map_err(WriteTomlConfigError::ClientWrite)?; + client_toml + .write( + toml::to_string_pretty(&client_config) + .map_err(WriteTomlConfigError::ClientSer)? + .as_bytes(), + ) + .map_err(WriteTomlConfigError::ClientWrite)?; + Ok(()) } diff --git a/src/command/config/validate.rs b/src/command/config/validate.rs index f2b72df..b809be7 100644 --- a/src/command/config/validate.rs +++ b/src/command/config/validate.rs @@ -1,7 +1,7 @@ //! `config validate` subcommand use crate::config::{client, server}; -use std::{env, fs, io::Read}; +use std::{fs, io::Read}; use validator::{Validate, ValidationErrors}; #[derive(thiserror::Error, Debug)] @@ -29,10 +29,7 @@ pub(super) fn run() -> Result<(), ConfigValidateError> { } fn read_server_toml() -> Result { - const FILENAME: &str = "config/server.toml"; - - let cwd = env::current_dir()?; - let mut file = fs::File::open(cwd.join(FILENAME))?; + let mut file = fs::File::open("config/server.toml")?; let mut buffer = String::new(); file.read_to_string(&mut buffer)?; @@ -41,10 +38,7 @@ fn read_server_toml() -> Result { } fn read_client_toml() -> Result { - const FILENAME: &str = "config/client.toml"; - - let cwd = env::current_dir()?; - let mut file = fs::File::open(cwd.join(FILENAME))?; + let mut file = fs::File::open("config/client.toml")?; let mut buffer = String::new(); file.read_to_string(&mut buffer)?; diff --git a/src/config/server.rs b/src/config/server.rs index 4fdba54..9593b93 100644 --- a/src/config/server.rs +++ b/src/config/server.rs @@ -54,6 +54,7 @@ pub struct Network { } #[derive(Deserialize, Serialize, Debug)] +#[serde(rename_all = "lowercase")] pub enum HttpProtocol { Https, Http,