-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
786 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::version::RequestedVersion; | ||
use anyhow::Error; | ||
use clap::{Parser, Subcommand}; | ||
|
||
/// A Python interpreter management system for Huak. | ||
#[derive(Parser)] | ||
#[command(version, author, about, arg_required_else_help = true)] | ||
pub(crate) struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
#[arg(short, long, global = true)] | ||
quiet: bool, | ||
#[arg(long, global = true)] | ||
no_color: bool, | ||
} | ||
|
||
impl Cli { | ||
pub(crate) fn run(self) -> Result<(), Error> { | ||
match self.command { | ||
Commands::Install { version } => cmd::install(&version), | ||
} | ||
} | ||
} | ||
|
||
// List of commands. | ||
#[derive(Subcommand)] | ||
#[clap(rename_all = "kebab-case")] | ||
enum Commands { | ||
/// Install a Python interpreter. | ||
Install { | ||
#[arg(required = true)] | ||
version: RequestedVersion, | ||
}, | ||
} | ||
|
||
mod cmd { | ||
use super::Error; | ||
use super::RequestedVersion; | ||
use crate::install; | ||
use crate::resolve::Strategy; | ||
|
||
pub(crate) fn install(version: &RequestedVersion) -> Result<(), Error> { | ||
install::install_to_home(version, &Strategy::Auto) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::{ | ||
resolve::{get_release, Strategy}, | ||
version::RequestedVersion, | ||
}; | ||
use anyhow::{bail, Context, Error, Ok}; // TODO(cnpryer): Use thiserror in library code. | ||
use huak_home::huak_home_dir; | ||
use std::{fs::File, path::PathBuf}; | ||
use tar::Archive; | ||
use tempfile::TempDir; | ||
use zstd::decode_all; | ||
|
||
pub(crate) fn install_to_home( | ||
version: &RequestedVersion, | ||
strategy: &Strategy, | ||
) -> Result<(), Error> { | ||
let release = get_release(version, strategy).context("requested release data")?; | ||
let tmp_dir = TempDir::new()?; | ||
let tmp_name = "tmp.tar.zst"; | ||
let tmp_path = tmp_dir.path().join(tmp_name); | ||
let target_dir = huak_home_dir() | ||
.context("requested huak's home directory")? | ||
.join("bin"); | ||
|
||
download_file(release.url, &tmp_path)?; | ||
|
||
let mut archive = File::open(tmp_path)?; | ||
let decoded = decode_all(&mut archive)?; | ||
|
||
let mut archive = Archive::new(decoded.as_slice()); | ||
archive.unpack(target_dir)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn download_file(url: &str, to: &PathBuf) -> Result<(), Error> { | ||
let mut response = reqwest::blocking::get(url)?; | ||
|
||
if !response.status().is_success() { | ||
bail!("failed to download file from {url}"); | ||
} | ||
|
||
let mut dest_file = File::create(to)?; | ||
response.copy_to(&mut dest_file)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
use huak_home::huak_home_dir; | ||
use clap::Parser; | ||
use cli::Cli; | ||
use colored::Colorize; | ||
use human_panic::setup_panic; | ||
|
||
mod cli; | ||
mod install; | ||
mod releases; | ||
mod resolve; | ||
mod version; | ||
|
||
fn main() { | ||
println!("{:?}", huak_home_dir()); | ||
println!("{:?}", releases::RELEASES[0].url); | ||
setup_panic!(); | ||
|
||
if let Err(e) = Cli::parse().run() { | ||
eprintln!("{}{} {}", "error".red(), ":".bold(), e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use crate::{releases::Release, version::RequestedVersion}; | ||
|
||
pub(crate) fn get_release( | ||
_version: &RequestedVersion, | ||
_strategy: &Strategy, | ||
) -> Option<Release<'static>> { | ||
todo!() | ||
} | ||
|
||
pub(crate) enum Strategy { | ||
Auto, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use anyhow::Error; | ||
use std::str::FromStr; // TODO(cnpryer): Library code should use thiserror | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RequestedVersion(String); // TODO(cnpryer) | ||
|
||
impl FromStr for RequestedVersion { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(RequestedVersion(s.to_owned())) | ||
} | ||
} |