From d628550abcf938e48d102cd1f083cf188a4af3ff Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 21 Jun 2024 11:16:03 +0900 Subject: [PATCH] refactor --- src/command/config/update/v1.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/command/config/update/v1.rs b/src/command/config/update/v1.rs index 5cfd74f..7f95915 100644 --- a/src/command/config/update/v1.rs +++ b/src/command/config/update/v1.rs @@ -1,15 +1,16 @@ //! `config update v1` subcommand //! -use crate::config::{ - client, server, Revision, CLIENT_CONFIG_PATH, OLD_CONFIG_PATH, SERVER_CONFIG_PATH, +use crate::{ + command::config::{read_file_as_string, ReadError}, + config::{client, server, Revision, CLIENT_CONFIG_PATH, OLD_CONFIG_PATH, SERVER_CONFIG_PATH}, }; use color_print::cprintln; use sqlx::{postgres::PgConnectOptions, ConnectOptions}; use std::{ collections::HashMap, fs, - io::{self, Read, Write}, + io::{self, Write}, path::Path, }; use url::Url; @@ -34,7 +35,7 @@ pub(crate) enum Error { #[derive(thiserror::Error, Debug)] pub(crate) enum ReadYamlConfigError { #[error(transparent)] - ReadFile(#[from] io::Error), + ReadFile(#[from] ReadError), #[error(transparent)] Yaml(#[from] yaml_rust::ScanError), #[error("invalid config ({0})")] @@ -57,12 +58,7 @@ pub(crate) enum WriteTomlConfigError { } fn read_default_yml() -> Result, ReadYamlConfigError> { - let mut default_yml = fs::File::open(OLD_CONFIG_PATH)?; - - let mut buffer = String::new(); - default_yml.read_to_string(&mut buffer)?; - - let content = YamlLoader::load_from_str(&buffer)?; + let content = YamlLoader::load_from_str(&read_file_as_string(OLD_CONFIG_PATH)?)?; if content.is_empty() { return Err(ReadYamlConfigError::InvalidConfig(