Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "hit-cli"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
authors = ["Mehmood S. Deshmukh <meshde.md@gmail.com>"]
homepage = "https://usehit.dev"
repository = "https://github.com/meshde/hit-cli"
license-file = "LICENSE"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -20,6 +24,7 @@ edit = "0.1.5"
flatten-json-object = "0.6.1"
getopts = "0.2.21"
handlebars = "5.1.2"
human-panic = "2.0.2"
hyper = "1.3.1"
inquire = "0.7.5"
openapiv3 = "1.0.1"
Expand Down
6 changes: 6 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod run;

use crate::core::command::Command as ConfigCommand;
use crate::core::config::{CommandType as ConfigCommandType, Config};
use crate::utils::error::CliError;
use clap::{command, Arg, ArgMatches, Command, FromArgMatches as _, Parser, Subcommand};
use clap_complete::CompleteEnv;
use convert_case::{Case, Casing};
Expand Down Expand Up @@ -129,6 +130,11 @@ pub async fn init() -> ExitCode {
};

if let Err(_e) = output {
if let Some(e) = _e.downcast_ref::<CliError>() {
eprintln!("{}", e);
} else {
panic!("{}", _e);
}
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
Expand Down
24 changes: 21 additions & 3 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::core::command::Command;
use crate::core::config::Config;
use crate::core::env::get_env;
use crate::core::ephenv::get_ephenvs;
use crate::utils::error::CliError;
use crate::utils::http::handle_request;
use colored_json;
use edit::edit;
Expand Down Expand Up @@ -31,8 +32,24 @@ pub async fn run(

let url = api_call.url.as_str();

let current_env = get_env().expect("env not set");
let env_data = config.envs.get(&current_env).expect("env not recognized");
let current_env = match get_env() {
Some(e) => e,
None => {
return Err(Box::new(CliError {
message: "env not set".to_string(),
help: None,
}))
}
};
let env_data = match config.envs.get(&current_env) {
Some(d) => d,
None => {
return Err(Box::new(CliError {
message: "env not recognized".to_string(),
help: None,
}))
}
};
let ephenv_data = get_ephenvs();
let merged_data = env_data
.clone()
Expand Down Expand Up @@ -76,7 +93,8 @@ pub async fn run(
.collect::<HashMap<String, String>>(),
input,
)
.await?;
.await
.unwrap();

get_app_config().set_prev_request(response.clone());

Expand Down
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ mod constants;
mod core;
mod utils;

use reqwest;
use human_panic;
use std::process;
use tokio;

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
cli::init().await;
Ok(())
async fn main() -> process::ExitCode {
human_panic::setup_panic!(human_panic::Metadata::new("hit", env!("CARGO_PKG_VERSION"))
.authors("Mehmood S. Deshmukh <meshde.md@gmail.com>")
.homepage("https://github.com/meshde/hit-cli/issues"));
cli::init().await
}
15 changes: 15 additions & 0 deletions src/utils/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub struct CliError {
pub message: String,
pub help: Option<String>,
}

impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error for CliError {}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod error;
pub mod http;
pub mod input;
7 changes: 6 additions & 1 deletion tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ impl SetupFixture {
"API_URL": "https://staging-api.example.com"
}
},
"commands": {}
"commands": {
"get-by-id": {
"method": "GET",
"url": "{{API_URL}}/items/:id",
}
}
});

fs::write(&config_path, test_config.to_string()).unwrap();
Expand Down
26 changes: 26 additions & 0 deletions tests/run_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
mod fixtures;
use assert_cmd::prelude::*;
use fixtures::{get_hit_command_for_setup, hit_setup, SetupFixture};
use rstest::*;

#[rstest]
fn test_failure_when_env_not_set(
hit_setup: SetupFixture,
) -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = get_hit_command_for_setup(&hit_setup);
cmd.args(["run", "get-by-id", "--id", "meshde"]);
cmd.assert().failure().stderr("env not set\n");

Ok(())
}

#[rstest]
fn test_failure_when_env_not_recognized(hit_setup: SetupFixture) -> () {
let mut use_cmd = get_hit_command_for_setup(&hit_setup);
use_cmd.args(["env", "use", "something"]);
use_cmd.assert().success();

let mut cmd = get_hit_command_for_setup(&hit_setup);
cmd.args(["run", "get-by-id", "--id", "meshde"]);
cmd.assert().failure().stderr("env not recognized\n");
}