Skip to content

Commit

Permalink
feat(snot-cli): start and stop test cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Mar 23, 2024
1 parent 0f894af commit 944b3f5
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tower-http = { version = "0.5.2", features = ["fs", "trace"] }
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
url = "2.5"
nix = { version = "0.28", features = ["process"] }

snarkos-account = { path = "../snarkos/account" }
Expand Down
5 changes: 5 additions & 0 deletions crates/snot-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

anyhow.workspace = true
clap.workspace = true
reqwest = { workspace = true, features = ["blocking"] }
url.workspace = true
17 changes: 17 additions & 0 deletions crates/snot-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use anyhow::Result;
use clap::Parser;

#[derive(Debug, Parser)]
#[clap(name = "snot-cli", author = "MONADIC.US")]
pub struct Cli {
// /// The subcommand to run.
#[clap(subcommand)]
pub subcommand: crate::commands::Commands,
}

impl Cli {
/// Runs the subcommand.
pub fn run(self) -> Result<()> {
self.subcommand.run()
}
}
17 changes: 17 additions & 0 deletions crates/snot-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use anyhow::Result;
use clap::Parser;

mod test;

#[derive(Debug, Parser)]
pub enum Commands {
Test(test::Test),
}

impl Commands {
pub fn run(self) -> Result<()> {
match self {
Commands::Test(test) => test.run(),
}
}
}
50 changes: 50 additions & 0 deletions crates/snot-cli/src/commands/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::path::PathBuf;

use anyhow::Result;
use clap::Parser;

/// For interacting with snot tests.
#[derive(Debug, Parser)]
pub struct Test {
/// The url the agent is on.
#[clap(short, long, default_value = "http://localhost:1234")]
url: url::Url,
#[clap(subcommand)]
command: Commands,
}

/// Test commands
#[derive(Debug, Parser)]
enum Commands {
Start {
/// The test spec file.
spec: PathBuf,
},
Stop,
}

impl Test {
const START_ENDPOINT: &'static str = "api/v1/test/prepare";
const STOP_ENDPOINT: &'static str = "api/v1/test";

pub fn run(self) -> Result<()> {
let client = reqwest::blocking::Client::new();
use Commands::*;
match self.command {
Start { spec } => {
let file: String = std::fs::read_to_string(spec)?;
client
.post(&format!("{}{}", self.url, Self::START_ENDPOINT))
.body(file)
.send()?;
Ok(())
}
Stop => {
client
.delete(&format!("{}{}", self.url, Self::STOP_ENDPOINT))
.send()?;
Ok(())
}
}
}
}
19 changes: 17 additions & 2 deletions crates/snot-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
fn main() {
println!("Hello, world!");
use std::process::exit;

use anyhow::Result;
use clap::Parser;

mod cli;
mod commands;

fn main() -> Result<()> {
let cli = cli::Cli::parse();

if let Err(err) = cli.run() {
eprintln!("⚠️ {err}");
exit(1);
}

Ok(())
}
2 changes: 1 addition & 1 deletion scripts/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ if [ ! -f "$1" ]; then
exit 1
fi

curl -H "Content-Type: application/json" http://localhost:1234/api/v1/test/prepare -d "$(cat $1)"
curl http://localhost:1234/api/v1/test/prepare -d "$(cat $1)"

0 comments on commit 944b3f5

Please sign in to comment.