Skip to content

Commit 9a3c04c

Browse files
author
dromaz
committed
able to use version specified in .anchorversion
1 parent 8bdc1b1 commit 9a3c04c

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

avm/src/lib.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ pub fn version_binary_path(version: &Version) -> PathBuf {
4545
}
4646

4747
/// Update the current version to a new version
48-
pub fn use_version(version: &Version) -> Result<()> {
48+
pub fn use_version(opt_version: Option<Version>) -> Result<()> {
49+
let version = match opt_version {
50+
Some(version) => version,
51+
None => read_anchorversion_file()?
52+
};
53+
4954
let installed_versions = read_installed_versions();
5055
// Make sure the requested version is installed
51-
if !installed_versions.contains(version) {
56+
if !installed_versions.contains(&version) {
5257
if let Ok(current) = current_version() {
5358
println!("Version {version} is not installed, staying on version {current}.");
5459
} else {
@@ -118,7 +123,7 @@ pub fn install_version(version: &Version, force: bool) -> Result<()> {
118123
current_version_file.write_all(version.to_string().as_bytes())?;
119124
}
120125

121-
use_version(version)
126+
use_version(Some(version.clone()))
122127
}
123128

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

142+
/// Read version from .anchorversion
143+
pub fn read_anchorversion_file() -> Result<Version> {
144+
fs::read_to_string(".anchorversion")
145+
.map_err(|e| anyhow!(".anchorversion file not found: {e}"))
146+
.map(|content| Version::parse(content.trim()))?
147+
.map_err(|e| anyhow!("Unable to parse version: {e}"))
148+
}
149+
137150
/// Ensure the users home directory is setup with the paths required by AVM.
138151
pub fn ensure_paths() {
139152
let home_dir = AVM_HOME.to_path_buf();
@@ -239,6 +252,29 @@ mod tests {
239252
use semver::Version;
240253
use std::fs;
241254
use std::io::Write;
255+
use std::env;
256+
257+
#[test]
258+
fn test_read_anchorversion() {
259+
ensure_paths();
260+
let mut dir = env::current_dir().unwrap();
261+
dir.push(".anchorversion");
262+
let mut file_created = fs::File::create(&dir).unwrap();
263+
let test_version = "0.26.0";
264+
file_created.write(test_version.as_bytes()).unwrap();
265+
266+
267+
let version = read_anchorversion_file();
268+
match version {
269+
Ok(v) => {
270+
assert_eq!(v.to_string(), test_version);
271+
},
272+
Err(_e) => {
273+
assert!(false);
274+
}
275+
}
276+
fs::remove_file(&dir).unwrap();
277+
}
242278

243279
#[test]
244280
fn test_ensure_paths() {

avm/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::{Error, Result};
2+
use avm;
23
use clap::{Parser, Subcommand};
34
use semver::Version;
45

@@ -15,8 +16,8 @@ pub struct Cli {
1516
pub enum Commands {
1617
#[clap(about = "Use a specific version of Anchor")]
1718
Use {
18-
#[clap(value_parser = parse_version)]
19-
version: Version,
19+
#[clap(value_parser = parse_version, required = false)]
20+
version: Option<Version>,
2021
},
2122
#[clap(about = "Install a version of Anchor")]
2223
Install {
@@ -48,7 +49,7 @@ fn parse_version(version: &str) -> Result<Version, Error> {
4849
}
4950
pub fn entry(opts: Cli) -> Result<()> {
5051
match opts.command {
51-
Commands::Use { version } => avm::use_version(&version),
52+
Commands::Use { version } => avm::use_version(version),
5253
Commands::Install { version, force } => avm::install_version(&version, force),
5354
Commands::Uninstall { version } => avm::uninstall_version(&version),
5455
Commands::List {} => avm::list_versions(),

0 commit comments

Comments
 (0)