From 4912f8b82d5359ddd30576b6d353354e172ea56e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 10:25:04 -0300 Subject: [PATCH 01/16] feat(env): add headers field and cli commands in env --- src/action/env.rs | 19 ++++++++++++++++ src/action/ls.rs | 1 + src/cli.rs | 10 +++++++++ src/env.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) diff --git a/src/action/env.rs b/src/action/env.rs index 6aaeb48..3b7c5fe 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -24,6 +24,15 @@ pub struct RmArgs { env: String, } +#[derive(clap::Args, Debug)] +pub struct HeaderSetArgs { + key: String, + value: String, +} + +#[derive(clap::Args, Debug)] +pub struct HeaderLsArgs {} + pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { match command { Cmd::Create(args) => create(ctx, args), @@ -31,6 +40,10 @@ 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 { + crate::cli::HeaderEnvCmd::Set(args) => header_set(ctx, args), + crate::cli::HeaderEnvCmd::Ls(args) => header_ls(ctx, args), + }, }; Ok(()) @@ -136,3 +149,9 @@ pub fn print(ctx: &Ctx) { .unwrap_or("default".into()) ); } +pub fn header_set(ctx: &Ctx, args: HeaderSetArgs) { + let mut env = ctx.require_env(); + env.headers.insert(args.key, args.value); + env.update(ctx); +} +pub fn header_ls(ctx: &Ctx, args: HeaderLsArgs) {} diff --git a/src/action/ls.rs b/src/action/ls.rs index 990f7d7..9b3536f 100644 --- a/src/action/ls.rs +++ b/src/action/ls.rs @@ -156,6 +156,7 @@ pub fn cmd(ctx: &Ctx, args: Args) { } for child in node.children.iter() { + println!("This is a child: {}", child.value.handle()); queue.push(&child); } } diff --git a/src/cli.rs b/src/cli.rs index 4e8f339..0bacbb4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -231,6 +231,16 @@ pub enum EnvCmd { /// Delete a environment #[command(name = "rm", alias = "remove")] Rm(action::env::RmArgs), + Header { + #[command(subcommand)] + command: HeaderEnvCmd, + }, +} + +#[derive(Debug, Subcommand)] +pub enum HeaderEnvCmd { + Set(action::env::HeaderSetArgs), + Ls(action::env::HeaderLsArgs), } #[derive(Debug, Subcommand)] diff --git a/src/env.rs b/src/env.rs index b4143b5..3995053 100644 --- a/src/env.rs +++ b/src/env.rs @@ -57,10 +57,55 @@ impl Variables { } } +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct Headers(pub HashMap); +impl Deref for Headers { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Headers { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Display for Headers { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for (key, value) in self.iter() { + writeln!(f, "{key}={value}")?; + } + + Ok(()) + } +} +impl PairMap<'_> for Headers { + const NAME: &'static str = "variable"; + + fn map(&mut self) -> &mut HashMap { + &mut self.0 + } +} +impl Headers { + pub fn parse(file_content: &str) -> Self { + let mut headers = Headers::default(); + + for var in file_content.split('\n').filter(|line| !line.is_empty()) { + headers.set(var); + } + + headers + } +} + #[derive(Clone, Serialize, Deserialize)] pub struct Env { pub name: String, pub variables: Variables, + pub headers: Headers, } impl Default for Env { @@ -68,6 +113,7 @@ impl Default for Env { Self { name: String::from("default"), variables: Variables::default(), + headers: Headers::default(), } } } @@ -100,10 +146,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(()) } @@ -119,6 +173,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) } From 777dbfc0cb3e0c1acec1d303bd27bc852d5306cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 10:31:16 -0300 Subject: [PATCH 02/16] fix(errors): error propagation from env header actions --- src/action/env.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/action/env.rs b/src/action/env.rs index 3b7c5fe..4955535 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -41,8 +41,8 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { Cmd::Ls => ls(ctx), Cmd::Rm(args) => rm(ctx, args), Cmd::Header { command } => match command { - crate::cli::HeaderEnvCmd::Set(args) => header_set(ctx, args), - crate::cli::HeaderEnvCmd::Ls(args) => header_ls(ctx, args), + crate::cli::HeaderEnvCmd::Set(args) => header_set(ctx, args)?, + crate::cli::HeaderEnvCmd::Ls(args) => header_ls(ctx, args)?, }, }; @@ -149,9 +149,12 @@ pub fn print(ctx: &Ctx) { .unwrap_or("default".into()) ); } -pub fn header_set(ctx: &Ctx, args: HeaderSetArgs) { +pub fn header_set(ctx: &Ctx, args: HeaderSetArgs) -> QuartzResult { let mut env = ctx.require_env(); env.headers.insert(args.key, args.value); - env.update(ctx); + env.update(ctx)?; + Ok(()) +} +pub fn header_ls(ctx: &Ctx, args: HeaderLsArgs) -> QuartzResult { + Ok(()) } -pub fn header_ls(ctx: &Ctx, args: HeaderLsArgs) {} From 33938002bf425696952ece697220fac7778b9f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 11:12:56 -0300 Subject: [PATCH 03/16] fix(env): headers formatting --- src/action/env.rs | 2 ++ src/env.rs | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/action/env.rs b/src/action/env.rs index 4955535..0bdbcd4 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -156,5 +156,7 @@ pub fn header_set(ctx: &Ctx, args: HeaderSetArgs) -> QuartzResult { Ok(()) } pub fn header_ls(ctx: &Ctx, args: HeaderLsArgs) -> QuartzResult { + let env = ctx.require_env(); + println!("{}", env.headers); Ok(()) } diff --git a/src/env.rs b/src/env.rs index 3995053..d348b5d 100644 --- a/src/env.rs +++ b/src/env.rs @@ -76,7 +76,7 @@ impl DerefMut for Headers { impl Display for Headers { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (key, value) in self.iter() { - writeln!(f, "{key}={value}")?; + writeln!(f, "{key}: {value}")?; } Ok(()) @@ -88,6 +88,10 @@ impl PairMap<'_> for Headers { fn map(&mut self) -> &mut HashMap { &mut self.0 } + fn pair(input: &'_ str) -> Option<(String, String)> { + let (key, val) = input.split_once(": ")?; + Some((key.to_owned(), val.to_owned())) + } } impl Headers { pub fn parse(file_content: &str) -> Self { From 5a81ff3e3a36872c61b64f43604d1ee3a87af7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 11:16:32 -0300 Subject: [PATCH 04/16] refactor(env): remove unnecessary list env arguments --- src/action/env.rs | 7 ++----- src/cli.rs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/action/env.rs b/src/action/env.rs index 0bdbcd4..0cab7aa 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -30,9 +30,6 @@ pub struct HeaderSetArgs { value: String, } -#[derive(clap::Args, Debug)] -pub struct HeaderLsArgs {} - pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { match command { Cmd::Create(args) => create(ctx, args), @@ -42,7 +39,7 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { Cmd::Rm(args) => rm(ctx, args), Cmd::Header { command } => match command { crate::cli::HeaderEnvCmd::Set(args) => header_set(ctx, args)?, - crate::cli::HeaderEnvCmd::Ls(args) => header_ls(ctx, args)?, + crate::cli::HeaderEnvCmd::Ls => header_ls(ctx)?, }, }; @@ -155,7 +152,7 @@ pub fn header_set(ctx: &Ctx, args: HeaderSetArgs) -> QuartzResult { env.update(ctx)?; Ok(()) } -pub fn header_ls(ctx: &Ctx, args: HeaderLsArgs) -> QuartzResult { +pub fn header_ls(ctx: &Ctx) -> QuartzResult { let env = ctx.require_env(); println!("{}", env.headers); Ok(()) diff --git a/src/cli.rs b/src/cli.rs index 0bacbb4..cd96c21 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -240,7 +240,7 @@ pub enum EnvCmd { #[derive(Debug, Subcommand)] pub enum HeaderEnvCmd { Set(action::env::HeaderSetArgs), - Ls(action::env::HeaderLsArgs), + Ls, } #[derive(Debug, Subcommand)] From 657d4fab1cac5a361d7b9330978e9d74e021a16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 11:48:58 -0300 Subject: [PATCH 05/16] feat(env): parse command-line set env header --- src/action/env.rs | 10 ++++++---- src/cli.rs | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/action/env.rs b/src/action/env.rs index 0cab7aa..6dce26f 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -1,6 +1,6 @@ use std::process::ExitCode; -use crate::{cli::EnvCmd as Cmd, Ctx, Env, QuartzResult, StateField}; +use crate::{cli::EnvCmd as Cmd, Ctx, Env, PairMap, QuartzResult, StateField}; use colored::Colorize; #[derive(clap::Args, Debug)] @@ -38,7 +38,7 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { Cmd::Ls => ls(ctx), Cmd::Rm(args) => rm(ctx, args), Cmd::Header { command } => match command { - crate::cli::HeaderEnvCmd::Set(args) => header_set(ctx, args)?, + crate::cli::HeaderEnvCmd::Set { headers } => header_set(ctx, headers)?, crate::cli::HeaderEnvCmd::Ls => header_ls(ctx)?, }, }; @@ -146,9 +146,11 @@ pub fn print(ctx: &Ctx) { .unwrap_or("default".into()) ); } -pub fn header_set(ctx: &Ctx, args: HeaderSetArgs) -> QuartzResult { +pub fn header_set(ctx: &Ctx, args: Vec) -> QuartzResult { let mut env = ctx.require_env(); - env.headers.insert(args.key, args.value); + for header in args { + env.headers.set(&header); + } env.update(ctx)?; Ok(()) } diff --git a/src/cli.rs b/src/cli.rs index cd96c21..c7835b3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -239,7 +239,7 @@ pub enum EnvCmd { #[derive(Debug, Subcommand)] pub enum HeaderEnvCmd { - Set(action::env::HeaderSetArgs), + Set { headers: Vec }, Ls, } From f81f875b480f5c26701b280fb7b0c53c4a50fdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 12:22:37 -0300 Subject: [PATCH 06/16] feat(env): append env headers to every request --- src/action/send.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/action/send.rs b/src/action/send.rs index ab0bb98..6d3a731 100644 --- a/src/action/send.rs +++ b/src/action/send.rs @@ -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}; @@ -93,11 +94,15 @@ pub async fn cmd(ctx: &Ctx, mut args: Args) -> QuartzResult { let mut res: hyper::Response; 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() { + req.headers_mut() + .insert(HeaderName::from_str(key)?, HeaderValue::from_str(val)?); + } entry.message(&req); if let Some(ref body) = body { From 6be6ba87341204ac50613ca268f5f9e313433935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 13:09:41 -0300 Subject: [PATCH 07/16] refactor(env): remove unnecessary struct --- src/action/env.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/action/env.rs b/src/action/env.rs index 6dce26f..a38bee7 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -24,12 +24,6 @@ pub struct RmArgs { env: String, } -#[derive(clap::Args, Debug)] -pub struct HeaderSetArgs { - key: String, - value: String, -} - pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { match command { Cmd::Create(args) => create(ctx, args), From 9c996844d69695d49ebaa4a911bca889c76928fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 13:14:36 -0300 Subject: [PATCH 08/16] feat(env): add remove header from env command --- src/action/env.rs | 21 ++++++++++++++++++--- src/cli.rs | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/action/env.rs b/src/action/env.rs index a38bee7..e76a7c2 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -1,6 +1,9 @@ use std::process::ExitCode; -use crate::{cli::EnvCmd as Cmd, Ctx, Env, PairMap, QuartzResult, StateField}; +use crate::{ + cli::{EnvCmd as Cmd, HeaderEnvCmd}, + Ctx, Env, PairMap, QuartzResult, StateField, +}; use colored::Colorize; #[derive(clap::Args, Debug)] @@ -24,6 +27,11 @@ pub struct RmArgs { env: String, } +#[derive(clap::Args, Debug)] +pub struct HeaderRmArgs { + key: String, +} + pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { match command { Cmd::Create(args) => create(ctx, args), @@ -32,8 +40,9 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { Cmd::Ls => ls(ctx), Cmd::Rm(args) => rm(ctx, args), Cmd::Header { command } => match command { - crate::cli::HeaderEnvCmd::Set { headers } => header_set(ctx, headers)?, - crate::cli::HeaderEnvCmd::Ls => header_ls(ctx)?, + HeaderEnvCmd::Set { headers } => header_set(ctx, headers)?, + HeaderEnvCmd::Ls => header_ls(ctx)?, + HeaderEnvCmd::Rm(args) => header_rm(ctx, args)?, }, }; @@ -153,3 +162,9 @@ pub fn header_ls(ctx: &Ctx) -> QuartzResult { println!("{}", env.headers); Ok(()) } +pub fn header_rm(ctx: &Ctx, args: HeaderRmArgs) -> QuartzResult { + let mut env = ctx.require_env(); + env.headers.remove(&args.key); + env.update(ctx)?; + Ok(()) +} diff --git a/src/cli.rs b/src/cli.rs index c7835b3..0372924 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -241,6 +241,7 @@ pub enum EnvCmd { pub enum HeaderEnvCmd { Set { headers: Vec }, Ls, + Rm(action::env::HeaderRmArgs), } #[derive(Debug, Subcommand)] From d27cd415133054e40e2bba0d2cc126adf181e1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 13:28:28 -0300 Subject: [PATCH 09/16] feat(env): get env header command --- src/action/env.rs | 17 +++++++++++++++++ src/cli.rs | 1 + 2 files changed, 18 insertions(+) diff --git a/src/action/env.rs b/src/action/env.rs index e76a7c2..b4f60af 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -1,3 +1,4 @@ +use core::panic; use std::process::ExitCode; use crate::{ @@ -32,6 +33,11 @@ pub struct HeaderRmArgs { key: String, } +#[derive(clap::Args, Debug)] +pub struct HeaderGetArgs { + key: String, +} + pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { match command { Cmd::Create(args) => create(ctx, args), @@ -43,6 +49,7 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { HeaderEnvCmd::Set { headers } => header_set(ctx, headers)?, HeaderEnvCmd::Ls => header_ls(ctx)?, HeaderEnvCmd::Rm(args) => header_rm(ctx, args)?, + HeaderEnvCmd::Get(args) => header_get(ctx, args)?, }, }; @@ -168,3 +175,13 @@ pub fn header_rm(ctx: &Ctx, args: HeaderRmArgs) -> QuartzResult { env.update(ctx)?; Ok(()) } +pub fn header_get(ctx: &Ctx, args: HeaderGetArgs) -> QuartzResult { + let env = ctx.require_env(); + let name = args.key; + let value = env + .headers + .get(&name) + .unwrap_or_else(|| panic!("no header named {name} found")); + println!("{value}"); + Ok(()) +} diff --git a/src/cli.rs b/src/cli.rs index 0372924..ea58572 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -242,6 +242,7 @@ pub enum HeaderEnvCmd { Set { headers: Vec }, Ls, Rm(action::env::HeaderRmArgs), + Get(action::env::HeaderGetArgs), } #[derive(Debug, Subcommand)] From 929060da66bc498674f4d2290bee09c7522c66ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 13:41:53 -0300 Subject: [PATCH 10/16] test(env): test if env headers are saving --- tests/it/env.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/it/env.rs b/tests/it/env.rs index da77d91..d645c6e 100644 --- a/tests/it/env.rs +++ b/tests/it/env.rs @@ -116,3 +116,17 @@ 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(()) +} From d5c65302285303c706a8f1cb97fb982051157eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 13:58:02 -0300 Subject: [PATCH 11/16] test(env): add test removing env header --- tests/it/env.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/it/env.rs b/tests/it/env.rs index d645c6e..772efa7 100644 --- a/tests/it/env.rs +++ b/tests/it/env.rs @@ -130,3 +130,17 @@ fn it_create_headers() -> TestResult { 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(()) +} From f3053ee49d17649e39261531cf1fb19f226b9c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 19 May 2024 14:09:48 -0300 Subject: [PATCH 12/16] fix(env): not override endpoint already existing headers --- src/action/send.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/action/send.rs b/src/action/send.rs index 6d3a731..59ee871 100644 --- a/src/action/send.rs +++ b/src/action/send.rs @@ -100,8 +100,10 @@ pub async fn cmd(ctx: &Ctx, mut args: Args) -> QuartzResult { .into_request() .unwrap_or_else(|_| panic!("malformed request")); for (key, val) in env.headers.iter() { - req.headers_mut() - .insert(HeaderName::from_str(key)?, HeaderValue::from_str(val)?); + if !endpoint.headers.contains_key(key) { + req.headers_mut() + .insert(HeaderName::from_str(key)?, HeaderValue::from_str(val)?); + } } entry.message(&req); From 50608435c454162eb4e761464b6b068d6ff287bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 26 May 2024 17:00:29 -0300 Subject: [PATCH 13/16] fix: remove useless print statement --- src/action/ls.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/action/ls.rs b/src/action/ls.rs index 9b3536f..990f7d7 100644 --- a/src/action/ls.rs +++ b/src/action/ls.rs @@ -156,7 +156,6 @@ pub fn cmd(ctx: &Ctx, args: Args) { } for child in node.children.iter() { - println!("This is a child: {}", child.value.handle()); queue.push(&child); } } From 6ee3fe881d3455506b18e784844ba2fd9af6f613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 26 May 2024 17:12:22 -0300 Subject: [PATCH 14/16] refactor(cli): remove redundant cli command definition --- src/action/env.rs | 37 ++++++++++++++----------------------- src/cli.rs | 10 +--------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/action/env.rs b/src/action/env.rs index b4f60af..0830170 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -2,7 +2,7 @@ use core::panic; use std::process::ExitCode; use crate::{ - cli::{EnvCmd as Cmd, HeaderEnvCmd}, + cli::{EnvCmd as Cmd, HeaderCmd}, Ctx, Env, PairMap, QuartzResult, StateField, }; use colored::Colorize; @@ -28,16 +28,6 @@ pub struct RmArgs { env: String, } -#[derive(clap::Args, Debug)] -pub struct HeaderRmArgs { - key: String, -} - -#[derive(clap::Args, Debug)] -pub struct HeaderGetArgs { - key: String, -} - pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { match command { Cmd::Create(args) => create(ctx, args), @@ -46,10 +36,10 @@ pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult { Cmd::Ls => ls(ctx), Cmd::Rm(args) => rm(ctx, args), Cmd::Header { command } => match command { - HeaderEnvCmd::Set { headers } => header_set(ctx, headers)?, - HeaderEnvCmd::Ls => header_ls(ctx)?, - HeaderEnvCmd::Rm(args) => header_rm(ctx, args)?, - HeaderEnvCmd::Get(args) => header_get(ctx, args)?, + 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)?, }, }; @@ -169,19 +159,20 @@ pub fn header_ls(ctx: &Ctx) -> QuartzResult { println!("{}", env.headers); Ok(()) } -pub fn header_rm(ctx: &Ctx, args: HeaderRmArgs) -> QuartzResult { - let mut env = ctx.require_env(); - env.headers.remove(&args.key); - env.update(ctx)?; +pub fn header_rm(ctx: &Ctx, keys: Vec) -> 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, args: HeaderGetArgs) -> QuartzResult { +pub fn header_get(ctx: &Ctx, key: String) -> QuartzResult { let env = ctx.require_env(); - let name = args.key; let value = env .headers - .get(&name) - .unwrap_or_else(|| panic!("no header named {name} found")); + .get(&key) + .unwrap_or_else(|| panic!("no header named {key} found")); println!("{value}"); Ok(()) } diff --git a/src/cli.rs b/src/cli.rs index ea58572..ce73015 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -233,18 +233,10 @@ pub enum EnvCmd { Rm(action::env::RmArgs), Header { #[command(subcommand)] - command: HeaderEnvCmd, + command: HeaderCmd, }, } -#[derive(Debug, Subcommand)] -pub enum HeaderEnvCmd { - Set { headers: Vec }, - Ls, - Rm(action::env::HeaderRmArgs), - Get(action::env::HeaderGetArgs), -} - #[derive(Debug, Subcommand)] pub enum VarCmd { /// Open an editor to modify variables From 9aba30bdb8863483976f121ac15883bc14419448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Sun, 26 May 2024 17:36:45 -0300 Subject: [PATCH 15/16] refactor(env): remove redundant `Headers` declaration --- src/endpoint.rs | 10 ++++++++++ src/env.rs | 50 +------------------------------------------------ 2 files changed, 11 insertions(+), 49 deletions(-) diff --git a/src/endpoint.rs b/src/endpoint.rs index d991fe7..e4d2e38 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -51,6 +51,16 @@ impl PairMap<'_> for Query { #[derive(Default, Debug, Serialize, Deserialize, Clone)] pub struct Headers(pub HashMap); +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; diff --git a/src/env.rs b/src/env.rs index d348b5d..5479bcc 100644 --- a/src/env.rs +++ b/src/env.rs @@ -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); @@ -57,54 +57,6 @@ impl Variables { } } -#[derive(Default, Debug, Clone, Serialize, Deserialize)] -pub struct Headers(pub HashMap); -impl Deref for Headers { - type Target = HashMap; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for Headers { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl Display for Headers { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - for (key, value) in self.iter() { - writeln!(f, "{key}: {value}")?; - } - - Ok(()) - } -} -impl PairMap<'_> for Headers { - const NAME: &'static str = "variable"; - - fn map(&mut self) -> &mut HashMap { - &mut self.0 - } - fn pair(input: &'_ str) -> Option<(String, String)> { - let (key, val) = input.split_once(": ")?; - Some((key.to_owned(), val.to_owned())) - } -} -impl Headers { - pub fn parse(file_content: &str) -> Self { - let mut headers = Headers::default(); - - for var in file_content.split('\n').filter(|line| !line.is_empty()) { - headers.set(var); - } - - headers - } -} - #[derive(Clone, Serialize, Deserialize)] pub struct Env { pub name: String, From ff84570b4ab87d91494e1c22df15362867c76f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20C=2E=20Brand=C3=A3o?= Date: Tue, 28 May 2024 08:36:14 -0300 Subject: [PATCH 16/16] fix(env): list headers without additional line break --- src/action/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/action/env.rs b/src/action/env.rs index 0830170..3154faa 100644 --- a/src/action/env.rs +++ b/src/action/env.rs @@ -156,7 +156,7 @@ pub fn header_set(ctx: &Ctx, args: Vec) -> QuartzResult { } pub fn header_ls(ctx: &Ctx) -> QuartzResult { let env = ctx.require_env(); - println!("{}", env.headers); + print!("{}", env.headers); Ok(()) } pub fn header_rm(ctx: &Ctx, keys: Vec) -> QuartzResult {