Skip to content

Commit

Permalink
Merge pull request #5 from rust-playground/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn authored Nov 5, 2021
2 parents cc8a2f8 + 6a81838 commit 1c36ef3
Show file tree
Hide file tree
Showing 36 changed files with 327 additions and 372 deletions.
Binary file added .DS_Store
Binary file not shown.
50 changes: 50 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Lint & Test
on:
pull_request:
types: [opened, edited, reopened, synchronize]
jobs:
test:
strategy:
matrix:
platform: [macos-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Rust Stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Install Cargo
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy

- name: Checkout code
uses: actions/checkout@v2

- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

- name: Lint
uses: actions-rs/clippy@master
with:
args: --all-features --all-targets

- name: Test
run: cargo test --all-features
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

15 changes: 10 additions & 5 deletions alfred-workflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ name = "alfred-workflow"
description = "This contains common and reusable abstractions for creating workflows."
repository = "https://github.com/rust-playground/alfred-workflows-rs/tree/master/alfred-workflow"
license = " MIT"
version = "1.1.0"
version = "1.2.0"
authors = ["Dean Karn <dean.karn@gmail.com>"]
edition = "2018"
readme = "README.md"
keywords = ["alfred", "workflow"]
categories = ["development-tools"]

[dependencies]
alfred = "4.0.1"
dirs = "2.0.2"
failure = "0.1.5"
rusqlite = {version = "0.20.0",features = ["chrono", "bundled"]}
alfred = "4.0.2"
dirs = "4.0.0"
anyhow = "1.0.44"

[dependencies.rusqlite]
features = [
"bundled-full",
]
version = "0.26.1"
13 changes: 6 additions & 7 deletions alfred-workflow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This contains common abstractions for reuse in multiple workflows
//!
use alfred::{json, Item};
use failure::{format_err, Error};
use anyhow::{anyhow, Error};
use rusqlite::Connection;
use std::{fs, io::Write};

Expand All @@ -17,9 +17,8 @@ use std::{fs, io::Write};
/// # Examples
///
/// ```
/// use failure::Error;
/// use anyhow::Error;
/// use rusqlite::Connection;
/// use rusqlite::NO_PARAMS;
///
/// fn main() -> Result<(), Error> {
/// let conn = alfred_workflow::open_database_or_else("myworkflow", create_tables)?;
Expand All @@ -32,7 +31,7 @@ use std::{fs, io::Write};
/// key TEXT NOT NULL PRIMARY KEY,
/// value TEXT NOT NULL
/// );",
/// NO_PARAMS,
/// [],
/// )?;
/// Ok(())
/// }
Expand All @@ -43,7 +42,7 @@ where
{
let conn: Connection;
let path = dirs::home_dir()
.ok_or_else(|| format_err!("Impossible to get your home dir!"))?
.ok_or_else(|| anyhow!("Impossible to get your home dir!"))?
.join(".alfred")
.join("workflows")
.join(name);
Expand All @@ -69,7 +68,7 @@ where
/// ```
/// use alfred::{json, Item};
/// use std::{io, io::Write};
/// use failure::Error;
/// use anyhow::Error;
///
/// fn main() -> Result<(), Error> {
/// let item = alfred::ItemBuilder::new("settings")
Expand All @@ -83,5 +82,5 @@ where
W: Write,
{
json::write_items(writer, &items[..])
.map_err(|e| format_err!("failed to write alfred items->json: {}", e))
.map_err(|e| anyhow!("failed to write alfred items->json: {}", e))
}
26 changes: 13 additions & 13 deletions buildkite-workflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,38 @@ license = "MIT"
name = "buildkite-workflow"
readme = "README.md"
repository = "https://github.com/rust-playground/alfred-workflows-rs/tree/master/buildkite-workflow"
version = "1.0.0"
version = "1.1.0"

[[bin]]
name = "buildkite-workflow"
path = "src/bin/main.rs"

[dependencies]
alfred = "4.0.2"
dirs = "2.0.2"
regex = "1.3.1"
serde_json = "1.0.41"
snafu = "0.6.0"
structopt = "0.3.4"
dirs = "4.0.0"
regex = "1.5.4"
serde_json = "1.0.68"
structopt = "0.3.25"
anyhow = "1.0.44"
thiserror = "1.0.30"

[dependencies.chrono]
features = ["serde"]
version = "0.4.9"
version = "0.4.19"

[dependencies.reqwest]
features = ["rustls-tls"]
version = "0.9.22"
features = ["rustls-tls","blocking", "json"]
version = "0.11.6"

[dependencies.rusqlite]
features = [
"chrono",
"bundled",
"bundled-full",
]
version = "0.20.0"
version = "0.26.1"

[dependencies.serde]
features = ["derive"]
version = "1.0.102"
version = "1.0.130"

[lib]
name = "buildkite_workflow_lib"
Expand Down
8 changes: 4 additions & 4 deletions buildkite-workflow/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alfred::{json, Item};
use anyhow::Error;
use buildkite_workflow_lib::workflow::BuildkiteWorkflow;
use std::error::Error;
use std::io::Write;
use std::process::Command;
use std::{env, io};
Expand All @@ -14,7 +14,7 @@ struct Opt {
args: Vec<String>,
}

fn main() -> Result<(), Box<dyn Error>> {
fn main() -> Result<(), Error> {
let opt = Opt::from_args();

let api_key = env::var("API_KEY")?;
Expand Down Expand Up @@ -64,10 +64,10 @@ fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn write_items<W>(writer: W, items: &[Item]) -> Result<(), Box<dyn Error>>
fn write_items<W>(writer: W, items: &[Item]) -> Result<(), Error>
where
W: Write,
{
json::write_items(writer, &items[..])?;
json::write_items(writer, items)?;
Ok(())
}
19 changes: 6 additions & 13 deletions buildkite-workflow/src/buildkite_api/errors.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use snafu::Snafu;

use thiserror::Error;
pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
#[derive(Debug, Error)]
pub enum Error {
#[snafu(display("Database error: {}", err))]
HTTP { err: String },

#[snafu(display("Reqwest error: {}", err))]
ReqwestError { err: reqwest::Error },
}
#[error("HTTP error: {}", _0)]
Http(String),

impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Error::ReqwestError { err: err }
}
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
}
30 changes: 8 additions & 22 deletions buildkite-workflow/src/buildkite_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,37 @@ impl<'a> BuildkiteAPI<'a> {

#[inline]
fn fetch_organizations(&self, url: &str) -> Result<OrganizationResponse> {
let mut response = reqwest::Client::new()
let response = reqwest::blocking::Client::new()
.get(url)
.bearer_auth(self.token)
.header(CONTENT_TYPE, "application/json")
.send()?;

if !response.status().is_success() {
return Err(Error::HTTP {
err: response.text()?,
});
return Err(Error::Http(response.text()?));
}

let results: Vec<Organization> = response.json()?;
let link = response.headers().get(LINK);
let next = self.extract_next(link);

let results: Vec<Organization> = response.json()?;
Ok(OrganizationResponse { next, results })
}

#[inline]
fn fetch_pipelines(&self, url: &str) -> Result<PipelineResponse> {
let mut response = reqwest::Client::new()
let response = reqwest::blocking::Client::new()
.get(url)
.bearer_auth(self.token)
.header(CONTENT_TYPE, "application/json")
.send()?;

if !response.status().is_success() {
return Err(Error::HTTP {
err: response.text()?,
});
return Err(Error::Http(response.text()?));
}

let results: Vec<Pipeline> = response.json()?;
let link = response.headers().get(LINK);
let next = self.extract_next(link);

let results: Vec<Pipeline> = response.json()?;
Ok(PipelineResponse { next, results })
}

Expand All @@ -102,11 +96,7 @@ impl<'a> Iterator for OrganizationsIter<'a> {
type Item = Result<Vec<Organization>>;

fn next(&mut self) -> Option<Self::Item> {
if self.next.is_none() {
return None;
}

let response = self.api.fetch_organizations(&self.next.as_ref().unwrap());
let response = self.api.fetch_organizations(self.next.as_ref()?);
if response.is_err() {
return Some(Err(response.err().unwrap()));
}
Expand All @@ -130,11 +120,7 @@ impl<'a> Iterator for PipelinesIter<'a> {
type Item = Result<Vec<Pipeline>>;

fn next(&mut self) -> Option<Self::Item> {
if self.next.is_none() {
return None;
}

let response = self.api.fetch_pipelines(&self.next.as_ref().unwrap());
let response = self.api.fetch_pipelines(self.next.as_ref()?);
if response.is_err() {
return Some(Err(response.err().unwrap()));
}
Expand Down
16 changes: 4 additions & 12 deletions buildkite-workflow/src/database/errors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use snafu::Snafu;
use thiserror::Error;

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

#[derive(Debug, Snafu)]
#[derive(Debug, Error)]
pub enum Error {
#[snafu(display("Database error: {}", err))]
Sqlite { err: String },
}

impl From<rusqlite::Error> for Error {
fn from(err: rusqlite::Error) -> Self {
Error::Sqlite {
err: err.to_string(),
}
}
#[error(transparent)]
Sqlite(#[from] rusqlite::Error),
}
9 changes: 4 additions & 5 deletions buildkite-workflow/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod models;

use crate::database::models::Pipeline;
use errors::Result;
use rusqlite::{Connection, ToSql, NO_PARAMS};
use rusqlite::{Connection, ToSql};

pub struct DbContext {
conn: Connection,
Expand All @@ -18,20 +18,19 @@ impl DbContext {

#[inline]
pub fn run_migrations(&self) -> Result<()> {
self.conn.execute(
self.conn.execute_batch(
"CREATE TABLE IF NOT EXISTS pipelines (
unique_name TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
url TEXT NOT NULL
);",
NO_PARAMS,
)?;
Ok(())
}

#[inline]
pub fn delete_pipelines(&self) -> Result<()> {
self.conn.execute("DELETE FROM pipelines;", NO_PARAMS)?;
self.conn.execute("DELETE FROM pipelines;", [])?;
Ok(())
}

Expand Down Expand Up @@ -85,7 +84,7 @@ impl DbContext {
#[inline]
pub fn optimize(&self) -> Result<()> {
// since this workflow is READ heavy, let's optimize the SQLite indexes and DB
self.conn.execute("VACUUM;", NO_PARAMS)?;
self.conn.execute("VACUUM;", [])?;
Ok(())
}
}
Loading

0 comments on commit 1c36ef3

Please sign in to comment.