main.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use anyhow::{Error, Result};
  2. use clap::{Parser, Subcommand};
  3. use semver::Version;
  4. pub const VERSION: &str = env!("CARGO_PKG_VERSION");
  5. #[derive(Parser)]
  6. #[clap(name = "avm", about = "Anchor version manager", version)]
  7. pub struct Cli {
  8. #[clap(subcommand)]
  9. command: Commands,
  10. }
  11. #[derive(Subcommand)]
  12. pub enum Commands {
  13. #[clap(about = "Use a specific version of Anchor")]
  14. Use {
  15. #[clap(value_parser = parse_version)]
  16. version: Version,
  17. },
  18. #[clap(about = "Install a version of Anchor")]
  19. Install {
  20. #[clap(value_parser = parse_version)]
  21. version: Version,
  22. #[clap(long)]
  23. /// Flag to force installation even if the version
  24. /// is already installed
  25. force: bool,
  26. },
  27. #[clap(about = "Uninstall a version of Anchor")]
  28. Uninstall {
  29. #[clap(value_parser = parse_version)]
  30. version: Version,
  31. },
  32. #[clap(about = "List available versions of Anchor")]
  33. List {},
  34. #[clap(about = "Update to the latest Anchor version")]
  35. Update {},
  36. }
  37. // If `latest` is passed use the latest available version.
  38. fn parse_version(version: &str) -> Result<Version, Error> {
  39. if version == "latest" {
  40. Ok(avm::get_latest_version())
  41. } else {
  42. Version::parse(version).map_err(|e| anyhow::anyhow!(e))
  43. }
  44. }
  45. pub fn entry(opts: Cli) -> Result<()> {
  46. match opts.command {
  47. Commands::Use { version } => avm::use_version(&version),
  48. Commands::Install { version, force } => avm::install_version(&version, force),
  49. Commands::Uninstall { version } => avm::uninstall_version(&version),
  50. Commands::List {} => avm::list_versions(),
  51. Commands::Update {} => avm::update(),
  52. }
  53. }
  54. fn main() -> Result<()> {
  55. // Make sure the user's home directory is setup with the paths required by AVM.
  56. avm::ensure_paths();
  57. let opt = Cli::parse();
  58. entry(opt)
  59. }