Skip to content

Commit

Permalink
wip(auth): move to tool_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sasial-dev committed Aug 12, 2022
1 parent 94fb18a commit a415670
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/tool_source/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use reqwest::{
blocking::Client,
header::{ACCEPT, AUTHORIZATION, USER_AGENT},
};
use semver::Version;
use semver::{Version, Op};
use serde::{Deserialize, Serialize};

use crate::auth::AuthManifest;
use crate::home::Home;
use crate::tool_id::ToolId;
use crate::tool_name::ToolName;
use crate::tool_source::Asset;
Expand All @@ -24,11 +23,10 @@ pub struct GitHubSource {
}

impl GitHubSource {
pub fn new(home: &Home) -> Self {
let token = AuthManifest::load(home).ok();
pub fn new(auth: Option<AuthManifest>) -> Self {
Self {
client: Client::new(),
token: token.flatten().map(|t| t.github).flatten(),
token: auth.map(|t| t.github).flatten()
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/tool_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::{bail, Context};
use fs_err::File;
use once_cell::unsync::OnceCell;

use crate::auth::AuthManifest;
use crate::home::Home;
use crate::manifest::Manifest;
use crate::tool_alias::ToolAlias;
Expand All @@ -24,6 +25,7 @@ pub struct ToolStorage {
pub storage_dir: PathBuf,
pub bin_dir: PathBuf,
home: Home,
auth: Option<AuthManifest>,
github: OnceCell<GitHubSource>,
}

Expand All @@ -35,10 +37,13 @@ impl ToolStorage {
let bin_dir = home.path().join("bin");
fs_err::create_dir_all(&bin_dir)?;

let auth = AuthManifest::load(home)?;

Ok(Self {
storage_dir,
bin_dir,
home: home.clone(),
auth,
github: OnceCell::new(),
})
}
Expand Down Expand Up @@ -154,7 +159,7 @@ impl ToolStorage {
log::info!("Installing tool: {}", spec);

log::debug!("Fetching GitHub releases...");
let github = self.github.get_or_init(|| GitHubSource::new(&self.home));
let github = self.github.get_or_init(|| GitHubSource::new(self.auth));
let mut releases = github.get_all_releases(spec.name())?;
releases.sort_by(|a, b| a.version.cmp(&b.version).reverse());

Expand Down Expand Up @@ -232,7 +237,7 @@ impl ToolStorage {
log::info!("Installing tool: {id}");

log::debug!("Fetching GitHub release...");
let github = self.github.get_or_init(|| GitHubSource::new(&self.home));
let github = self.github.get_or_init(|| GitHubSource::new(self.auth));
let release = github.get_release(id)?;

let mut compatible_assets = self.get_compatible_assets(&release);
Expand Down

0 comments on commit a415670

Please sign in to comment.