Skip to content

Commit

Permalink
Merge pull request #70 from YOU54F/feat/pact_plugin_cli_skip_load
Browse files Browse the repository at this point in the history
feat(cli): support PACT_PLUGIN_CLI_SKIP_LOAD
  • Loading branch information
rholshausen authored Aug 21, 2024
2 parents 454ded1 + 9dc6d01 commit 8ae3c6f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude = [

[dependencies]
pact-plugin-driver = "0.6.1"
clap = { version = "4.4.11", features = [ "derive", "cargo" ] }
clap = { version = "4.4.11", features = [ "derive", "cargo", "env" ] }
comfy-table = "7.1.1"
home = "0.5.9"
anyhow = "1.0.86"
Expand Down
5 changes: 5 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ Options:
-v, --version <VERSION>
The version to install. This is only used for known plugins

--skip-load
Skip auto-loading of plugin
[env: PACT_PLUGIN_CLI_SKIP_LOAD=]

-h, --help
Print help (see a summary with '-h')

Expand Down
29 changes: 18 additions & 11 deletions cli/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub fn install_plugin(
_source_type: &Option<InstallationSource>,
override_prompt: bool,
skip_if_installed: bool,
version: &Option<String>
version: &Option<String>,
skip_load: bool,
) -> anyhow::Result<()> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand All @@ -37,9 +38,9 @@ pub fn install_plugin(

let install_url = Url::parse(source.as_str());
if let Ok(install_url) = install_url {
install_plugin_from_url(&http_client, install_url.as_str(), override_prompt, skip_if_installed).await
install_plugin_from_url(&http_client, install_url.as_str(), override_prompt, skip_if_installed,skip_load).await
} else {
install_known_plugin(&http_client, source.as_str(), override_prompt, skip_if_installed, version).await
install_known_plugin(&http_client, source.as_str(), override_prompt, skip_if_installed, version, skip_load).await
}
});

Expand All @@ -53,7 +54,8 @@ async fn install_known_plugin(
name: &str,
override_prompt: bool,
skip_if_installed: bool,
version: &Option<String>
version: &Option<String>,
skip_load: bool,
) -> anyhow::Result<()> {
let index = fetch_repository_index(&http_client, Some(DEFAULT_INDEX)).await?;
if let Some(entry) = index.entries.get(name) {
Expand All @@ -65,7 +67,7 @@ async fn install_known_plugin(
entry.latest_version.as_str()
};
if let Some(version_entry) = entry.versions.iter().find(|v| v.version == version) {
install_plugin_from_url(&http_client, version_entry.source.value().as_str(), override_prompt, skip_if_installed).await
install_plugin_from_url(&http_client, version_entry.source.value().as_str(), override_prompt, skip_if_installed,skip_load).await
} else {
Err(anyhow!("'{}' is not a valid version for plugin '{}'", version, name))
}
Expand All @@ -78,7 +80,8 @@ async fn install_plugin_from_url(
http_client: &Client,
source_url: &str,
override_prompt: bool,
skip_if_installed: bool
skip_if_installed: bool,
skip_load: bool
) -> anyhow::Result<()> {
let response = fetch_json_from_url(source_url, &http_client).await?;
if let Some(map) = response.as_object() {
Expand All @@ -104,13 +107,17 @@ async fn install_plugin_from_url(
download_plugin_executable(&manifest, &plugin_dir, &http_client, url, &tag, true).await?;

env::set_var("pact_do_not_track", "true");
load_plugin(&manifest.as_dependency())
if !skip_load {
load_plugin(&manifest.as_dependency())
.await
.and_then(|plugin| {
println!("Installed plugin {} version {} OK", manifest.name, manifest.version);
plugin.kill();
Ok(())
})
println!("Installed plugin {} version {} OK", manifest.name, manifest.version);
plugin.kill();
Ok(())
}) }
else {
return Ok(())
}
} else {
println!("Skipping installing plugin {} version {} as it is already installed", manifest.name, manifest.version);
Ok(())
Expand Down
10 changes: 7 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ enum Commands {

#[clap(short, long)]
/// The version to install. This is only used for known plugins.
version: Option<String>
version: Option<String>,

#[clap(long,env="PACT_PLUGIN_CLI_SKIP_LOAD")]
/// Skip auto-loading of plugin
skip_load: bool
},

/// Remove a plugin
Expand Down Expand Up @@ -232,8 +236,8 @@ fn main() -> Result<(), ExitCode> {
let result = match &cli.command {
Commands::List(command) => list_plugins(command),
Commands::Env => print_env(),
Commands::Install { yes, skip_if_installed, source, source_type, version } => {
install::install_plugin(source, source_type, *yes || cli.yes, *skip_if_installed, version)
Commands::Install { yes, skip_if_installed, source, source_type, version, skip_load } => {
install::install_plugin(source, source_type, *yes || cli.yes, *skip_if_installed, version, *skip_load)
},
Commands::Remove { yes, name, version } => remove_plugin(name, version, *yes || cli.yes),
Commands::Enable { name, version } => enable_plugin(name, version),
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/cmd/install.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ Options:
-v, --version <VERSION>
The version to install. This is only used for known plugins

--skip-load
Skip auto-loading of plugin

[env: PACT_PLUGIN_CLI_SKIP_LOAD=]

-h, --help
Print help (see a summary with '-h')

0 comments on commit 8ae3c6f

Please sign in to comment.