Skip to content

Commit

Permalink
Merge pull request #8 from adorton-adobe/cache-control-cli
Browse files Browse the repository at this point in the history
Cache control cli tweaks from @adorton-adobe, fixes #5 and fixes #7.
  • Loading branch information
adobeDan authored Jan 16, 2021
2 parents 0058da6 + 32ad502 commit a126b29
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 11 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ openssl-probe = "0.1.2"
#sqlx = { path="../sqlx", features = [ "runtime-tokio-native-tls", "sqlite" ] }
url = "2.1.1"
sys-info = "0.7.0"
dialoguer = "0.7.1"

[dependencies.sqlx]
git = "https://github.com/adorton-adobe/sqlx"
branch = "tokio-1.0"
features = [ "runtime-tokio-native-tls", "sqlite" ]
features = [ "runtime-tokio-native-tls", "sqlite" ]
20 changes: 15 additions & 5 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ it.
*/
use crate::cops::{Kind, Request as CRequest, Response as CResponse};
use crate::settings::Settings;
use dialoguer::Confirm;
use log::{debug, error, info};
use sqlx::{
sqlite::{SqlitePool, SqlitePoolOptions},
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Cache {
}

pub async fn control(
&self, clear: Option<bool>, export_file: Option<String>,
&self, clear: bool, yes: bool, export_file: Option<String>,
import_file: Option<String>,
) -> Result<(), sqlx::Error> {
if !self.enabled {
Expand All @@ -70,13 +71,22 @@ impl Cache {
println!("cache-control: Requesting export of cache to '{}'", path);
eprintln!("cache-control: Export of cache not yet supported, sorry");
}
if let Some(clear) = clear {
if clear {
if clear {
let confirm = match yes {
true => true,
false => Confirm::new()
.with_prompt(
"Really clear the cache? This operation cannot be undone.",
)
.default(false)
.show_default(true)
.interact()
.unwrap(),
};
if confirm {
sqlx::query(CLEAR_ALL).execute(pool).await?;
println!("cache-control: Cache has been cleared.");
info!("Cache at '{}' has been cleared", db_name);
} else {
eprintln!("cache-control: To clear the cache, use --clear yes");
}
}
Ok(())
Expand Down
14 changes: 12 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,24 @@ pub enum Opt {
/// Path to optional config file
config_file: Option<String>,

#[structopt(long, parse(try_from_str = parse_bool))]
#[structopt(short = "C", long)]
/// Path to cache file
cache_file: Option<String>,

#[structopt(long)]
/// Whether to clear the cache (dangerous!)
clear: Option<bool>,
clear: bool,

#[structopt(short)]
/// Bypass confirmation prompts
yes: bool,

#[structopt(short, long)]
/// Export cache to a file (not yet implemented)
export_file: Option<String>,

#[structopt(short, long)]
/// Import cache from a file (not yet implemented)
import_file: Option<String>,
},
}
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
cli::Opt::CacheControl {
config_file,
cache_file,
clear,
yes,
export_file,
import_file,
} => {
let mut conf = Settings::from_cache_control(config_file)?;
let mut conf = Settings::from_cache_control(config_file, cache_file)?;
conf.validate()?;
let cache = Cache::new_from(&conf).await?;
Cache::control(&cache, clear, export_file, import_file).await?;
Cache::control(&cache, clear, yes, export_file, import_file).await?;
debug!("conf: {:?}", conf);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl Settings {
s.try_into()
}

pub fn from_cache_control(config_file: Option<String>) -> Result<Self, ConfigError> {
pub fn from_cache_control(
config_file: Option<String>, cache_file: Option<String>,
) -> Result<Self, ConfigError> {
let mut s = Config::new();
s.merge(ConfigFile::from_str(
include_str!("res/defaults.toml"),
Expand All @@ -86,6 +88,9 @@ impl Settings {
if let Some(filename) = config_file {
s.merge(ConfigFile::with_name(filename.as_str()))?;
}
if let Some(cache_file) = cache_file {
s.set("cache.cache_file_path", cache_file)?;
}
// force enablement of the cache if doing cache control
s.set("proxy.mode", "cache")?;
s.try_into()
Expand Down

0 comments on commit a126b29

Please sign in to comment.