Skip to content

Commit

Permalink
Make artifact source accessible on Home
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 28, 2024
1 parent 88970e3 commit 86c6367
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
18 changes: 18 additions & 0 deletions lib/storage/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::env::var;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::manifests::AuthManifest;
use crate::result::{RokitError, RokitResult};
use crate::sources::ArtifactSource;

use super::{ToolCache, ToolStorage};

Expand Down Expand Up @@ -86,6 +88,22 @@ impl Home {
&self.tool_cache
}

/**
Creates a new `ArtifactSource` for this `Home`.
This will load any stored authentication from disk and use
it to authenticate with the artifact source and various providers.
# Errors
- If the auth manifest could not be loaded or created.
- If the artifact source could not be created.
*/
pub async fn artifact_source(&self) -> RokitResult<ArtifactSource> {
let auth = AuthManifest::load_or_create(&self.path).await?;
ArtifactSource::new_authenticated(&auth.get_all_tokens())
}

/**
Saves the contents of this `Home` to disk.
Expand Down
11 changes: 5 additions & 6 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use console::style;

use rokit::{
discovery::discover_all_manifests,
manifests::{AuthManifest, RokitManifest},
sources::{Artifact, ArtifactProvider, ArtifactSource},
manifests::RokitManifest,
sources::{Artifact, ArtifactProvider},
storage::Home,
tool::{ToolAlias, ToolId},
};
Expand Down Expand Up @@ -40,6 +40,7 @@ impl AddSubcommand {

let tool_cache = home.tool_cache();
let tool_storage = home.tool_storage();
let source = home.artifact_source().await?;

// 1. Check for trust, or prompt the user to trust the tool
if !tool_cache.is_trusted(&id) {
Expand All @@ -49,10 +50,8 @@ impl AddSubcommand {
let _ = tool_cache.add_trust(id.clone());
}

// 2. Load tool source, manifest, and do a preflight check
// to make sure we don't overwrite any existing tool(s)
let auth = AuthManifest::load(home.path()).await?;
let source = ArtifactSource::new_authenticated(&auth.get_all_tokens())?;
// 2. Load manifest and do a preflight check to
// ensure we don't overwrite any existing tool(s)
let manifest_path = if self.global {
home.path().to_path_buf()
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use console::style;
use futures::{stream::FuturesUnordered, TryStreamExt};
use rokit::{
discovery::discover_all_manifests,
manifests::AuthManifest,
sources::{Artifact, ArtifactProvider, ArtifactSource},
sources::{Artifact, ArtifactProvider},
storage::Home,
};

Expand All @@ -30,8 +29,7 @@ impl InstallSubcommand {
pub async fn run(self, home: &Home) -> Result<()> {
let force = self.force;

let auth = AuthManifest::load(home.path()).await?;
let source = ArtifactSource::new_authenticated(&auth.get_all_tokens())?;
let source = home.artifact_source().await?;
let manifests = discover_all_manifests(false, false).await;

let tool_cache = home.tool_cache();
Expand Down
9 changes: 2 additions & 7 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use clap::Parser;

use console::style;
use rokit::{
manifests::AuthManifest,
sources::{Artifact, ArtifactProvider, ArtifactSource},
sources::{Artifact, ArtifactProvider},
storage::Home,
tool::ToolId,
};
Expand Down Expand Up @@ -34,11 +33,7 @@ impl SelfUpdateSubcommand {
};

let pb = new_progress_bar("Loading", 4, 1);

// NOTE: Auth is not really necessary here since we know Rokit is not
// a private repository, but it may still help against rate limiting.
let auth = AuthManifest::load(home.path()).await?;
let source = ArtifactSource::new_authenticated(&auth.get_all_tokens())?;
let source = home.artifact_source().await?;

pb.inc(1);
pb.set_message("Fetching");
Expand Down
7 changes: 3 additions & 4 deletions src/cli/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use console::style;
use futures::{stream::FuturesUnordered, TryStreamExt};
use rokit::{
discovery::discover_all_manifests,
manifests::{AuthManifest, RokitManifest},
sources::{Artifact, ArtifactProvider, ArtifactSource},
manifests::RokitManifest,
sources::{Artifact, ArtifactProvider},
storage::Home,
};

Expand All @@ -26,8 +26,7 @@ pub struct UpdateSubcommand {
impl UpdateSubcommand {
pub async fn run(self, home: &Home) -> Result<()> {
// 1. Load tool source and the desired manifest
let auth = AuthManifest::load(home.path()).await?;
let source = ArtifactSource::new_authenticated(&auth.get_all_tokens())?;
let source = home.artifact_source().await?;
let manifest_path = if self.global {
home.path().to_path_buf()
} else {
Expand Down

0 comments on commit 86c6367

Please sign in to comment.