Skip to content

Commit

Permalink
Refactor code to update scout version to 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jun 12, 2024
1 parent 550db50 commit 79744a5
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scout"
version = "0.1.10"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ You have to pass the directory to scan as an argument.
Optional arguments:

- `-o` or `--output`: to specify the output file, by default it will be `./output.json`.
- `-v` or `--verbose`: to display more information in the terminal.
- `-p` or `--print`: to print the output in the terminal (this will disable the output file and verbose).

```bash
scout /path/to/directory -o=/path/to/output.json
scout /path/to/directory -o=/path/to/output.json -v
```

You will have an output, like this:
Expand All @@ -47,6 +49,26 @@ And an output file, like this:
}
```

#### Print

If you want to print the output in the terminal, you can use the `-p` or `--print` argument.

```bash
scout /path/to/directory -p
```

You will have an output, like this:

```bash
tests/data/file-3.md
tests/data/file-2.md
tests/data/file.jpg
tests/data/file.mkv
tests/data/file-1.md
tests/data/nested/file-nested-2.md
tests/data/nested/file-nested-1.md
```

### Files excluded

- Files with dots at the beginning of their names, like `.gitignore`.
Expand Down
92 changes: 75 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,93 @@ fn main() {
.help("Sets the output file path as JSON, default is './output.json'")
.takes_value(true),
)
.arg(
Arg::new("verbose")
.short('v')
.long("verbose")
.help("Enables verbose mode")
.takes_value(false),
)
.arg(
Arg::new("print")
.short('p')
.long("print")
.help("Prints the list of files to the console, this option will clear all other console outputs")
.takes_value(false),
)
.get_matches();

let directory_path = matches.value_of("directory").unwrap();
let output_file_path = matches.value_of("output").unwrap_or("./output.json");
let current_dir = std::env::current_dir().unwrap();
let default_json_path = format!("{}/output.json", current_dir.display());

let output_file_path = matches.value_of("output").unwrap_or(&default_json_path);
if !output_file_path.ends_with(".json") {
println!("Output file must be a JSON file.");
return;
}

println!("Scanning {} directory...", directory_path);
let verbose = matches.is_present("verbose");
let print = matches.is_present("print");

if !print {
const VERSION: &str = env!("CARGO_PKG_VERSION");
println!("scout v{}", VERSION);
}

if verbose && !print {
println!("");
println!("Scanning {} directory...", directory_path);
}

// check if the directory exists
if !std::path::Path::new(directory_path).exists() {
println!("Directory does not exist.");
return;
}

// check if permission is granted
if !std::path::Path::new(directory_path).is_dir() {
println!("Permission denied.");
return;
}

let start = std::time::Instant::now();
let date = chrono::Local::now().to_string();
let files = list_files_recursive(directory_path);

println!("Scan completed!");
if verbose && !print {
println!("Scan completed!");

println!("");
println!("Directory: {}", directory_path);
println!("Date: {:?}", date);
println!("Time in seconds: {:?}", start.elapsed());
println!("Total files: {}", files.len());
}

if !print {
println!("");
println!("Output file: {}", output_file_path);
}

println!("");
println!("Directory: {}", directory_path);
println!("Date: {:?}", date);
println!("Time in seconds: {:?}", start.elapsed());
println!("Total files: {}", files.len());
println!("Output file: {}", output_file_path);
if print {
for file in &files {
println!("{}", file);
}
}

let file_list = FileList {
path: directory_path.to_string(),
date: date,
time_seconds: start.elapsed().as_secs().to_string(),
total_files: files.len(),
files,
};
if !print {
let file_list = FileList {
path: directory_path.to_string(),
date: date,
time_seconds: start.elapsed().as_secs().to_string(),
total_files: files.len(),
files,
};

to_json(&file_list, output_file_path);
to_json(&file_list, output_file_path);
}
}

fn to_json(file_list: &FileList, output_file: &str) -> () {
Expand Down
3 changes: 3 additions & 0 deletions testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

```bash
cargo test
rm -rf ./target
cargo build --release
rm -f ~/Downloads/scout
cp target/release/scout ~/Downloads
~/Downloads/scout ~/Downloads
```

0 comments on commit 79744a5

Please sign in to comment.