Skip to content

Commit

Permalink
able to use version specified in .anchorversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dromaz committed Jul 5, 2023
1 parent 8bdc1b1 commit 9a3c04c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
42 changes: 39 additions & 3 deletions avm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ pub fn version_binary_path(version: &Version) -> PathBuf {
}

/// Update the current version to a new version
pub fn use_version(version: &Version) -> Result<()> {
pub fn use_version(opt_version: Option<Version>) -> Result<()> {
let version = match opt_version {
Some(version) => version,
None => read_anchorversion_file()?
};

let installed_versions = read_installed_versions();
// Make sure the requested version is installed
if !installed_versions.contains(version) {
if !installed_versions.contains(&version) {
if let Ok(current) = current_version() {
println!("Version {version} is not installed, staying on version {current}.");
} else {
Expand Down Expand Up @@ -118,7 +123,7 @@ pub fn install_version(version: &Version, force: bool) -> Result<()> {
current_version_file.write_all(version.to_string().as_bytes())?;
}

use_version(version)
use_version(Some(version.clone()))
}

/// Remove an installed version of anchor-cli
Expand All @@ -134,6 +139,14 @@ pub fn uninstall_version(version: &Version) -> Result<()> {
Ok(())
}

/// Read version from .anchorversion
pub fn read_anchorversion_file() -> Result<Version> {
fs::read_to_string(".anchorversion")
.map_err(|e| anyhow!(".anchorversion file not found: {e}"))
.map(|content| Version::parse(content.trim()))?
.map_err(|e| anyhow!("Unable to parse version: {e}"))
}

/// Ensure the users home directory is setup with the paths required by AVM.
pub fn ensure_paths() {
let home_dir = AVM_HOME.to_path_buf();
Expand Down Expand Up @@ -239,6 +252,29 @@ mod tests {
use semver::Version;
use std::fs;
use std::io::Write;
use std::env;

#[test]
fn test_read_anchorversion() {
ensure_paths();
let mut dir = env::current_dir().unwrap();
dir.push(".anchorversion");
let mut file_created = fs::File::create(&dir).unwrap();
let test_version = "0.26.0";
file_created.write(test_version.as_bytes()).unwrap();


let version = read_anchorversion_file();
match version {
Ok(v) => {
assert_eq!(v.to_string(), test_version);
},
Err(_e) => {
assert!(false);
}
}
fs::remove_file(&dir).unwrap();
}

#[test]
fn test_ensure_paths() {
Expand Down
7 changes: 4 additions & 3 deletions avm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{Error, Result};
use avm;
use clap::{Parser, Subcommand};
use semver::Version;

Expand All @@ -15,8 +16,8 @@ pub struct Cli {
pub enum Commands {
#[clap(about = "Use a specific version of Anchor")]
Use {
#[clap(value_parser = parse_version)]
version: Version,
#[clap(value_parser = parse_version, required = false)]
version: Option<Version>,
},
#[clap(about = "Install a version of Anchor")]
Install {
Expand Down Expand Up @@ -48,7 +49,7 @@ fn parse_version(version: &str) -> Result<Version, Error> {
}
pub fn entry(opts: Cli) -> Result<()> {
match opts.command {
Commands::Use { version } => avm::use_version(&version),
Commands::Use { version } => avm::use_version(version),
Commands::Install { version, force } => avm::install_version(&version, force),
Commands::Uninstall { version } => avm::uninstall_version(&version),
Commands::List {} => avm::list_versions(),
Expand Down

0 comments on commit 9a3c04c

Please sign in to comment.