Skip to content

Commit

Permalink
feat: content addressable storage
Browse files Browse the repository at this point in the history
  • Loading branch information
daimond113 committed Jul 28, 2024
1 parent d8cd78e commit 37cc86f
Show file tree
Hide file tree
Showing 36 changed files with 575 additions and 312 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ url = { version = "2.5.2", features = ["serde"] }
# TODO: reevaluate whether to use this
# secrecy = "0.8.0"
chrono = { version = "0.4.38", features = ["serde"] }
sha2 = "0.10.8"

# TODO: remove this when gitoxide adds support for: committing, pushing, adding
git2 = { version = "0.19.0", optional = true }
Expand Down
11 changes: 5 additions & 6 deletions src/cli/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use crate::cli::config::{read_config, write_config};
use anyhow::Context;
use keyring::Entry;
use serde::Deserialize;
use std::path::Path;

pub fn get_token<P: AsRef<Path>>(data_dir: P) -> anyhow::Result<Option<String>> {
pub fn get_token() -> anyhow::Result<Option<String>> {
match std::env::var("PESDE_TOKEN") {
Ok(token) => return Ok(Some(token)),
Err(std::env::VarError::NotPresent) => {}
Err(e) => return Err(e.into()),
}

let config = read_config(data_dir)?;
let config = read_config()?;
if let Some(token) = config.token {
return Ok(Some(token));
}
Expand All @@ -29,7 +28,7 @@ pub fn get_token<P: AsRef<Path>>(data_dir: P) -> anyhow::Result<Option<String>>
Ok(None)
}

pub fn set_token<P: AsRef<Path>>(data_dir: P, token: Option<&str>) -> anyhow::Result<()> {
pub fn set_token(token: Option<&str>) -> anyhow::Result<()> {
let entry = match Entry::new("token", env!("CARGO_PKG_NAME")) {
Ok(entry) => entry,
Err(e) => return Err(e.into()),
Expand All @@ -47,9 +46,9 @@ pub fn set_token<P: AsRef<Path>>(data_dir: P, token: Option<&str>) -> anyhow::Re
Err(e) => return Err(e.into()),
}

let mut config = read_config(&data_dir)?;
let mut config = read_config()?;
config.token = token.map(|s| s.to_string());
write_config(data_dir, &config)?;
write_config(&config)?;

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clap::Args;
use colored::Colorize;
use pesde::{
errors::ManifestReadError,
source::{pesde::PesdePackageSource, PackageSource},
source::{pesde::PesdePackageSource, traits::PackageSource},
Project,
};
use serde::Deserialize;
Expand Down Expand Up @@ -71,7 +71,7 @@ impl LoginCommand {
},
None => match manifest {
Some(_) => None,
None => Some(read_config(project.data_dir())?.default_index),
None => Some(read_config()?.default_index),
},
};

Expand Down Expand Up @@ -182,7 +182,7 @@ impl LoginCommand {

println!("logged in as {}", get_token_login(&reqwest, &token)?.bold());

set_token(project.data_dir(), Some(&token))?;
set_token(Some(&token))?;

Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions src/cli/commands/auth/logout.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::cli::auth::set_token;
use clap::Args;
use pesde::Project;

#[derive(Debug, Args)]
pub struct LogoutCommand {}

impl LogoutCommand {
pub fn run(self, project: Project) -> anyhow::Result<()> {
set_token(project.data_dir(), None)?;
pub fn run(self) -> anyhow::Result<()> {
set_token(None)?;

println!("logged out");

Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl AuthCommands {
pub fn run(self, project: Project, reqwest: reqwest::blocking::Client) -> anyhow::Result<()> {
match self {
AuthCommands::Login(login) => login.run(project, reqwest),
AuthCommands::Logout(logout) => logout.run(project),
AuthCommands::WhoAmI(whoami) => whoami.run(project, reqwest),
AuthCommands::Logout(logout) => logout.run(),
AuthCommands::WhoAmI(whoami) => whoami.run(reqwest),
}
}
}
5 changes: 2 additions & 3 deletions src/cli/commands/auth/whoami.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::cli::{auth::get_token_login, get_token};
use clap::Args;
use colored::Colorize;
use pesde::Project;

#[derive(Debug, Args)]
pub struct WhoAmICommand {}

impl WhoAmICommand {
pub fn run(self, project: Project, reqwest: reqwest::blocking::Client) -> anyhow::Result<()> {
let token = match get_token(project.data_dir())? {
pub fn run(self, reqwest: reqwest::blocking::Client) -> anyhow::Result<()> {
let token = match get_token()? {
Some(token) => token,
None => {
println!("not logged in");
Expand Down
7 changes: 3 additions & 4 deletions src/cli/commands/config/default_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cli::config::{read_config, write_config, CliConfig};
use clap::Args;
use pesde::Project;

#[derive(Debug, Args)]
pub struct DefaultIndexCommand {
Expand All @@ -14,8 +13,8 @@ pub struct DefaultIndexCommand {
}

impl DefaultIndexCommand {
pub fn run(self, project: Project) -> anyhow::Result<()> {
let mut config = read_config(project.data_dir())?;
pub fn run(self) -> anyhow::Result<()> {
let mut config = read_config()?;

let index = if self.reset {
Some(CliConfig::default().default_index)
Expand All @@ -26,7 +25,7 @@ impl DefaultIndexCommand {
match index {
Some(index) => {
config.default_index = index.clone();
write_config(project.data_dir(), &config)?;
write_config(&config)?;
println!("default index set to: {index}");
}
None => {
Expand Down
7 changes: 3 additions & 4 deletions src/cli/commands/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::Subcommand;
use pesde::Project;

mod default_index;
mod scripts_repo;
Expand All @@ -14,10 +13,10 @@ pub enum ConfigCommands {
}

impl ConfigCommands {
pub fn run(self, project: Project) -> anyhow::Result<()> {
pub fn run(self) -> anyhow::Result<()> {
match self {
ConfigCommands::DefaultIndex(default_index) => default_index.run(project),
ConfigCommands::ScriptsRepo(scripts_repo) => scripts_repo.run(project),
ConfigCommands::DefaultIndex(default_index) => default_index.run(),
ConfigCommands::ScriptsRepo(scripts_repo) => scripts_repo.run(),
}
}
}
7 changes: 3 additions & 4 deletions src/cli/commands/config/scripts_repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cli::config::{read_config, write_config, CliConfig};
use clap::Args;
use pesde::Project;

#[derive(Debug, Args)]
pub struct ScriptsRepoCommand {
Expand All @@ -14,8 +13,8 @@ pub struct ScriptsRepoCommand {
}

impl ScriptsRepoCommand {
pub fn run(self, project: Project) -> anyhow::Result<()> {
let mut config = read_config(project.data_dir())?;
pub fn run(self) -> anyhow::Result<()> {
let mut config = read_config()?;

let repo = if self.reset {
Some(CliConfig::default().scripts_repo)
Expand All @@ -26,7 +25,7 @@ impl ScriptsRepoCommand {
match repo {
Some(repo) => {
config.scripts_repo = repo.clone();
write_config(project.data_dir(), &config)?;
write_config(&config)?;
println!("scripts repo set to: {repo}");
}
None => {
Expand Down
8 changes: 2 additions & 6 deletions src/cli/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,8 @@ impl InitCommand {
));
}

manifest["indices"][DEFAULT_INDEX_NAME] = toml_edit::value(
read_config(project.data_dir())?
.default_index
.to_bstring()
.to_string(),
);
manifest["indices"][DEFAULT_INDEX_NAME] =
toml_edit::value(read_config()?.default_index.to_bstring().to_string());

project.write_manifest(manifest.to_string())?;

Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fn bin_link_file(alias: &str) -> String {
.collect::<Vec<_>>()
.join(", ");

#[cfg(windows)]
#[cfg(not(unix))]
let prefix = String::new();
#[cfg(not(windows))]
#[cfg(unix)]
let prefix = "#!/usr/bin/env -S lune run\n";

format!(
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Subcommand {
) -> anyhow::Result<()> {
match self {
Subcommand::Auth(auth) => auth.run(project, reqwest),
Subcommand::Config(config) => config.run(project),
Subcommand::Config(config) => config.run(),
Subcommand::Init(init) => init.run(project),
Subcommand::Run(run) => run.run(project),
Subcommand::Install(install) => install.run(project, multi, reqwest),
Expand All @@ -70,7 +70,7 @@ impl Subcommand {
Subcommand::Patch(patch) => patch.run(project, reqwest),
#[cfg(feature = "patches")]
Subcommand::PatchCommit(patch_commit) => patch_commit.run(project),
Subcommand::SelfUpgrade(self_upgrade) => self_upgrade.run(project, reqwest),
Subcommand::SelfUpgrade(self_upgrade) => self_upgrade.run(reqwest),
}
}
}
8 changes: 6 additions & 2 deletions src/cli/commands/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::Args;
use colored::Colorize;
use pesde::{
patches::setup_patches_repo,
source::{PackageRef, PackageSource},
source::traits::{PackageRef, PackageSource},
Project, MANIFEST_FILE_NAME,
};

Expand Down Expand Up @@ -39,7 +39,11 @@ impl PatchCommand {
.join(chrono::Utc::now().timestamp().to_string());
std::fs::create_dir_all(&directory)?;

source.download(&node.node.pkg_ref, &directory, &project, &reqwest)?;
source
.download(&node.node.pkg_ref, &project, &reqwest)?
.0
.write_to(&directory, project.cas_dir(), false)
.context("failed to write package contents")?;

// TODO: if MANIFEST_FILE_NAME does not exist, try to convert it

Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/patch_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::cli::IsUpToDate;
use anyhow::Context;
use clap::Args;
use pesde::{
manifest::Manifest, names::PackageNames, patches::create_patch, source::VersionId, Project,
MANIFEST_FILE_NAME,
manifest::Manifest, names::PackageNames, patches::create_patch, source::version_id::VersionId,
Project, MANIFEST_FILE_NAME,
};
use std::{path::PathBuf, str::FromStr};

Expand Down
5 changes: 2 additions & 3 deletions src/cli/commands/self_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cli::{config::read_config, version::get_or_download_version};
use clap::Args;
use pesde::Project;

#[derive(Debug, Args)]
pub struct SelfUpgradeCommand {
Expand All @@ -10,8 +9,8 @@ pub struct SelfUpgradeCommand {
}

impl SelfUpgradeCommand {
pub fn run(self, project: Project, reqwest: reqwest::blocking::Client) -> anyhow::Result<()> {
let config = read_config(project.data_dir())?;
pub fn run(self, reqwest: reqwest::blocking::Client) -> anyhow::Result<()> {
let config = read_config()?;

get_or_download_version(&reqwest, &config.last_checked_updates.unwrap().1)?;

Expand Down
11 changes: 6 additions & 5 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Context;
use serde::{Deserialize, Serialize};
use std::path::Path;

use crate::cli::home_dir;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliConfig {
Expand Down Expand Up @@ -37,8 +38,8 @@ impl Default for CliConfig {
}
}

pub fn read_config<P: AsRef<Path>>(data_dir: P) -> anyhow::Result<CliConfig> {
let config_string = match std::fs::read_to_string(data_dir.as_ref().join("config.toml")) {
pub fn read_config() -> anyhow::Result<CliConfig> {
let config_string = match std::fs::read_to_string(home_dir()?.join("config.toml")) {
Ok(config_string) => config_string,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Ok(CliConfig::default());
Expand All @@ -51,9 +52,9 @@ pub fn read_config<P: AsRef<Path>>(data_dir: P) -> anyhow::Result<CliConfig> {
Ok(config)
}

pub fn write_config<P: AsRef<Path>>(data_dir: P, config: &CliConfig) -> anyhow::Result<()> {
pub fn write_config(config: &CliConfig) -> anyhow::Result<()> {
let config_string = toml::to_string(config).context("failed to serialize config")?;
std::fs::write(data_dir.as_ref().join("config.toml"), config_string)
std::fs::write(home_dir()?.join("config.toml"), config_string)
.context("failed to write config file")?;

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub mod version;

use crate::cli::auth::get_token;
use anyhow::Context;
use pesde::{lockfile::DownloadedGraph, names::PackageNames, source::VersionId, Project};
use pesde::{
lockfile::DownloadedGraph, names::PackageNames, source::version_id::VersionId, Project,
};
use std::{collections::HashSet, str::FromStr};

pub const HOME_DIR: &str = concat!(".", env!("CARGO_PKG_NAME"));
Expand Down
2 changes: 1 addition & 1 deletion src/cli/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn update_scripts_folder(project: &Project) -> anyhow::Result<()> {
} else {
std::fs::create_dir_all(&scripts_dir).context("failed to create scripts directory")?;

let cli_config = read_config(project.data_dir())?;
let cli_config = read_config()?;

gix::prepare_clone(cli_config.scripts_repo, &scripts_dir)
.context("failed to prepare scripts repository clone")?
Expand Down
24 changes: 7 additions & 17 deletions src/cli/version.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
fs::create_dir_all,
io::Read,
path::{Path, PathBuf},
};
use std::{fs::create_dir_all, io::Read, path::PathBuf};

use anyhow::Context;
use colored::Colorize;
Expand Down Expand Up @@ -42,13 +38,10 @@ fn get_repo() -> (String, String) {

const CHECK_INTERVAL: chrono::Duration = chrono::Duration::seconds(30);

pub fn check_for_updates<P: AsRef<Path>>(
reqwest: &reqwest::blocking::Client,
data_dir: P,
) -> anyhow::Result<()> {
pub fn check_for_updates(reqwest: &reqwest::blocking::Client) -> anyhow::Result<()> {
let (owner, repo) = get_repo();

let config = read_config(&data_dir)?;
let config = read_config()?;

let version = if let Some((_, version)) = config
.last_checked_updates
Expand All @@ -71,13 +64,10 @@ pub fn check_for_updates<P: AsRef<Path>>(
.max()
.context("failed to find latest version")?;

write_config(
&data_dir,
&CliConfig {
last_checked_updates: Some((chrono::Utc::now(), version.clone())),
..config
},
)?;
write_config(&CliConfig {
last_checked_updates: Some((chrono::Utc::now(), version.clone())),
..config
})?;

version
};
Expand Down
Loading

0 comments on commit 37cc86f

Please sign in to comment.