Skip to content

Commit

Permalink
Add --platform to rig available
Browse files Browse the repository at this point in the history
And make `--list-distros` work on all platforms.
  • Loading branch information
gaborcsardi committed Aug 22, 2023
1 parent f3cc5dd commit ea5d4a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
32 changes: 23 additions & 9 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,43 @@ pub fn rig_app() -> Command {
.help("List all available versions.")
.long("all")
.required(false),
)
.arg(
Arg::new("platform")
.help("Use this platform, instead of auto-detecting it.")
.long("platform")
.required(false)
)
.arg(
Arg::new("list-distros")
.help("List supported Linux distributions instead of R versions.")
.long("list-distros")
.num_args(0)
.required(false)
);

#[cfg(target_os = "macos")]
#[cfg(any(target_os = "windows", target_os = "linux"))]
{
cmd_available = cmd_available.arg(
Arg::new("arch")
.help(HELP_ARCH)
.help("Use this architecture, instead of auto-detecting it.")
.short('a')
.long("arch")
.required(false)
.default_value(&_default_arch)
.value_parser(["arm64", "x86_64"]),
.value_parser(clap::value_parser!(String))
);
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
{
cmd_available = cmd_available.arg(
Arg::new("list-distros")
.help("List supported Linux distributions instead of R versions.")
.long("list-distros")
.num_args(0)
Arg::new("arch")
.help(HELP_ARCH)
.short('a')
.long("arch")
.required(false)
.default_value(&_default_arch)
.value_parser(["arm64", "aarch64", "x86_64"]),
);
}

Expand Down
59 changes: 41 additions & 18 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,25 +235,16 @@ pub fn sc_rstudio(args: &ArgMatches) -> Result<(), Box<dyn Error>> {

// -- rig avilable --------------------------------------------------------

pub fn sc_available(args: &ArgMatches, mainargs: &ArgMatches)
-> Result<(), Box<dyn Error>> {
#[allow(unused_mut)]
let mut os = env::consts::OS.to_string();
fn get_platform(args: &ArgMatches)
-> Result<String, Box<dyn Error>> {
let platform: Option<String> = args.get_one::<String>("platform").cloned();

#[cfg(target_os = "linux")]
{
if args.get_flag("list-distros") {
return sc_available_distros(args, mainargs);
}
if let Some(x) = platform {
return Ok(x);
}

let mut arch = "".to_string();
if os == "macos" {
arch = args
.get_one::<String>("arch").unwrap().to_string();
} else if os == "linux" {
arch = env::consts::ARCH.to_string();
}
#[allow(unused_mut)]
let mut os = env::consts::OS.to_string();

#[cfg(target_os = "linux")]
{
Expand All @@ -264,9 +255,41 @@ pub fn sc_available(args: &ArgMatches, mainargs: &ArgMatches)
}

debug!("Auto-detected platform: {}.", os);
debug!("Auto-detected arch: {}.", arch);

Ok(os)
}

fn get_arch(platform: &str, args: &ArgMatches) -> String {
#[allow(unused_mut)]

let arch: String = match args.get_one::<String>("arch").cloned() {
Some(x) => x,
None => env::consts::ARCH.to_string()
};

// Prefer 'arm64' on macos, but 'aarch64' on linux
if platform == "macos" && arch == "aarch64" {
"arm64".to_string()
} else if platform == "linux" && arch == "arm64" {
"aarch64".to_string()
} else {
arch
}
}

pub fn sc_available(args: &ArgMatches, mainargs: &ArgMatches)
-> Result<(), Box<dyn Error>> {
#[allow(unused_mut)]

if args.get_flag("list-distros") {
return sc_available_distros(args, mainargs);
}

let platform = get_platform(args)?;
let arch = get_arch(&platform, args);

let url = "https://api.r-hub.io/rversions/available/".to_string() +
&os + "/" + &arch;
&platform + "/" + &arch;
let resp = download_json_sync(vec![url])?;
let resp = resp[0].as_array().unwrap();

Expand Down

0 comments on commit ea5d4a1

Please sign in to comment.