Skip to content

Commit

Permalink
Functional-ish installation!
Browse files Browse the repository at this point in the history
- Trust check implementation
- Extracting assets from releases
- `aftman self-update` command
  • Loading branch information
LPGhatguy committed Dec 20, 2021
1 parent 1801f97 commit 26ca90f
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 155 deletions.
387 changes: 275 additions & 112 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ repository = "https://github.com/LPGhatguy/aftman"

[dependencies]
anyhow = "1.0.43"
atty = "0.2.14"
dialoguer = "0.9.0"
dirs = "3.0.2"
env_logger = "0.9.0"
fs-err = "2.6.0"
Expand All @@ -21,6 +23,7 @@ serde = { version = "1.0.129", features = ["derive"] }
serde_json = "1.0.67"
structopt = "0.3.22"
toml = "0.5.8"
zip = "0.5.13"

[dev-dependencies]
serde_json = "1.0.66"
2 changes: 1 addition & 1 deletion aftman.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[tools]
rojo = "rojo-rbx/rojo@6.2.0"
rojo = "rojo-rbx/rojo@7.0.0"
14 changes: 13 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Args {
Subcommand::List(_) => todo!(),
Subcommand::Add(sub) => sub.run(tools),
Subcommand::Update(_) => todo!(),
Subcommand::SelfUpdate(sub) => sub.run(tools),
}
}
}
Expand All @@ -25,13 +26,14 @@ pub enum Subcommand {
List(ListSubcommand),
Add(AddSubcommand),
Update(UpdateSubcommand),
SelfUpdate(SelfUpdateSubcommand),
}

/// Lists all existing tools managed by Aftman.
#[derive(Debug, StructOpt)]
pub struct ListSubcommand {}

/// Adds a new tool to Aftman and install it.
/// Adds a new tool to Aftman and installs it.
#[derive(Debug, StructOpt)]
pub struct AddSubcommand {
/// A tool spec describing where to get the tool and what version to
Expand Down Expand Up @@ -62,3 +64,13 @@ pub struct UpdateSubcommand {
#[structopt(long)]
pub latest: bool,
}

/// Update all aliases to Aftman. Run this after Aftman has been upgraded.
#[derive(Debug, StructOpt)]
pub struct SelfUpdateSubcommand {}

impl SelfUpdateSubcommand {
pub fn run(self, tools: ToolStorage) -> anyhow::Result<()> {
tools.update_links()
}
}
3 changes: 2 additions & 1 deletion src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub fn check_ident(ident_type: &str, ident: &str) -> anyhow::Result<()> {
);
ensure!(
ident.chars().all(|c| c != '/'),
"{} must not contain a slash"
"{} must not contain a slash",
ident_type
);

Ok(())
Expand Down
51 changes: 38 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,47 @@ mod trust;

use std::env::{consts::EXE_SUFFIX, current_dir, current_exe};

use anyhow::{format_err, Context};
use anyhow::{bail, format_err, Context};
use structopt::StructOpt;

use crate::cli::Args;
use crate::manifest::Manifest;
use crate::tool_storage::ToolStorage;

fn run() -> anyhow::Result<()> {
let tool_storage = ToolStorage::init()?;
let exe_name = current_exe_name()?;
let start_dir = current_dir().context("Failed to find current working directory")?;
let manifests = Manifest::discover(&start_dir)?;

let tool_storage = ToolStorage::init()?;
if exe_name != "aftman" {
let start_dir = current_dir().context("Failed to find current working directory")?;
let manifests = Manifest::discover(&start_dir)?;

for manifest in &manifests {
if let Some(tool_id) = manifest.tools.get(exe_name.as_str()) {
let args = std::env::args().collect();
tool_storage.run(tool_id, args)?;
return Ok(());
for manifest in &manifests {
if let Some(tool_id) = manifest.tools.get(exe_name.as_str()) {
let args = std::env::args().skip(1).collect();
tool_storage.run(tool_id, args)?;
return Ok(());
}
}

// If we're in Aftman's bin dir, we know for sure that we were supposed
// to be an Aftman tool.
let exe_path = current_exe()?;
if exe_path.starts_with(&tool_storage.bin_dir) {
let manifest_list = manifests
.iter()
.filter_map(|manifest| {
manifest
.path
.as_ref()
.map(|path| format!("- {}", path.display()))
})
.collect::<Vec<_>>()
.join("\n");

bail!("Tried to run an Aftman-managed version of {}, but no aftman.toml files list this tool.\n\
To run {} from this directory, add it to one of these files:\n\
{}", exe_name, exe_name, manifest_list);
}
}

Expand All @@ -46,15 +68,18 @@ fn current_exe_name() -> anyhow::Result<String> {
.and_then(|name| name.to_str())
.ok_or_else(|| format_err!("OS gave a funny result when asking for executable name"))?;

if exe_name.ends_with(EXE_SUFFIX) {
exe_name = &exe_name[..exe_name.len() - EXE_SUFFIX.len()];
}
exe_name = exe_name.strip_suffix(EXE_SUFFIX).unwrap_or(exe_name);

Ok(exe_name.to_owned())
}

fn main() {
env_logger::init();
let log_env = env_logger::Env::default().default_filter_or("info");

env_logger::Builder::from_env(log_env)
.format_module_path(false)
.format_timestamp(None)
.init();

if let Err(err) = run() {
eprintln!("Aftman error: {:?}", err);
Expand Down
10 changes: 8 additions & 2 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
use std::io;
use std::path::Path;
use std::path::{Path, PathBuf};

use anyhow::{bail, format_err, Context};
use serde::{Deserialize, Serialize};
Expand All @@ -23,6 +23,10 @@ static DEFAULT_GLOBAL_MANIFEST: &str = r#"
#[derive(Debug, Serialize, Deserialize)]
pub struct Manifest {
pub tools: BTreeMap<ToolAlias, ToolId>,

/// The path that this manifest was loaded from if it was loaded from a file.
#[serde(skip)]
pub path: Option<PathBuf>,
}

impl Manifest {
Expand Down Expand Up @@ -81,9 +85,11 @@ impl Manifest {
}
};

let manifest: Manifest = toml::from_slice(&contents)
let mut manifest: Manifest = toml::from_slice(&contents)
.with_context(|| format_err!("Invalid manifest at {}", file_path.display()))?;

manifest.path = Some(file_path);

Ok(Some(manifest))
}
}
12 changes: 8 additions & 4 deletions src/tool_source/github.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::Read;
use std::io::{Cursor, Read, Seek};

use anyhow::Context;
use reqwest::{
Expand All @@ -8,8 +8,9 @@ use reqwest::{
use semver::Version;
use serde::{Deserialize, Serialize};

use crate::tool_id::ToolId;
use crate::tool_name::ToolName;
use crate::{tool_id::ToolId, tool_source::Asset};
use crate::tool_source::Asset;

use super::Release;

Expand Down Expand Up @@ -76,7 +77,7 @@ impl GitHubSource {
.with_context(|| format!("Could not find release {}", spec))
}

pub fn download_asset(&self, url: &str) -> anyhow::Result<impl Read> {
pub fn download_asset(&self, url: &str) -> anyhow::Result<impl Read + Seek> {
let builder = self
.client
.get(url)
Expand All @@ -85,7 +86,10 @@ impl GitHubSource {

// TODO: Authorization

Ok(builder.send()?)
let response = builder.send()?;
let body = response.bytes()?.to_vec();

Ok(Cursor::new(body))
}
}

Expand Down
Loading

0 comments on commit 26ca90f

Please sign in to comment.