Skip to content

Commit

Permalink
Better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Oct 19, 2020
1 parent 7daef78 commit 01305f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "xaction"
version = "0.1.0"
version = "0.2.0"
description = "action-less automation for GitHub actions"
license = "MIT OR Apache-2.0"
repository = "https://github.com/matklad/xaction"
Expand Down
2 changes: 1 addition & 1 deletion examples/ci.rs
Expand Up @@ -22,7 +22,7 @@ fn try_main() -> Result<()> {
cmd!("cargo test --workspace --no-run").run()?;
}

let version = cargo_toml.version();
let version = cargo_toml.version()?;
let tag = format!("v{}", version);
let dry_run = env::var("CI").is_err()
|| git::tag_list()?.contains(&tag)
Expand Down
23 changes: 15 additions & 8 deletions src/lib.rs
Expand Up @@ -2,6 +2,9 @@ use std::{env, path::PathBuf, sync::atomic::AtomicBool, sync::atomic::Ordering,

pub use xshell::*;

pub type Error = Box<dyn std::error::Error>;
pub type Result<T, E = Error> = std::result::Result<T, E>;

pub fn section(name: &'static str) -> Section {
Section::new(name)
}
Expand Down Expand Up @@ -32,28 +35,29 @@ pub struct CargoToml {
}

impl CargoToml {
pub fn version(&self) -> &str {
pub fn version(&self) -> Result<&str> {
self.get("version")
}

pub fn get(&self, field: &str) -> &str {
fn get(&self, field: &str) -> Result<&str> {
for line in self.contents.lines() {
let words = line.split_ascii_whitespace().collect::<Vec<_>>();
match words.as_slice() {
[n, "=", v, ..] if n.trim() == field => {
assert!(v.starts_with('"') && v.ends_with('"'));
return &v[1..v.len() - 1];
return Ok(&v[1..v.len() - 1]);
}
_ => (),
}
}
panic!("can't find `{}` in {}", field, self.path.display())
Err(format!("can't find `{}` in {}", field, self.path.display()))?
}

pub fn publish(&self) -> Result<()> {
let token = env::var("CRATES_IO_TOKEN").unwrap_or("no token".to_string());
let dry_run = dry_run();
cmd!("cargo publish --token {token} {dry_run...}").run()
cmd!("cargo publish --token {token} {dry_run...}").run()?;
Ok(())
}
}

Expand All @@ -63,7 +67,8 @@ pub mod git {
use super::{dry_run, Result};

pub fn current_branch() -> Result<String> {
cmd!("git branch --show-current").read()
let res = cmd!("git branch --show-current").read()?;
Ok(res)
}

pub fn tag_list() -> Result<Vec<String>> {
Expand All @@ -81,12 +86,14 @@ pub mod git {
if dry_run().is_some() {
return Ok(());
}
cmd!("git tag {tag}").run()
cmd!("git tag {tag}").run()?;
Ok(())
}

pub fn push_tags() -> Result<()> {
let dry_run = dry_run();
cmd!("git push --tags {dry_run...}").run()
cmd!("git push --tags {dry_run...}").run()?;
Ok(())
}
}

Expand Down

0 comments on commit 01305f0

Please sign in to comment.