Skip to content

Commit

Permalink
fix: Lift the process exit to main
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpleBooth committed Oct 26, 2023
1 parent 7b21b86 commit 5f5ac06
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ versioned-files update 1.2.3
Then you will see the following error:

```text, verify(stream=stderr)
ERROR: No versioned-files.yml file found.
Error: No versioned-files.yml file found.
```
6 changes: 3 additions & 3 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ locations:
versioned-files update 1.2.3
```
```shell, verify(stream=stderr)
ERROR: Invalid config file schema: locations: unknown variant `this-type-does-not-exist`, expected `string-pattern` or `cargo` at line 2 column 3
Error: Invalid config file schema: locations: unknown variant `this-type-does-not-exist`, expected `string-pattern` or `cargo` at line 2 column 3
```

#### Location file does not exist
Expand All @@ -30,7 +30,7 @@ versioned-files update 1.2.3
```
```shell, verify()
Updating this-file-does-not-exist.txt...failed
ERROR: Failed to update this-file-does-not-exist.txt: File does not exist
Error: Failed to update this-file-does-not-exist.txt: File does not exist
```

#### Pattern does not contain `{{version}}` placeholder
Expand All @@ -49,5 +49,5 @@ versioned-files update 1.2.3
```
```shell, verify()
Updating file.txt...failed
ERROR: String pattern "This pattern does not contain a placeholder" does not contain the required {{version}} placeholder
Error: String pattern "This pattern does not contain a placeholder" does not contain the required {{version}} placeholder
```
71 changes: 35 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::pin::Pin;
use std::process;

use anyhow::anyhow;

use async_stream::stream;
use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -31,64 +32,62 @@ enum Commands {
}

#[tokio::main]
async fn main() {
async fn main() -> Result<(), anyhow::Error> {
let cli = Cli::parse();

match &cli.command {
Commands::Update { version } => update_command(version).await,
}
Commands::Update { version } => update_command(version).await?,
};

Ok(())
}

#[derive(Debug)]
enum Event {
Errored(String),
UpdateStarted(usize, String),
UpdateSucceeded(usize),
UpdateFailed(usize, String),
enum UpdateEvent {
Started(usize, String),
Succeeded(usize),
Failed(usize, String),
}

async fn update_command(version: &str) {
async fn update_command(version: &str) -> Result<(), anyhow::Error> {
const CONFIG_FILE: &str = "versioned-files.yml";

let config = Config::from_file(CONFIG_FILE);

let mut streams = config.map(|config| config.locations).map_or_else(
|err: anyhow::Error| -> Pin<Box<dyn Stream<Item = Event>>> {
Box::pin(stream! { yield Event::Errored(err.to_string()) })
},
|locations| -> Pin<Box<dyn Stream<Item = Event>>> {
let mut streams = config.map(|config| config.locations).map(
|locations| -> Pin<Box<dyn Stream<Item = UpdateEvent>>> {
Box::pin(futures::stream::select_all(locations.into_iter().map(
|location| update_location(1, version.to_string(), location),
)))
},
);
)?;

let mut result = Ok(());

while let Some(event) = streams.next().await {
match event {
Event::Errored(err) => {
eprintln!("ERROR: {err}");
process::exit(1);
}
Event::UpdateStarted(_, file) => {
UpdateEvent::Started(_, file) => {
print!("Updating {file}...");
}
Event::UpdateSucceeded(_) => {
UpdateEvent::Succeeded(_) => {
println!("success");
}
Event::UpdateFailed(_, message) => {
UpdateEvent::Failed(_, message) => {
println!("failed");
println!("ERROR: {message}");
process::exit(1);
println!("Error: {message}");
result = Err(anyhow!(message));
}
}
}

result
}

fn update_location(
id: usize,
version: String,
location: Location,
) -> Pin<Box<dyn Stream<Item = Event>>> {
) -> Pin<Box<dyn Stream<Item = UpdateEvent>>> {
match location {
StringPattern(location_config) => {
Box::pin(update_string_pattern_location(id, version, location_config))
Expand All @@ -97,17 +96,17 @@ fn update_location(
}
}

fn update_cargo_location(id: usize, version: String) -> impl Stream<Item = Event> {
fn update_cargo_location(id: usize, version: String) -> impl Stream<Item = UpdateEvent> {
stream! {
yield Event::UpdateStarted(id, "Cargo.toml".to_string());
yield UpdateEvent::Started(id, "Cargo.toml".to_string());
match location_types::cargo::update_cargo_version(&version) {
Ok(()) => {
yield Event::UpdateSucceeded(id);
yield Event::UpdateStarted(id, "Cargo.lock".to_string());
yield Event::UpdateSucceeded(id);
yield UpdateEvent::Succeeded(id);
yield UpdateEvent::Started(id, "Cargo.lock".to_string());
yield UpdateEvent::Succeeded(id);
}
Err(err) => {
yield Event::UpdateFailed(id, err.to_string());
yield UpdateEvent::Failed(id, err.to_string());
}
}
}
Expand All @@ -117,15 +116,15 @@ fn update_string_pattern_location(
id: usize,
version: String,
location_config: location_types::string_pattern::Config,
) -> impl Stream<Item = Event> {
) -> impl Stream<Item = UpdateEvent> {
stream! {
yield Event::UpdateStarted(id, location_config.file.clone());
yield UpdateEvent::Started(id, location_config.file.clone());
match location_types::string_pattern::replace_version_with_string_pattern(
&location_config,
&version,
) {
Ok(()) => yield Event::UpdateSucceeded(id),
Err(err) => yield Event::UpdateFailed(id, err.to_string()),
Ok(()) => yield UpdateEvent::Succeeded(id),
Err(err) => yield UpdateEvent::Failed(id, err.to_string()),
}
}
}

0 comments on commit 5f5ac06

Please sign in to comment.