Skip to content

Commit 404546b

Browse files
authored
Add clean command (#8)
* Add clean command
1 parent fc7e1b8 commit 404546b

8 files changed

Lines changed: 63 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "protofetch"
3-
version = "0.0.7"
3+
version = "0.0.8"
44
edition = "2018"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ OPTIONS:
2727
Print version information
2828
2929
SUBCOMMANDS:
30+
clean Cleans generated proto sources and lock file
3031
fetch Fetches protodep dependencies defined in the toml configuration file
3132
help Print this message or the help of the given subcommand(s)
3233
init Creates an init protofetch setup in provided directory and name
@@ -40,6 +41,7 @@ SUBCOMMANDS:
4041
```toml
4142
name = "repository name"
4243
description = "this is a repository"
44+
proto_out_dir = "proto/src/dir/output"
4345

4446
[repo1]
4547
protocol = "https"
@@ -50,4 +52,9 @@ description = "this is a repository"
5052
protocol = "ssh"
5153
url = "github.com/org/repo2"
5254
revision = "5.2.0"
55+
56+
[another-name]
57+
protocol = "ssh"
58+
url = "github.com/org/repo3"
59+
revision = "a16f097eab6e64f2b711fd4b977e610791376223"
5360
```

src/cli/args.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,47 @@ pub struct CliArgs {
77
#[clap(subcommand)]
88
pub cmd: Command,
99
#[clap(short, long, default_value = "protofetch.toml")]
10-
///location of the protofetch configuration toml
10+
/// location of the protofetch configuration toml
1111
pub module_location: String,
1212
#[clap(short, long, default_value = "protofetch.lock")]
13-
///location of the protofetch lock file
13+
/// location of the protofetch lock file
1414
pub lockfile_location: String,
1515
#[clap(short, long, default_value = ".protofetch_cache")]
16-
///location of the protofetch cache directory
16+
/// location of the protofetch cache directory
1717
pub cache_directory: String,
18+
/// name of the proto source files directory output,
19+
/// this will be used if config is not present in the toml config
20+
#[clap(short, long, default_value = "proto_src")]
21+
pub proto_output_directory: String,
1822
}
1923

2024
#[derive(Debug, Parser)]
2125
pub enum Command {
22-
///Fetches protodep dependencies defined in the toml configuration file
26+
/// Fetches protodep dependencies defined in the toml configuration file
2327
Fetch {
2428
#[clap(short, long)]
2529
///forces re-creation of lock file
2630
force_lock: bool,
2731
///Name of the dependencies source files directory
2832
#[clap(short, long, default_value = "dependencies")]
2933
source_output_directory: String,
30-
///Name of the proto files directory
31-
#[clap(short, long, default_value = "proto_src")]
32-
proto_output_directory: String,
3334
},
34-
///Creates a lock file based on toml configuration file
35+
/// Creates a lock file based on toml configuration file
3536
Lock,
36-
///Creates an init protofetch setup in provided directory and name
37+
/// Creates an init protofetch setup in provided directory and name
3738
Init {
3839
#[clap(default_value = ".")]
3940
directory: String,
4041
#[clap(short, long)]
4142
name: Option<String>,
4243
},
43-
///Migrates a protodep toml file to a protofetch format
44+
/// Migrates a protodep toml file to a protofetch format
4445
Migrate {
4546
#[clap(default_value = ".")]
4647
directory: String,
4748
#[clap(short, long)]
4849
name: Option<String>,
4950
},
51+
/// Cleans generated proto sources and lock file
52+
Clean,
5053
}

src/cli/command_handlers.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
use std::{
1010
env,
1111
error::Error,
12+
fs,
1213
path::{Path, PathBuf},
1314
};
1415

@@ -28,12 +29,12 @@ pub fn do_fetch(
2829
LockFile::from_file(lockfile_path)?
2930
};
3031
let dependencies_out_dir = cache.location.join(dependencies_out_dir);
31-
fetch::fetch(
32-
cache,
33-
&lockfile,
34-
&dependencies_out_dir,
35-
proto_output_directory,
36-
)?;
32+
let proto_out_dir = lockfile
33+
.proto_out_dir
34+
.as_ref()
35+
.map(Path::new)
36+
.unwrap_or(proto_output_directory);
37+
fetch::fetch(cache, &lockfile, &dependencies_out_dir, proto_out_dir)?;
3738

3839
Ok(())
3940
}
@@ -108,6 +109,26 @@ pub fn do_migrate(
108109
Ok(())
109110
}
110111

