diff --git a/src/command/config.rs b/src/command/config.rs index 881a740..8db715d 100644 --- a/src/command/config.rs +++ b/src/command/config.rs @@ -29,6 +29,14 @@ pub(crate) enum ConfigError { 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)] pub(crate) enum RevisionCheckError { #[error("failed to determine the current config revision ({0})")] @@ -113,12 +121,3 @@ fn current_revision() -> Result { _ => 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(()) -} diff --git a/src/command/config/update.rs b/src/command/config/update.rs index ef15ea8..38342a3 100644 --- a/src/command/config/update.rs +++ b/src/command/config/update.rs @@ -22,6 +22,23 @@ pub(crate) enum UpdateError { V1(#[from] v1::Error), } +pub(super) async fn run(revision: Option) -> 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> { 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); @@ -58,20 +75,3 @@ async fn update_to(revision: Revision) -> Result<(), UpdateError> { } Ok(()) } - -pub(super) async fn run(revision: Option) -> 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, - } -} diff --git a/src/command/config/update/v1.rs b/src/command/config/update/v1.rs index 54c95a9..693ecd3 100644 --- a/src/command/config/update/v1.rs +++ b/src/command/config/update/v1.rs @@ -31,6 +31,43 @@ pub(crate) enum Error { 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!("{} 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!("{} has been created!", CLIENT_CONFIG_PATH); + + Ok(()) +} + /// Errors that can happen while reading `.config/default.yml` #[derive(thiserror::Error, Debug)] pub(crate) enum ReadYamlConfigError { @@ -429,40 +466,3 @@ fn create_new_client_config(meta: Meta) -> Result { 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!("{} 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!("{} has been created!", CLIENT_CONFIG_PATH); - - Ok(()) -}