Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions src/cmds/default.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/cmds/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod check;
pub mod default;
pub mod install;
pub mod show;
pub mod r#use;
29 changes: 29 additions & 0 deletions src/cmds/use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use anyhow::Result;
use clap::Parser;
use std::os::unix::fs::symlink;
use std::{fs, path::Path};

use crate::{Config, perm_path};

#[derive(Parser)]
pub struct Args {
#[arg(default_value = "stable")]
pub new_channel: String,
}

impl Default for Args {
fn default() -> Self {
Self {
new_channel: "stable".to_string(),
}
}
}

pub async fn run(args: &Args, config: &Config) -> anyhow::Result<()> {
config.set_fixed_channel(&args.new_channel)?;
println!("Set fixed channel to {}", args.new_channel);

println!("updating PATH variable");
perm_path::check_or_update(config)?;
Ok(())
}
81 changes: 72 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ r#"
<#FFFFFF> ██║ </#FFFFFF><#999999>██╔╝ ██╗</#999999><#FF007F>██████╔╝</#FF007F>
<#FFFFFF> ╚═╝ </#FFFFFF><#999999>╚═╝ ╚═╝</#999999><#FF007F>╚═════╝ </#FF007F>"#
};

#[derive(Parser)]
#[command(author, version, about, long_about = Some(BANNER))]
struct Cli {
#[arg(short, long, env = "TX3_ROOT")]
#[arg(global = true, short, long, env = "TX3_ROOT")]
root_dir: Option<PathBuf>,

#[arg(short, long, env = "TX3_CHANNEL")]
#[arg(global = true, short, long, env = "TX3_CHANNEL")]
channel: Option<String>,

#[command(subcommand)]
Expand All @@ -40,7 +41,8 @@ enum Commands {
/// Uninstall the tx3 toolchain
Uninstall,
/// Set the default channel
Default(cmds::default::Args),
#[command(alias("default"))]
Use(cmds::r#use::Args),
/// Show the version of the tx3 toolchain
Show(cmds::show::Args),
}
Expand Down Expand Up @@ -70,12 +72,69 @@ impl Config {
.unwrap_or_else(|| Self::default_root_dir().unwrap())
}

pub fn channel(&self) -> String {
self.channel.clone().unwrap_or_else(|| "stable".to_string())
pub fn fixed_channel_dir(&self) -> PathBuf {
self.root_dir().join("default")
}

pub fn fixed_channel(&self) -> anyhow::Result<Option<String>> {
let fixed_channel_dir = self.fixed_channel_dir();

if !fixed_channel_dir.exists() {
return Ok(None);
}

let target = std::fs::read_link(fixed_channel_dir).context("reading fixed channel dir")?;

let channel = target
.file_name()
.ok_or_else(|| anyhow::anyhow!("no fixed channel dir"))?;

Ok(Some(channel.to_str().unwrap().to_string()))
}

fn set_fixed_channel(&self, channel: &str) -> Result<()> {
let fixed_channel_dir = self.fixed_channel_dir();
let channel_dir = self.root_dir().join(channel);

// Remove existing symlink if it exists
if fixed_channel_dir.exists() {
std::fs::remove_file(&fixed_channel_dir)?;
}

// Create new symlink
std::os::unix::fs::symlink(&channel_dir, &fixed_channel_dir)?;

Ok(())
}

pub fn channel(&self) -> anyhow::Result<String> {
let explicit = self.channel.clone();

if let Some(explicit) = explicit {
return Ok(explicit);
}

if let Some(default) = self.fixed_channel()? {
return Ok(default);
}

Err(anyhow::anyhow!("no channel set"))
}

pub fn ensure_channel(&self) -> String {
match self.channel() {
Ok(channel) => channel,
Err(e) => {
eprintln!("Error getting channel: {}", e);
self.set_fixed_channel("stable").unwrap();
"stable".to_string()
}
}
}

pub fn channel_dir(&self) -> PathBuf {
self.root_dir().join(self.channel())
let channel = self.ensure_channel();
self.root_dir().join(channel)
}

pub fn bin_dir(&self) -> PathBuf {
Expand All @@ -102,17 +161,21 @@ async fn main() -> anyhow::Result<()> {

println!("\n{}\n", BANNER.trim_start());

println!("root dir: {}", config.root_dir().display());
println!("current channel: {}", config.ensure_channel());
println!();

if let Some(command) = cli.command {
match command {
Commands::Install(args) => cmds::install::run(&args, &config).await?,
Commands::Check(args) => cmds::check::run(&args, &config).await?,
Commands::Uninstall => todo!(),
Commands::Default(args) => cmds::default::run(&args, &config).await?,
Commands::Use(args) => cmds::r#use::run(&args, &config).await?,
Commands::Show(args) => cmds::show::run(&args, &config).await?,
Commands::Uninstall => todo!(),
}
} else {
cmds::install::run(&cmds::install::Args::default(), &config).await?;
cmds::default::run(&cmds::default::Args::default(), &config).await?;
cmds::r#use::run(&cmds::r#use::Args::default(), &config).await?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn download_remote_manifest(config: &Config) -> anyhow::Result<()> {
.await
.context("fetching latest release")?;

let manifest_name = format!("manifest-{}.json", config.channel());
let manifest_name = format!("manifest-{}.json", config.ensure_channel());

let manifest_asset = release
.assets
Expand Down