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(core): add search command #99

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions coffee_cmd/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub enum CoffeeCommand {
/// show the README file of the plugin
#[clap(arg_required_else_help = true)]
Show { plugin: String },
/// search the remote repositories for a plugin
#[clap(arg_required_else_help = true)]
Search { plugin: String },
/// clean up remote repositories storage information
#[clap(arg_required_else_help = false)]
Nurse {},
Expand All @@ -76,6 +79,7 @@ impl From<&CoffeeCommand> for coffee_core::CoffeeOperation {
CoffeeCommand::Remote { action } => Self::Remote(action.into()),
CoffeeCommand::Remove { plugin } => Self::Remove(plugin.to_owned()),
CoffeeCommand::Show { plugin } => Self::Show(plugin.to_owned()),
CoffeeCommand::Search { plugin } => Self::Search(plugin.to_owned()),
CoffeeCommand::Nurse {} => Self::Nurse,
}
}
Expand Down
8 changes: 8 additions & 0 deletions coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ async fn main() -> Result<(), CoffeeError> {
}
Err(err) => Err(err),
},
CoffeeCommand::Search { plugin } => match coffee.search(&plugin).await {
Ok(val) => {
let repository_url = val.repository_url.as_str();
term::success!("found plugin {plugin} in remote repository {repository_url}");
Ok(())
}
Err(err) => Err(err),
},
CoffeeCommand::Nurse {} => {
term::info!("Nurse command is not implemented");
Ok(())
Expand Down
13 changes: 13 additions & 0 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ impl PluginManager for CoffeeManager {
Err(err)
}

async fn search(&mut self, plugin: &str) -> Result<CoffeeSearch, CoffeeError> {
for repo in self.repos.values() {
if let Some(plugin) = repo.get_plugin_by_name(plugin) {
return Ok(CoffeeSearch {
repository_url: repo.url().url_string,
plugin,
});
}
}
let err = CoffeeError::new(404, &format!("unable to locate plugin `{plugin}`"));
Err(err)
}

async fn nurse(&mut self) -> Result<(), CoffeeError> {
unimplemented!("nurse command is not implemented")
}
Expand Down
2 changes: 2 additions & 0 deletions coffee_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum CoffeeOperation {
/// Setup(core lightning root path)
Setup(String),
Show(String),
/// Search(plugin name)
Search(String),
Nurse,
}

Expand Down
15 changes: 15 additions & 0 deletions coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub async fn run_httpd<T: ToSocketAddrs>(
.service(coffee_remote_rm)
.service(coffee_remote_list)
.service(coffee_show)
.service(coffee_search)
.with_json_spec_at("/api/v1")
.build()
})
Expand Down Expand Up @@ -168,6 +169,20 @@ async fn coffee_show(data: web::Data<AppState>, body: Json<Show>) -> Result<Json
handle_httpd_response!(result)
}

#[api_v2_operation]
#[get("/search")]
async fn coffee_search(
data: web::Data<AppState>,
body: Json<Search>,
) -> Result<Json<Value>, Error> {
let plugin = &body.plugin;

let mut coffee = data.coffee.lock().await;
let result = coffee.search(plugin).await;

handle_httpd_response!(result)
}

// this is just a hack to support swagger UI with https://paperclip-rs.github.io/paperclip/
// and the raw html is taken from https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/installation.md#unpkg
#[get("/")]
Expand Down
3 changes: 3 additions & 0 deletions coffee_lib/src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub trait PluginManager {
/// show the README file of the plugin
async fn show(&mut self, plugin: &str) -> Result<CoffeeShow, CoffeeError>;

/// search remote repositories for a plugin by name
async fn search(&mut self, plugin: &str) -> Result<CoffeeSearch, CoffeeError>;

/// clean up storage information about the remote repositories of the plugin manager.
async fn nurse(&mut self) -> Result<(), CoffeeError>;
}
11 changes: 11 additions & 0 deletions coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ pub mod request {
pub struct Show {
pub plugin: String,
}

#[derive(Debug, Deserialize, Apiv2Schema, Serialize)]
pub struct Search {
pub plugin: String,
}
}

// Definition of the response types.
Expand Down Expand Up @@ -118,4 +123,10 @@ pub mod response {
pub struct CoffeeShow {
pub readme: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CoffeeSearch {
pub repository_url: String,
pub plugin: Plugin,
}
}
8 changes: 8 additions & 0 deletions docs/docs-book/src/using-coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ coffee list
```bash
coffee show <plugin_name>
```

## Searching for a plugin in remote repositories

> ✅ Implemented

```bash
coffee search <plugin_name>
```
_________
## Running coffee as a server

Expand Down
35 changes: 35 additions & 0 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,41 @@ pub async fn httpd_add_remove_plugins() {
// Assert that the `readme` starts with the expected content
assert!(readme.starts_with("# Helpme plugin"), "{:?}", readme);

// Define the request body to be sent to the /search endpoint
let search_request = Search {
plugin: "summary".to_string(),
};

let response = client
.get(format!("{}/search", url))
.json(&search_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/search response: {}", body);

// Parse the response body
let response_json = serde_json::from_str(&body);
assert!(response_json.is_ok(), "{:?}", response_json);
let response_json: serde_json::Value = response_json.unwrap();

// Extract the `repository_url` field from the response JSON
let repository_url = response_json["repository_url"].as_str();
assert!(repository_url.is_some(), "{:?}", repository_url);
let repository_url = repository_url.unwrap();

// Assert that repository_url is the expected value
assert_eq!(
repository_url, "https://github.com/lightningd/plugins",
"{:?}",
repository_url
);

// Define the request body to be sent to the /install endpoint
let install_request = Install {
plugin: "summary".to_string(),
Expand Down
13 changes: 12 additions & 1 deletion tests/src/coffee_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn init_coffee_test_with_cln() -> anyhow::Result<()> {
}

#[tokio::test]
//#[ntest::timeout(120000)]
#[ntest::timeout(560000)]
pub async fn init_coffee_test_add_remote() {
init();
let mut cln = Node::tmp("regtest").await.unwrap();
Expand Down Expand Up @@ -240,6 +240,17 @@ pub async fn test_errors_and_show() {
.await
.unwrap();

// Search for summary plugin
let result = manager.coffee().search("summary").await;
assert!(result.is_ok(), "{:?}", result);
let result = result.unwrap();
let repo_url = result.repository_url.as_str();
assert_eq!(
repo_url, "https://github.com/lightningd/plugins",
"{:?}",
repo_url
);

// Install summary plugin
let result = manager.coffee().install("summary", true, false).await;
assert!(result.is_ok(), "{:?}", result);
Expand Down
Loading