Browse Source

avm: Ask whether to install if the version is not installed with the `use` command (#3230)

acheron 1 year ago
parent
commit
1c0b2132ec
2 changed files with 12 additions and 11 deletions
  1. 1 0
      CHANGELOG.md
  2. 11 11
      avm/src/lib.rs

+ 1 - 0
CHANGELOG.md

@@ -46,6 +46,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 - lang: Deprecate `#[interface]` attribute ([#3195](https://github.com/coral-xyz/anchor/pull/3195)).
 - ts: Include unresolved accounts in the resolution error message ([#3207](https://github.com/coral-xyz/anchor/pull/3207)).
 - lang: Add `LazyAccount` ([#3194](https://github.com/coral-xyz/anchor/pull/3194)).
+- avm: Ask whether to install if the version is not installed with the `use` command ([#3230](https://github.com/coral-xyz/anchor/pull/3230)).
 
 ### Fixes
 

+ 11 - 11
avm/src/lib.rs

@@ -7,7 +7,7 @@ use reqwest::StatusCode;
 use semver::{Prerelease, Version};
 use serde::{de, Deserialize};
 use std::fs;
-use std::io::Write;
+use std::io::{BufRead, Write};
 use std::path::PathBuf;
 use std::process::Stdio;
 
@@ -80,16 +80,16 @@ pub fn use_version(opt_version: Option<Version>) -> Result<()> {
     // Make sure the requested version is installed
     let installed_versions = read_installed_versions()?;
     if !installed_versions.contains(&version) {
-        if let Ok(current) = current_version() {
-            println!("Version {version} is not installed, staying on version {current}.");
-        } else {
-            println!("Version {version} is not installed, no current version.");
-        }
-
-        return Err(anyhow!(
-            "You need to run 'avm install {}' to install it before using it.",
-            version
-        ));
+        println!("Version {version} is not installed. Would you like to install? [y/n]");
+        let input = std::io::stdin()
+            .lock()
+            .lines()
+            .next()
+            .expect("Expected input")?;
+        match input.as_str() {
+            "y" | "yes" => return install_version(InstallTarget::Version(version), false),
+            _ => return Err(anyhow!("Installation rejected.")),
+        };
     }
 
     let mut current_version_file = fs::File::create(current_version_file_path())?;