Skip to content

Commit

Permalink
Merge pull request #13 from miller-time/write_expr
Browse files Browse the repository at this point in the history
combine `write` filter and value args
  • Loading branch information
miller-time authored Jul 12, 2024
2 parents 27034af + 89efabc commit a241c7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ $ cat example.hcl | hq '.some_block[label="another_block_label"].attr'
You can modify HCL (even HCL that is formatted and contains comments) like so:

```sh
$ cat example.hcl | hq write '.fmt_block.first_formatted_field' '"something_new"'
$ cat example.hcl | hq write '.fmt_block.first_formatted_field="something_new"'
```

```hcl
Expand Down
60 changes: 25 additions & 35 deletions src/bin/hq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use clap::{Parser, Subcommand};
struct Args {
// the `Read` options are duplicated here because when no command is given
// then the `read` command is the default and its options come from the root
#[arg(value_name = "filter", help = "HCL filter expression")]
#[arg(value_name = "FILTER", help = "HCL filter expression")]
filter: Option<String>,

#[clap(short, long, value_name = "FILE", help = "HCL file to read from")]
Expand All @@ -25,22 +25,19 @@ struct Args {
enum Command {
#[command(about = "Read value from HCL (default)")]
Read {
#[arg(value_name = "filter", help = "HCL filter expression")]
filter: Option<String>,

#[clap(short, long, value_name = "FILE", help = "HCL file to read from")]
file: Option<String>,

#[arg(value_name = "FILTER", help = "HCL filter expression")]
filter: Option<String>,
},
#[command(about = "Write value into HCL")]
Write {
#[arg(value_name = "filter", help = "HCL filter expression")]
filter: Option<String>,

#[clap(short, long, value_name = "FILE", help = "HCL file to read from")]
file: Option<String>,

#[arg(required = true, help = "Value to write into HCL")]
value: String,
#[arg(required = true, help = "HCL write expression (<FILTER>=<VALUE>)")]
expr: String,
},
}

Expand All @@ -51,15 +48,11 @@ fn main() -> Result<(), Box<dyn Error>> {
None => {
read(args.filter, args.file)?;
}
Some(Command::Read { filter, file }) => {
read(filter, file)?;
Some(Command::Read { file, filter }) => {
read(file, filter)?;
}
Some(Command::Write {
filter,
file,
value,
}) => {
write(filter, file, value)?;
Some(Command::Write { file, expr }) => {
write(file, expr)?;
}
}

Expand All @@ -73,7 +66,7 @@ fn read_stdin() -> Result<String, Box<dyn Error>> {
Ok(buf)
}

fn read(filter: Option<String>, file: Option<String>) -> Result<(), Box<dyn Error>> {
fn read(file: Option<String>, filter: Option<String>) -> Result<(), Box<dyn Error>> {
let contents = match file {
Some(file) => fs::read_to_string(file)?,
None => read_stdin()?,
Expand Down Expand Up @@ -101,28 +94,25 @@ fn read(filter: Option<String>, file: Option<String>) -> Result<(), Box<dyn Erro
Ok(())
}

fn write(
filter: Option<String>,
file: Option<String>,
value: String,
) -> Result<(), Box<dyn Error>> {
fn write(file: Option<String>, expr: String) -> Result<(), Box<dyn Error>> {
let contents = match file {
Some(file) => fs::read_to_string(file)?,
None => read_stdin()?,
};
let mut body: hcl_edit::structure::Body = contents.parse()?;
let expr: hcl_edit::expr::Expression = value.parse()?;
match filter {
Some(filter) => {
let fields = hq_rs::parse_filter(&filter)?;
hq_rs::write(fields, &mut body, &expr)?;
print!("{body}");
io::stdout().flush()?;
}
None => {
print!("{expr}");
io::stdout().flush()?;
}
if !expr.contains('=') {
return Err("write expression should be <FILTER>=<VALUE>".into());
}
let parts: Vec<_> = expr.split('=').collect();
if parts.len() != 2 {
return Err("write expression should be <FILTER>=<VALUE>".into());
}
let filter = parts[0];
let new_value = parts[1];
let expr: hcl_edit::expr::Expression = new_value.parse()?;
let fields = hq_rs::parse_filter(filter)?;
hq_rs::write(fields, &mut body, &expr)?;
print!("{body}");
io::stdout().flush()?;
Ok(())
}

0 comments on commit a241c7a

Please sign in to comment.