Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd): init does not override config file #203

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .watch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
cat .github/workflows/on-push.yml \
| yq '.jobs | .[] | .steps | .[] | .run | select(. != null)' \
| xargs -I {} bash -c {}
change: "src/**"
change:
- "src/**"
- "tests/**/*.rs"
run_on_init: true

# Moved integration to earlier because it is faster than @nixbuild
Expand Down
116 changes: 116 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ yaml-rust = "0.4.5"
glob="0.2.11"
notify-debouncer-mini = "0.3.0"
nix = "0.26.2"
#clippy = "*"

[dev-dependencies]
assert_cmd = "2.0.14"
predicates = "3.1.0"

[[bin]]
name = "funzzy"
Expand Down
28 changes: 19 additions & 9 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cli::Command;
use crate::errors::FzzError;

use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -33,20 +34,29 @@ impl InitCommand {
}

impl Command for InitCommand {
fn execute(&self) -> Result<(), String> {
let res = match File::create(&self.file_name) {
fn execute(&self) -> Result<(), FzzError> {
if let Ok(_) = File::open(&self.file_name) {
return Err(FzzError::IoConfigError(
"Configuration file already exists (.watch.yaml)".to_string(),
None,
));
}

match File::create(&self.file_name) {
Ok(mut yaml) => {
if let Err(err) = yaml.write_all(DEFAULT_CONTENT.as_ref()) {
return Err(err.to_string());
return Err(FzzError::IoConfigError(
"Failed to write into file".to_string(),
Some(err),
));
}

Ok(())
}
Err(err) => Err(format!("File wasn't created. Cause: {}", err)),
};

res?;

Ok(())
Err(err) => Err(FzzError::IoConfigError(
"File wasn't created".to_string(),
Some(err),
)),
}
}
}
3 changes: 2 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ pub mod watch_non_block;
pub use crate::cli::init::InitCommand;
pub use crate::cli::watch::WatchCommand;
pub use crate::cli::watch_non_block::WatchNonBlockCommand;
use crate::errors::FzzError;

/// # Command interface
///
/// Each command from cli should implement this.
///
pub trait Command {
fn execute(&self) -> Result<(), String>;
fn execute(&self) -> Result<(), FzzError>;
}
10 changes: 7 additions & 3 deletions src/cli/watch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cli::Command;
use crate::cmd;
use crate::errors::FzzError;
use crate::rules;
use crate::stdout;
use crate::watcher;
Expand Down Expand Up @@ -31,7 +32,7 @@ impl WatchCommand {
}

impl Command for WatchCommand {
fn execute(&self) -> Result<(), String> {
fn execute(&self) -> Result<(), FzzError> {
stdout::verbose("Verbose mode enabled.", self.verbose);

let current_dir = std::env::current_dir().unwrap();
Expand Down Expand Up @@ -71,7 +72,7 @@ impl Command for WatchCommand {

let list_of_watched_paths = self.watches.paths_to_watch().unwrap_or_default();

watcher::events(
match watcher::events(
list_of_watched_paths,
|file_changed| {
let time_execution_started = std::time::Instant::now();
Expand Down Expand Up @@ -117,6 +118,9 @@ impl Command for WatchCommand {
}
},
self.verbose,
)
) {
Ok(_) => Ok(()),
Err(err) => Err(FzzError::GenericError(err)),
}
}
}
10 changes: 7 additions & 3 deletions src/cli/watch_non_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate notify;

use crate::cli::Command;
use crate::errors::FzzError;
use crate::stdout;
use crate::watcher;
use crate::watches::Watches;
Expand Down Expand Up @@ -32,7 +33,7 @@ impl WatchNonBlockCommand {

impl Command for WatchNonBlockCommand {
#![allow(unused_assignments)]
fn execute(&self) -> Result<(), String> {
fn execute(&self) -> Result<(), FzzError> {
stdout::verbose("Verbose mode enabled.", self.verbose);

let worker = workers::Worker::new(self.verbose, self.fail_fast, |event| {
Expand All @@ -55,7 +56,7 @@ impl Command for WatchNonBlockCommand {
}

let list_of_watched_paths = self.watches.paths_to_watch().unwrap_or_default();
watcher::events(
match watcher::events(
list_of_watched_paths,
|file_changed| {
if let Some(rules) = self.watches.watch(file_changed) {
Expand All @@ -79,6 +80,9 @@ impl Command for WatchNonBlockCommand {
}
},
self.verbose,
)
) {
Ok(_) => Ok(()),
Err(err) => Err(FzzError::GenericError(err)),
}
}
}
31 changes: 31 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub enum FzzError {
IoConfigError(String, Option<std::io::Error>),
GenericError(String),
}

impl fmt::Display for FzzError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FzzError::IoConfigError(msg, Some(err)) => {
write!(f, "{} \nReason: {}", msg, err)
}
FzzError::IoConfigError(msg, _) => {
write!(f, "Reason: {}", msg)
}
FzzError::GenericError(e) => write!(f, "Error: {}", e),
}
}
}

impl Error for FzzError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
FzzError::IoConfigError(_, Some(e)) => Some(e),
_ => None,
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// #![plugin(clippy)]
pub mod cli;
pub mod cmd;
pub mod errors;
pub mod rules;
pub mod stdout;
pub mod watcher;
Expand Down
Loading
Loading