This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathclean.rs
More file actions
78 lines (70 loc) · 2.17 KB
/
clean.rs
File metadata and controls
78 lines (70 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2023, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
use clap::ArgGroup;
use clap::Args;
use clap::ValueHint;
use stack_graphs::storage::SQLiteWriter;
use std::path::Path;
use std::path::PathBuf;
#[derive(Args)]
#[clap(group(
ArgGroup::new("paths")
.required(true)
.args(&["source_paths", "all", "delete"]),
))]
pub struct CleanArgs {
/// Source file or directory paths for which to clean indexing data.
#[clap(
value_name = "SOURCE_PATH",
value_hint = ValueHint::AnyPath,
)]
pub source_paths: Vec<PathBuf>,
/// Remove all data from the database.
#[clap(long, short = 'a')]
pub all: bool,
/// Delete the database file.
#[clap(long)]
pub delete: bool,
#[clap(long, short = 'v')]
pub verbose: bool,
}
impl CleanArgs {
pub fn run(self, db_path: &Path) -> anyhow::Result<()> {
if self.delete {
self.delete(db_path)
} else {
self.clean(db_path)
}
}
fn delete(&self, db_path: &Path) -> anyhow::Result<()> {
if !db_path.exists() {
return Ok(());
}
std::fs::remove_file(db_path)?;
if self.verbose {
println!("deleted database {}", db_path.display());
}
Ok(())
}
fn clean(&self, db_path: &Path) -> anyhow::Result<()> {
let mut db = SQLiteWriter::open(&db_path)?;
let count = if self.all {
db.clean_all()?
} else {
let mut count = 0usize;
for path in &self.source_paths {
let path = path.canonicalize()?;
count += db.clean_file_or_directory(&path)?;
}
count
};
if self.verbose {
println!("removed data for {} files", count);
}
Ok(())
}
}