Skip to content

Commit

Permalink
feat: content-type hints body edit into correct extension (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoRodriguesF committed May 11, 2024
1 parent f772395 commit f851f9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Content-type hints `body edit` into correct extension. (#46)

### Fixed

- `env use` on an unexistent command would not switch after confirming to create it.
Expand Down
22 changes: 22 additions & 0 deletions src/action/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{cli::BodyCmd as Cmd, validator, Ctx, QuartzResult};
use std::io::Write;

const POSSIBLE_EXT: [&str; 3] = ["json", "html", "xml"];

#[derive(clap::Args, Debug)]
pub struct Args {
/// Which extension to read body as. E.g.: quartz body --format json edit
Expand Down Expand Up @@ -33,6 +35,26 @@ pub fn edit(ctx: &Ctx, format: Option<String>) -> QuartzResult {
let handle = ctx.require_handle();
let path = handle.dir(ctx).join("body");

let format = if format.is_some() {
format
} else {
let endpoint = ctx.require_endpoint_from_handle(&handle);

if let Some(content) = endpoint.headers.get("content-type") {
let ext = POSSIBLE_EXT.iter().find_map(|ext| {
if content.contains(*ext) {
Some(ext.to_string())
} else {
None
}
});

ext
} else {
None
}
};

if let Some(format) = format {
// We cannot validate json for now. If we do so, variable notation will fail because it can
// generate invalid JSON. For exemple:
Expand Down

0 comments on commit f851f9e

Please sign in to comment.