WIP
This commit is contained in:
parent
80b66fb7ca
commit
c831afa9f4
3 changed files with 52 additions and 12 deletions
|
@ -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<HashMap<String, Yaml>, 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<client::Config, Error> {
|
|||
|
||||
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(())
|
||||
}
|
||||
|
|
|
@ -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<server::Config, ReadConfigError> {
|
||||
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<server::Config, ReadConfigError> {
|
|||
}
|
||||
|
||||
fn read_client_toml() -> Result<client::Config, ReadConfigError> {
|
||||
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)?;
|
||||
|
|
|
@ -54,6 +54,7 @@ pub struct Network {
|
|||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum HttpProtocol {
|
||||
Https,
|
||||
Http,
|
||||
|
|
Loading…
Reference in a new issue