feat: add atuin-run crate

This commit is contained in:
Ellie Huxtable 2024-04-22 09:07:50 +01:00
parent 7c9f376cf7
commit ceae49da60
7 changed files with 483 additions and 15 deletions

337
Cargo.lock generated
View File

@ -207,6 +207,7 @@ dependencies = [
"atuin-daemon",
"atuin-dotfiles",
"atuin-history",
"atuin-run",
"atuin-server",
"atuin-server-postgres",
"base64 0.22.1",
@ -383,6 +384,13 @@ dependencies = [
"whoami",
]
[[package]]
name = "atuin-run"
version = "0.1.0"
dependencies = [
"comrak",
]
[[package]]
name = "atuin-server"
version = "18.3.0"
@ -612,6 +620,30 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]]
name = "bit-set"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -750,7 +782,8 @@ dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
"strsim 0.11.1",
"terminal_size",
]
[[package]]
@ -844,6 +877,26 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "comrak"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0436149c9f6a1935b13306206c739b1ba84fa81f551b5eb87fc2ca7a13700af"
dependencies = [
"clap",
"derive_builder",
"entities",
"memchr",
"once_cell",
"regex",
"shell-words",
"slug",
"syntect",
"typed-arena",
"unicode_categories",
"xdg",
]
[[package]]
name = "config"
version = "0.13.4"
@ -917,6 +970,15 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "crc32fast"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@ -1031,14 +1093,38 @@ dependencies = [
"syn 2.0.66",
]
[[package]]
name = "darling"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
dependencies = [
"darling_core 0.14.4",
"darling_macro 0.14.4",
]
[[package]]
name = "darling"
version = "0.20.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83b2eb4d90d12bdda5ed17de686c2acb4c57914f8f921b8da7e112b5a36f3fe1"
dependencies = [
"darling_core",
"darling_macro",
"darling_core 0.20.9",
"darling_macro 0.20.9",
]
[[package]]
name = "darling_core"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim 0.10.0",
"syn 1.0.109",
]
[[package]]
@ -1051,17 +1137,28 @@ dependencies = [
"ident_case",
"proc-macro2",
"quote",
"strsim",
"strsim 0.11.1",
"syn 2.0.66",
]
[[package]]
name = "darling_macro"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [
"darling_core 0.14.4",
"quote",
"syn 1.0.109",
]
[[package]]
name = "darling_macro"
version = "0.20.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178"
dependencies = [
"darling_core",
"darling_core 0.20.9",
"quote",
"syn 2.0.66",
]
@ -1111,6 +1208,43 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "derive_builder"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8"
dependencies = [
"derive_builder_macro",
]
[[package]]
name = "derive_builder_core"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f"
dependencies = [
"darling 0.14.4",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "derive_builder_macro"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
dependencies = [
"derive_builder_core",
"syn 1.0.109",
]
[[package]]
name = "deunicode"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00"
[[package]]
name = "diff"
version = "0.1.13"
@ -1219,6 +1353,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "entities"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca"
[[package]]
name = "env_filter"
version = "0.1.0"
@ -1295,6 +1435,16 @@ dependencies = [
"once_cell",
]
[[package]]
name = "fancy-regex"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b95f7c0680e4142284cf8b22c14a476e87d61b004a3a0861872b32ef7ead40a2"
dependencies = [
"bit-set",
"regex",
]
[[package]]
name = "fastrand"
version = "2.1.0"
@ -1324,6 +1474,16 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flate2"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "flume"
version = "0.11.0"
@ -1987,6 +2147,18 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "line-wrap"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd1bc4d24ad230d21fb898d1116b1801d7adfc449d42026475862ab48b11e70e"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@ -2392,6 +2564,28 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "onig"
version = "6.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c4b31c8722ad9171c6d77d3557db078cab2bd50afcc9d09c8b315c59df8ca4f"
dependencies = [
"bitflags 1.3.2",
"libc",
"once_cell",
"onig_sys",
]
[[package]]
name = "onig_sys"
version = "69.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b829e3d7e9cc74c7e315ee8edb185bf4190da5acde74afd7fc59c35b1f086e7"
dependencies = [
"cc",
"pkg-config",
]
[[package]]
name = "opaque-debug"
version = "0.3.1"
@ -2412,9 +2606,9 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "os_pipe"
version = "1.1.5"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57119c3b893986491ec9aa85056780d3a0f3cf4da7cc09dd3650dbd6c6738fb9"
checksum = "29d73ba8daf8fac13b0501d1abeddcfe21ba7401ada61a819144b6c2a4f32209"
dependencies = [
"libc",
"windows-sys 0.52.0",
@ -2571,6 +2765,20 @@ version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db23d408679286588f4d4644f965003d056e3dd5abcaaa938116871d7ce2fee7"
[[package]]
name = "plist"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9d34169e64b3c7a80c8621a48adaf44e0cf62c78a9b25dd9dd35f1881a17cf9"
dependencies = [
"base64 0.21.7",
"indexmap 2.2.6",
"line-wrap",
"quick-xml",
"serde",
"time",
]
[[package]]
name = "poly1305"
version = "0.8.0"
@ -2698,6 +2906,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "quick-xml"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33"
dependencies = [
"memchr",
]
[[package]]
name = "quote"
version = "1.0.36"
@ -3133,6 +3350,15 @@ dependencies = [
"cipher",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "schannel"
version = "0.1.23"
@ -3274,7 +3500,7 @@ version = "3.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65569b702f41443e8bc8bbb1c5779bd0450bbe723b56198980e80ec45780bce2"
dependencies = [
"darling",
"darling 0.20.9",
"proc-macro2",
"quote",
"syn 2.0.66",
@ -3311,6 +3537,12 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "shellexpand"
version = "3.1.0"
@ -3375,6 +3607,16 @@ dependencies = [
"autocfg",
]
[[package]]
name = "slug"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bd94acec9c8da640005f8e135a39fc0372e74535e6b368b7a04b875f784c8c4"
dependencies = [
"deunicode",
"wasm-bindgen",
]
[[package]]
name = "smallvec"
version = "1.13.2"
@ -3676,6 +3918,12 @@ dependencies = [
"unicode-properties",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strsim"
version = "0.11.1"
@ -3744,6 +3992,29 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
[[package]]
name = "syntect"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "874dcfa363995604333cf947ae9f751ca3af4522c60886774c4963943b4746b1"
dependencies = [
"bincode",
"bitflags 1.3.2",
"fancy-regex",
"flate2",
"fnv",
"once_cell",
"onig",
"plist",
"regex-syntax 0.8.3",
"serde",
"serde_derive",
"serde_json",
"thiserror",
"walkdir",
"yaml-rust",
]
[[package]]
name = "sysinfo"
version = "0.30.12"
@ -3792,6 +4063,16 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "terminal_size"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
dependencies = [
"rustix",
"windows-sys 0.48.0",
]
[[package]]
name = "thiserror"
version = "1.0.61"
@ -4166,6 +4447,12 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "typed-arena"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"
[[package]]
name = "typed-builder"
version = "0.18.2"
@ -4315,6 +4602,16 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.1"
@ -4504,6 +4801,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "winapi-wsapoll"
version = "0.1.2"
@ -4736,12 +5042,27 @@ dependencies = [
"nix",
]
[[package]]
name = "xdg"
version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546"
[[package]]
name = "xml-rs"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791978798f0597cfc70478424c2b4fdc2b7a8024aaff78497ef00f24ef674193"
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "yansi"
version = "0.5.1"

View File

@ -0,0 +1,15 @@
[package]
name = "atuin-run"
edition = "2021"
version = "0.1.0"
authors.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
readme.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
comrak = "0.22"

View File

@ -0,0 +1,2 @@
pub mod markdown;

View File

@ -0,0 +1,100 @@
use comrak::{
arena_tree::NodeEdge,
nodes::{AstNode, NodeValue},
parse_document, Arena, ComrakOptions,
};
#[derive(Debug)]
pub struct Block {
pub info: String,
pub code: String,
}
// why yes, this is stolen from the examples:
// https://github.com/kivikakk/comrak/blob/56581d7275d8180f1a55771a2f3d41b6ebef26a6/examples/traverse_demo.rs
fn extract_text_traverse<'a>(root: &'a AstNode<'a>) -> Vec<Block> {
let mut output_blocks = Vec::new();
// Use `traverse` to get an iterator of `NodeEdge` and process each.
for edge in root.traverse() {
if let NodeEdge::Start(node) = edge {
if let NodeValue::CodeBlock(ref block) = node.data.borrow().value {
let block = Block {
code: block.literal.clone(),
info: block.info.clone(),
};
output_blocks.push(block);
}
}
}
output_blocks
}
pub fn parse(md: &str) -> Vec<Block> {
// setup parser
let arena = Arena::new();
let options = ComrakOptions::default();
// parse document and return root.
let root = parse_document(&arena, md, &options);
// extract text and print
extract_text_traverse(root)
}
#[cfg(test)]
mod test {
use crate::markdown::parse;
// Test if we can parse some markdown containing a single code block
#[test]
fn test_parse_simple() {
let md = "
```bash
echo 'foo bar'
```
";
let blocks = parse(md);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].info, "bash");
assert_eq!(blocks[0].code, "echo 'foo bar'\n");
}
#[test]
fn test_parse_not_so_simple() {
let md = "
# Hello I am a doc
## Here is how you do some things
### This thing is really cool
```bash
echo 'foo bar'
```
### This thing is cooler and with a better shell
```zsh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install atuin
```
";
let blocks = parse(md);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].info, "bash");
assert_eq!(blocks[0].code, "echo 'foo bar'\n");
assert_eq!(blocks[1].info, "zsh");
assert_eq!(
blocks[1].code,
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\ncargo install atuin\n"
);
}
}

