Skip to content

Commit

Permalink
Key profiles/chains/requests by ID in collection
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Dec 4, 2023
1 parent 8cb69c8 commit 60f3596
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 245 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Changed

- Hide sensitive chain values in preview
- [BREAKING] Key profiles/chains/requests by ID in collection file
- Add collection ID/path to help modal ([#59](https://github.com/LucasPickering/slumber/issues/59))
- Also add collection ID to terminal title

### Fixed

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ crossterm = "^0.27.0"
derive_more = {version = "1.0.0-beta.6", features = ["debug", "deref", "deref_mut", "display", "from"]}
dialoguer = {version = "^0.11.0", default-features = false, features = ["password"]}
dirs = "^5.0.1"
equivalent = "^1"
futures = "^0.3.28"
indexmap = {version = "^2.0.1", features = ["serde"]}
itertools = "^0.11.0"
Expand Down
26 changes: 13 additions & 13 deletions slumber.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
id: example

profiles:
- id: works
works:
name: Works
data:
host: https://httpbin.org
username: !template "xX{{chains.username}}Xx"
user_guid: abc123
- id: init-fails
init-fails:
name: Request Init Fails
data:
- id: request-fails
request-fails:
name: Request Fails
data:
host: http://localhost:5000
user_guid: abc123

chains:
- id: username
username:
source: !command ["sh", "-c", "whoami | tr -d '\n'"]
- id: password
password:
source: !prompt Password
sensitive: true
- id: auth_token
auth_token:
source: !request login
selector: $.headers["X-Amzn-Trace-Id"]

Expand All @@ -33,7 +33,7 @@ base: &base
Content-Type: application/json

requests:
- id: login
login:
method: POST
url: "{{host}}/anything/login"
query:
Expand All @@ -44,22 +44,22 @@ requests:
"password": "{{chains.password}}"
}
- <<: *base
id: get_users
get_users:
<<: *base
name: Get Users
method: GET
url: "{{host}}/get"
query:
foo: bar

- <<: *base
id: get_user
get_user:
<<: *base
name: Get User
method: GET
url: "{{host}}/anything/{{user_guid}}"

- <<: *base
id: modify_user
modify_user:
<<: *base
name: Modify User
method: PUT
url: "{{host}}/anything/{{user_guid}}"
Expand Down
37 changes: 16 additions & 21 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::{
collection::{ProfileId, RequestCollection, RequestRecipeId},
http::{HttpEngine, Repository, RequestBuilder},
template::{Prompt, Prompter, TemplateContext},
util::{data_directory, find_by, ResultExt},
util::{data_directory, ResultExt},
};
use anyhow::Context;
use anyhow::{anyhow, Context};
use dialoguer::{Input, Password};
use indexmap::IndexMap;
use std::{
Expand Down Expand Up @@ -70,38 +70,33 @@ impl Subcommand {
} => {
let collection_path =
RequestCollection::try_path(collection_override)?;
let collection =
let mut collection =
RequestCollection::load(collection_path).await?;

// Find profile and recipe by ID
let profile = profile
.map(|profile| {
Ok::<_, anyhow::Error>(
find_by(
collection.profiles,
|e| &e.id,
&profile,
"No profile with ID",
)?
.data,
)
// TODO include list of valid IDs in error msgs here
let profile_data = profile
.map::<anyhow::Result<_>, _>(|id| {
let profile =
collection.profiles.swap_remove(&id).ok_or_else(
|| anyhow!("No profile with ID `{id}`"),
)?;
Ok(profile.data)
})
.transpose()?
.unwrap_or_default();
let recipe = find_by(
collection.recipes,
|r| &r.id,
&request_id,
"No request recipe with ID",
)?;
let recipe =
collection.recipes.swap_remove(&request_id).ok_or_else(
|| anyhow!("No request with ID `{request_id}`"),
)?;

// Build the request
let repository = Repository::load(&collection.id)?;
let overrides: IndexMap<_, _> = overrides.into_iter().collect();
let request = RequestBuilder::new(
recipe,
TemplateContext {
profile,
profile: profile_data,
overrides,
chains: collection.chains,
repository: repository.clone(),
Expand Down
Loading

0 comments on commit 60f3596

Please sign in to comment.