-
Notifications
You must be signed in to change notification settings - Fork 13
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(kv): support setting key/value pairs #171
Merged
kate-goldenring
merged 1 commit into
fermyon:main
from
kate-goldenring:kv-subcommand-for-kv-pairs
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/// Module for determining the linkable resource to target for a command | ||
use crate::commands::links_output::{ResourceLinks, ResourceType}; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum ResourceTarget { | ||
ByName(String), | ||
ByLabel { label: String, app: String }, | ||
} | ||
|
||
impl ResourceTarget { | ||
pub fn find_in( | ||
&self, | ||
resources: Vec<ResourceLinks>, | ||
resource_type: ResourceType, | ||
) -> anyhow::Result<ResourceLinks> { | ||
match self { | ||
Self::ByName(resource) => resources | ||
.into_iter() | ||
.find(|r| &r.name == resource) | ||
.ok_or_else(|| { | ||
anyhow::anyhow!("No {resource_type} found with name \"{resource}\"") | ||
}), | ||
Self::ByLabel { label, app } => resources | ||
.into_iter() | ||
.find(|r| r.has_link(label, Some(app.as_str()))) | ||
.ok_or_else(|| { | ||
anyhow::anyhow!( | ||
r#"No {resource_type} found with label "{label}" for app "{app}""# | ||
) | ||
}), | ||
} | ||
} | ||
|
||
pub fn from_inputs( | ||
resource: &Option<String>, | ||
label: &Option<String>, | ||
app: &Option<String>, | ||
) -> anyhow::Result<ResourceTarget> { | ||
match (resource, label, app) { | ||
(Some(r), None, None) => Ok(ResourceTarget::ByName(r.to_owned())), | ||
(None, Some(l), Some(a)) => Ok(ResourceTarget::ByLabel { | ||
label: l.to_owned(), | ||
app: a.to_owned(), | ||
}), | ||
_ => Err(anyhow::anyhow!("Invalid combination of arguments")), // Should be prevented by clap | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use cloud_openapi::models::ResourceLabel; | ||
use uuid::Uuid; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_execute_target_from_inputs() { | ||
assert_eq!( | ||
ResourceTarget::from_inputs(&Some("mykv".to_owned()), &None, &None).unwrap(), | ||
ResourceTarget::ByName("mykv".to_owned()) | ||
); | ||
assert_eq!( | ||
ResourceTarget::from_inputs(&None, &Some("label".to_owned()), &Some("app".to_owned())) | ||
.unwrap(), | ||
ResourceTarget::ByLabel { | ||
label: "label".to_owned(), | ||
app: "app".to_owned(), | ||
} | ||
); | ||
assert!(ResourceTarget::from_inputs(&None, &None, &None).is_err()); | ||
assert!(ResourceTarget::from_inputs( | ||
&Some("mykv".to_owned()), | ||
&Some("label".to_owned()), | ||
&Some("app".to_owned()) | ||
) | ||
.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_execute_target_find_in() { | ||
let links = vec![ResourceLabel { | ||
app_id: Uuid::new_v4(), | ||
app_name: Some("app".to_owned()), | ||
label: "label".to_owned(), | ||
}]; | ||
let rl1 = ResourceLinks::new("mykv".to_owned(), vec![]); | ||
let rl2 = ResourceLinks::new("mykv2".to_owned(), links); | ||
let resources = vec![rl1.clone(), rl2.clone()]; | ||
assert_eq!( | ||
ResourceTarget::ByName("mykv".to_owned()) | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.unwrap(), | ||
rl1 | ||
); | ||
assert_eq!( | ||
ResourceTarget::ByLabel { | ||
label: "label".to_owned(), | ||
app: "app".to_owned(), | ||
} | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.unwrap(), | ||
rl2 | ||
); | ||
assert!(ResourceTarget::ByName("foo".to_owned()) | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.is_err()); | ||
assert!(ResourceTarget::ByLabel { | ||
label: "foo".to_owned(), | ||
app: "app".to_owned(), | ||
} | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if Clap is smart enough to show the user that if they don't set this they have to set the app/label. If not, can we document that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made some small adjustments (some conflicts withs), and it detects and tells the user when they have miscombined the flags. For example:
And: