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 2 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
22 changes: 22 additions & 0 deletions src/action/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@
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),
Cmd::Cp(args) => cp(ctx, args)?,
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(())
Expand Down Expand Up @@ -136,3 +149,12 @@
.unwrap_or("default".into())
);
}
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)?;
Ok(())
}
pub fn header_ls(ctx: &Ctx, args: HeaderLsArgs) -> QuartzResult {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Ok(())
}
1 change: 1 addition & 0 deletions src/action/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
println!("This is a child: {}", child.value.handle());

queue.push(&child);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
57 changes: 57 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,63 @@ impl Variables {
}
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Headers(pub HashMap<String, String>);
impl Deref for Headers {
type Target = HashMap<String, String>;

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<String, String> {
&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 {
fn default() -> Self {
Self {
name: String::from("default"),
variables: Variables::default(),
headers: Headers::default(),
}
}
}
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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)
}
Expand Down
Loading