View File

@ -42,13 +42,14 @@ clipboard = ["cli-clipboard"]
check-update = ["atuin-client/check-update"]
[dependencies]
atuin-server-postgres = { path = "../atuin-server-postgres", version = "18.3.0", optional = true }
atuin-server = { path = "../atuin-server", version = "18.3.0", optional = true }
atuin-client = { path = "../atuin-client", version = "18.3.0", optional = true, default-features = false }
atuin-common = { path = "../atuin-common", version = "18.3.0" }
atuin-dotfiles = { path = "../atuin-dotfiles", version = "0.3.0" }
atuin-history = { path = "../atuin-history", version = "0.2.0" }
atuin-daemon = { path = "../atuin-daemon", version = "0.2.0" }
atuin-server-postgres = { path = "../atuin-server-postgres", version = "18.3.0-prerelease.1", optional = true }
atuin-server = { path = "../atuin-server", version = "18.3.0-prerelease.1", optional = true }
atuin-client = { path = "../atuin-client", version = "18.3.0-prerelease.1", optional = true, default-features = false }
atuin-common = { path = "../atuin-common", version = "18.3.0-prerelease.1" }
atuin-dotfiles = { path = "../atuin-dotfiles", version = "0.2.0" }
atuin-history = { path = "../atuin-history", version = "0.1.0" }
atuin-daemon = { path = "../atuin-daemon", version = "0.1.0" }
atuin-run = { path = "../atuin-run", version = "0.1.0" }
log = { workspace = true }
env_logger = "0.11.2"

View File

@ -23,6 +23,7 @@ mod import;
mod info;
mod init;
mod kv;
mod run;
mod search;
mod stats;
mod store;
@ -80,6 +81,10 @@ pub enum Cmd {
#[command()]
Daemon,
/// Execute a runbook or workflow
#[command()]
Run,
/// Print example configuration
#[command()]
DefaultConfig,
@ -150,6 +155,8 @@ impl Cmd {
Self::Doctor => doctor::run(&settings).await,
Self::Run => run::run(),
Self::DefaultConfig => {
default_config::run();
Ok(())

View File

@ -0,0 +1,22 @@
use eyre::Result;
use atuin_run::markdown::parse;
pub fn run() -> Result<()> {
let blocks = parse(
"
1. do a thing
```sh
echo 'foo'
```
2. do another thing
```sh
echo 'bar'
```
",
);
println!("{:?}", blocks);
Ok(())
}