Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/env headers #50

Merged
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
42 changes: 41 additions & 1 deletion src/action/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use core::panic;
use std::process::ExitCode;

use crate::{cli::EnvCmd as Cmd, Ctx, Env, QuartzResult, StateField};
use crate::{
cli::{EnvCmd as Cmd, HeaderCmd},
Ctx, Env, PairMap, QuartzResult, StateField,
};
use colored::Colorize;

#[derive(clap::Args, Debug)]
Expand Down Expand Up @@ -31,6 +35,12 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult {
Cmd::Use(args) => switch(ctx, args)?,
Cmd::Ls => ls(ctx),
Cmd::Rm(args) => rm(ctx, args),
Cmd::Header { command } => match command {
HeaderCmd::Set { header } => header_set(ctx, header)?,
HeaderCmd::Ls => header_ls(ctx)?,
HeaderCmd::Rm { key } => header_rm(ctx, key)?,
HeaderCmd::Get { key } => header_get(ctx, key)?,
},
};

Ok(())
Expand Down Expand Up @@ -136,3 +146,33 @@ pub fn print(ctx: &Ctx) {
.unwrap_or("default".into())
);
}
pub fn header_set(ctx: &Ctx, args: Vec<String>) -> QuartzResult {
let mut env = ctx.require_env();
for header in args {
env.headers.set(&header);
}
env.update(ctx)?;
Ok(())
}
pub fn header_ls(ctx: &Ctx) -> QuartzResult {
let env = ctx.require_env();
print!("{}", env.headers);
Ok(())
}
pub fn header_rm(ctx: &Ctx, keys: Vec<String>) -> QuartzResult {
for key in keys {
let mut env = ctx.require_env();
env.headers.remove(&key);
env.update(ctx)?;
}
Ok(())
}
pub fn header_get(ctx: &Ctx, key: String) -> QuartzResult {
let env = ctx.require_env();
let value = env
.headers
.get(&key)
.unwrap_or_else(|| panic!("no header named {key} found"));
println!("{value}");
Ok(())
}
9 changes: 8 additions & 1 deletion src/action/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use chrono::Utc;
use hyper::{
body::{Bytes, HttpBody},
header::{HeaderName, HeaderValue},
Body, Client, Uri,
};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -93,11 +94,17 @@ pub async fn cmd(ctx: &Ctx, mut args: Args) -> QuartzResult {
let mut res: hyper::Response<Body>;

loop {
let req = endpoint
let mut req = endpoint
// TODO: Find a way around this clone
.clone()
.into_request()
.unwrap_or_else(|_| panic!("malformed request"));
for (key, val) in env.headers.iter() {
if !endpoint.headers.contains_key(key) {
req.headers_mut()
.insert(HeaderName::from_str(key)?, HeaderValue::from_str(val)?);
}
}

entry.message(&req);
if let Some(ref body) = body {
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ pub enum EnvCmd {
/// Delete a environment
#[command(name = "rm", alias = "remove")]
Rm(action::env::RmArgs),
Header {
#[command(subcommand)]
command: HeaderCmd,
},
}

#[derive(Debug, Subcommand)]
Expand Down
10 changes: 10 additions & 0 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ impl PairMap<'_> for Query {
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct Headers(pub HashMap<String, String>);

impl Headers {
pub fn parse(file_content: &str) -> Self {
let mut headers = Headers::default();
for header in file_content.lines().filter(|line| !line.is_empty()) {
headers.set(header);
}
headers
}
}

impl Deref for Headers {
type Target = HashMap<String, String>;

Expand Down
15 changes: 14 additions & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use serde::{Deserialize, Serialize};

use crate::{cookie::CookieJar, Ctx, PairMap};
use crate::{cookie::CookieJar, endpoint::Headers, Ctx, PairMap};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Variables(pub HashMap<String, String>);
Expand Down Expand Up @@ -61,13 +61,15 @@ impl Variables {
pub struct Env {
pub name: String,
pub variables: Variables,
pub headers: Headers,
}

impl Default for Env {
fn default() -> Self {
Self {
name: String::from("default"),
variables: Variables::default(),
headers: Headers::default(),
}
}
}
Expand Down Expand Up @@ -100,10 +102,18 @@ impl Env {
.write(true)
.truncate(true)
.open(self.dir(ctx).join("variables"))?;
let mut headers_file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(self.dir(ctx).join("headers"))?;

if !self.variables.is_empty() {
var_file.write_all(format!("{}", self.variables).as_bytes())?;
}
if !self.headers.0.is_empty() {
headers_file.write_all(format!("{}", self.headers).as_bytes())?;
}

Ok(())
}
Expand All @@ -119,6 +129,9 @@ impl Env {
if let Ok(var_contents) = std::fs::read_to_string(env.dir(ctx).join("variables")) {
env.variables = Variables::parse(&var_contents);
}
if let Ok(header_contents) = std::fs::read_to_string(env.dir(ctx).join("headers")) {
env.headers = Headers::parse(&header_contents);
}

Ok(env)
}
Expand Down
28 changes: 28 additions & 0 deletions tests/it/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,31 @@ fn it_cannot_create_duplicate() -> TestResult {

Ok(())
}

#[test]
fn it_create_headers() -> TestResult {
let quartz = Quartz::preset_empty_project()?;
quartz.cmd(&["env", "header", "set", "Header1: Value1"])?;
quartz.cmd(&["env", "header", "set", "Header2: Value2"])?;

let output = quartz.cmd(&["env", "header", "get", "Header1"])?;
assert_eq!(output.stdout.trim(), "Value1");

let output = quartz.cmd(&["env", "header", "get", "Header2"])?;
assert_eq!(output.stdout.trim(), "Value2");
Ok(())
}
#[test]
fn it_delete_header() -> TestResult {
let quartz = Quartz::preset_empty_project()?;
quartz.cmd(&["env", "header", "set", "Header1: Value1"])?;

let output = quartz.cmd(&["env", "header", "get", "Header1"])?;
assert_eq!(output.stdout.trim(), "Value1");

quartz.cmd(&["env", "header", "rm", "Header1"])?;
let output = quartz.cmd(&["env", "header", "get", "Header1"])?;
assert!(!output.status.success());

Ok(())
}
Loading