112+
pub fn do_clean(lockfile_path: &Path, proto_output_directory: &Path) -> Result<(), Box<dyn Error>> {
113+
if lockfile_path.exists() {
114+
let lockfile = LockFile::from_file(lockfile_path).expect("Lockfile was not found");
115+
let proto_out_dir = lockfile
116+
.proto_out_dir
117+
.as_ref()
118+
.map(Path::new)
119+
.unwrap_or(proto_output_directory);
120+
info!(
121+
"Cleaning protofetch proto source files folder {}.",
122+
&proto_out_dir.to_string_lossy()
123+
);
124+
fs::remove_dir_all(proto_out_dir)?;
125+
fs::remove_file(lockfile_path).expect("Lockfile could not be removed");
126+
Ok(())
127+
} else {
128+
Ok(())
129+
}
130+
}
131+
111132
/// Name if present otherwise attempt to extract from directory
112133
fn build_module_name(name: Option<&str>, path: &Path) -> Result<String, Box<dyn Error>> {
113134
match name {

src/fetch.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,9 @@ pub fn fetch<Cache: RepositoryCache>(
9393
cache: &Cache,
9494
lockfile: &LockFile,
9595
dependencies_out_dir: &Path,
96-
proto_output_directory: &Path,
96+
proto_out_dir: &Path,
9797
) -> Result<(), FetchError> {
9898
info!("Fetching dependencies source files...");
99-
let proto_out_dir = lockfile
100-
.proto_out_dir
101-
.as_ref()
102-
.map(Path::new)
103-
.unwrap_or(proto_output_directory);
10499

105100
if !dependencies_out_dir.exists() {
106101
std::fs::create_dir_all(dependencies_out_dir)?;
@@ -160,12 +155,12 @@ pub fn copy_proto_files(
160155
))
161156
})?;
162157
let proto_out_dist = proto_out_dir.join(&proto_src);
163-
let prefix = proto_out_dist
164-
.parent()
165-
.ok_or_else(|| FetchError::BadFilePath(format!(
158+
let prefix = proto_out_dist.parent().ok_or_else(|| {
159+
FetchError::BadFilePath(format!(
166160
"Bad parent dest file for {}",
167161
&proto_out_dist.to_string_lossy()
168-
)))?;
162+
))
163+
})?;
169164
std::fs::create_dir_all(prefix)?;
170165
fs::copy(proto_file_source.as_path(), proto_out_dist.as_path())?;
171166
}
@@ -184,12 +179,12 @@ fn find_proto_files(dir: &Path) -> Result<Vec<PathBuf>, FetchError> {
184179
let rec_call = find_proto_files(&path)?;
185180
files.append(&mut rec_call.clone());
186181
} else if let Some(extension) = path.extension() {
187-
if extension == "proto" {
188-
files.push(path);
189-
}
182+
if extension == "proto" {
183+
files.push(path);
190184
}
191185
}
192186
}
187+
}
193188
Ok(files)
194189
}
195190

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ fn run() -> Result<(), Box<dyn Error>> {
2525
let cache = ProtofetchCache::new(PathBuf::from(&cli_args.cache_directory))?;
2626
let module_path = Path::new(&cli_args.module_location);
2727
let lockfile_path = Path::new(&cli_args.lockfile_location);
28+
let proto_output_directory = Path::new(&cli_args.proto_output_directory);
2829

2930
match cli_args.cmd {
3031
cli::args::Command::Fetch {
3132
force_lock,
3233
source_output_directory,
33-
proto_output_directory,
3434
} => {
3535
let dependencies_out_dir = Path::new(&source_output_directory);
3636
let proto_output_directory = Path::new(&proto_output_directory);
@@ -54,5 +54,8 @@ fn run() -> Result<(), Box<dyn Error>> {
5454
cli::args::Command::Migrate { directory, name } => {
5555
command_handlers::do_migrate(&directory, name.as_deref(), &cli_args.module_location)
5656
}
57+
cli::args::Command::Clean => {
58+
command_handlers::do_clean(lockfile_path, proto_output_directory)
59+
}
5760
}
5861
}

src/proto_repository.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ impl ProtoRepository {
4848
let rendered_revision = revision.to_string();
4949
let result = self
5050
.git_repo
51-
.revparse_single(&format!("{}:module.toml", rendered_revision));
51+
.revparse_single(&format!("{}:protofetch.toml", rendered_revision));
5252

5353
match result {
5454
Err(e) if e.code() == git2::ErrorCode::NotFound => {
55-
log::warn!("Couldn't find module.toml, assuming module has no dependencies");
55+
log::debug!("Couldn't find protofetch.toml, assuming module has no dependencies");
5656
Ok(Descriptor {
5757
name: dep_name.to_string(),
5858
description: None,

0 commit comments

Comments
 (0)