feat: Add config overwrite confirmation
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
use crate::commands::configure::{config_option::ConfigOption, s3::S3Config};
|
||||
use crate::commands::configure::{
|
||||
config_option::{ConfigOption, ConfigOptionCli},
|
||||
configurable::Configurable,
|
||||
s3::S3Config,
|
||||
};
|
||||
use dialoguer::{Confirm, theme::ColorfulTheme};
|
||||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, fs};
|
||||
@@ -17,6 +22,9 @@ impl Config {
|
||||
active_s3: None,
|
||||
}
|
||||
}
|
||||
pub fn exists(&self, name: &String) -> bool {
|
||||
self.items.contains_key(name)
|
||||
}
|
||||
pub fn save(&self) -> anyhow::Result<()> {
|
||||
let json = serde_json::to_string(self)?;
|
||||
let save_path = dirs::config_dir()
|
||||
@@ -69,3 +77,29 @@ impl Config {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn manage_configuration(
|
||||
config: &mut Config,
|
||||
name: &String,
|
||||
option: &ConfigOptionCli,
|
||||
) -> anyhow::Result<()> {
|
||||
if config.exists(&name) {
|
||||
let confirm = Confirm::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(format!(
|
||||
"An entry already exists with the name \"{}\". Would you like to overwrite it?",
|
||||
&name
|
||||
))
|
||||
.interact()?;
|
||||
if !confirm {
|
||||
return Err(anyhow::anyhow!("User cancelled action"));
|
||||
}
|
||||
}
|
||||
config.add_item(
|
||||
name.clone(),
|
||||
match option {
|
||||
ConfigOptionCli::Server(server_config) => server_config.clone().configure().await?,
|
||||
ConfigOptionCli::S3(s3_config_cli) => s3_config_cli.clone().configure().await?,
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+4
-9
@@ -1,5 +1,4 @@
|
||||
use crate::commands::configure::config_option::ConfigOptionCli;
|
||||
use crate::commands::configure::configurable::Configurable;
|
||||
use crate::commands::configure::config::manage_configuration;
|
||||
use crate::{
|
||||
cli::{Cli, Commands},
|
||||
commands::configure::config::Config,
|
||||
@@ -19,13 +18,9 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
let mut config = Config::read();
|
||||
match &cli.command {
|
||||
Commands::Configure { name, option } => config.add_item(
|
||||
name.clone(),
|
||||
match option {
|
||||
ConfigOptionCli::Server(server_config) => server_config.clone().configure().await?,
|
||||
ConfigOptionCli::S3(s3_config_cli) => s3_config_cli.clone().configure().await?,
|
||||
},
|
||||
),
|
||||
Commands::Configure { name, option } => {
|
||||
manage_configuration(&mut config, name, option).await?
|
||||
}
|
||||
Commands::Upload(info) => {
|
||||
upload::interface::upload(info, config).await?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user